3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 06:05:43 +01:00

Answer stability round 1: 2 🔴, 6 🟡 (0 misread); write a link's address and title, ☑ and ☐ for tasks, a nested task list under its item

This commit is contained in:
2026-11-14 15:47:07 +01:00
committed by Lillem4n
parent 7e2a1a4a7d
commit a745eaaa61
3 changed files with 163 additions and 58 deletions
+19
View File
@@ -6,6 +6,7 @@ import type { AdfAttributes, AdfDocument, AdfMark, AdfNode } from '../adf/docume
import { adfDocument, blockTaskItemChildren, propertyRuns, propertyTimeout } from './property-harness.ts'
import { adfToHtml } from '../html/emit/adf-to-html.ts'
import { adfToLosslessMarkdown } from '../markdown/emit/adf-to-markdown.ts'
import { adfToPlainText } from '../plain-text/adf-to-plain-text.ts'
import { adfToPortableMarkdown, reduceToPortable } from '../markdown/portable/adf-to-portable-markdown.ts'
import { centeredImage, externalMedia } from '../adf/plain-image.ts'
import { htmlToAdf } from '../html/parse/html-to-adf.ts'
@@ -158,6 +159,24 @@ test('a generated document writes HTML holding no character HTML cannot hold, an
)
})
test('a generated document writes plain text showing every text node\'s text in document order, whatever the whitespace between', { timeout: propertyTimeout }, () => {
fc.assert(
fc.property(adfDocument, (document) => {
const written = adfToPlainText(document)
assert.ok(written.ok, written.ok ? '' : `${written.error.code}: ${written.error.message}`)
const page = written.value.replaceAll(/\s/g, '')
let from = 0
for (const text of documentTexts(document.content ?? [])) {
const shown = text.replaceAll(/\s/g, '')
const at = page.indexOf(shown, from)
assert.ok(at >= 0, `writing ${JSON.stringify(document)}: ${JSON.stringify(shown)} is not shown after ${from}`)
from = at + shown.length
}
}),
propertyRuns(gateRuns),
)
})
test('a generated document writes HTML that reads back to it', { timeout: propertyTimeout }, () => {
fc.assert(
fc.property(adfDocument, (document) => {
+89 -40
View File
@@ -85,13 +85,38 @@ test('writes blocks as their text, a blank line apart, with bullets, numbers and
)
})
test('writes marks as the text they mark, and an empty document as nothing', () => {
const link: AdfMark = { attrs: { href: 'https://example.com/', title: 'Home' }, type: 'link' }
assert.equal(plain(adfToPlainText(document(paragraph(text('a ', { type: 'strong' }), text('b', link, { type: 'em' }), text(' c', { type: 'code' }))))), 'a b c')
test('writes marks other than a link as the text they mark', () => {
assert.equal(plain(adfToPlainText(document(paragraph(text('a ', { type: 'strong' }), text('b', { type: 'em' }), text(' c', { type: 'code' }))))), 'a b c')
})
test('writes an empty document as nothing', () => {
assert.equal(plain(adfToPlainText(document())), '')
assert.equal(plain(adfToPlainText({ type: 'doc', version: 1 })), '')
})
test('writes a link\'s address and title after the text it covers, once for a run of text nodes', () => {
const link = (href: string, title?: string): AdfMark => ({ attrs: title === undefined ? { href } : { href, title }, type: 'link' })
const written = adfToPlainText(
document(
paragraph(text('Read the '), text('do', link('https://example.com/docs', 'Guide')), text('cs', link('https://example.com/docs', 'Guide'), { type: 'strong' }), text(' and the '), text('changelog', link('https://example.com/changes')), text('.')),
paragraph(text('https://example.com/', link('https://example.com/'))),
paragraph(text('a', link('https://one.example/')), text('b', link('https://two.example/'))),
),
)
assert.equal(plain(written), 'Read the docs (https://example.com/docs, "Guide") and the changelog (https://example.com/changes).\n\nhttps://example.com/\n\na (https://one.example/)b (https://two.example/)')
})
test('writes a link with a title after its address even where the text is the address', () => {
const link: AdfMark = { attrs: { href: 'https://example.com/', title: 'Home' }, type: 'link' }
assert.equal(plain(adfToPlainText(document(paragraph(text('https://example.com/', link))))), 'https://example.com/ (https://example.com/, "Home")')
})
test('writes a link whose text is empty as nothing, and a link mark with no address as its text', () => {
const link: AdfMark = { attrs: { href: 'https://example.com/' }, type: 'link' }
assert.equal(plain(adfToPlainText(document(paragraph(text('a'), text('', link), text('b'))))), 'ab')
assert.equal(plain(adfToPlainText(document(paragraph(text('a', { attrs: { title: 'x' }, type: 'link' }))))), 'a')
})
test('writes nothing for a block holding no text, and parts the blocks around it once', () => {
assert.equal(plain(adfToPlainText(document(paragraph(), line('a'), { type: 'rule' }, paragraph(), line('b'), bullets(item())))), 'a\n\nb')
})
@@ -100,11 +125,23 @@ test('writes a text node holding nothing as nothing', () => {
assert.equal(plain(adfToPlainText(document(line('a'), paragraph(text('')), line('b')))), 'a\n\nb')
})
test('keeps the text of a paragraph on its line, and an item\'s blocks a line apart, whatever they hold', () => {
test('writes the text nodes of an item\'s paragraph on one line', () => {
assert.equal(plain(adfToPlainText(document(bullets(item(paragraph(text('a'), text('b'))))))), '• ab')
})
test('writes the items of a list a line feed apart, whatever blocks they hold', () => {
assert.equal(plain(adfToPlainText(document(bullets(item(line('a')), item({ content: [line('b')], type: 'blockquote' }))))), '• a\n• b')
})
test('writes the blocks of one item a line feed apart, under its marker', () => {
assert.equal(plain(adfToPlainText(document(bullets(item(bullets(item(line('x'))), line('z')))))), '• • x\n z')
})
test('writes an item holding an empty list before its text as that text', () => {
assert.equal(plain(adfToPlainText(document(bullets(item(bullets(item()), line('x')))))), '• x')
})
test('writes the nodes of an unknown inline container on one line', () => {
const inline: AdfNode = paragraph({ text: 'a', type: 'unknownInline' }, { text: 'b', type: 'unknownInline' })
assert.equal(plain(adfToPlainText(document(inline))), 'ab')
})
@@ -119,15 +156,29 @@ test('keeps a marker for an item whose first content is a nested list', () => {
assert.equal(plain(adfToPlainText(document(bullets(item(bullets(item(line('x')))), item(line('y')))))), '• • x\n• y')
})
test('writes a task item holding blocks as an item', () => {
const tasks: AdfNode = { content: [{ attrs: { localId: 'a', state: 'TODO' }, content: [line('a'), line('b')], type: 'blockTaskItem' }], type: 'taskList' }
assert.equal(plain(adfToPlainText(document(tasks))), '• a\n b')
test('writes a task list nested in a task list under its parent item, with no marker of its own', () => {
const task = (value: string): AdfNode => ({ attrs: { state: 'TODO' }, content: [text(value)], type: 'taskItem' })
const nested: AdfNode = { content: [task('a'), { content: [task('b')], type: 'taskList' }], type: 'taskList' }
assert.equal(plain(adfToPlainText(document(nested))), '☐ a\n ☐ b')
})
test('writes a task list and a decision list as bullets', () => {
test('writes two hard breaks in a row as a blank line', () => {
assert.equal(plain(adfToPlainText(document(paragraph(text('a'), { type: 'hardBreak' }, { type: 'hardBreak' }, text('b'))))), 'a\n\nb')
})
test('writes a code block ending in a line feed as one block, a blank line from the next', () => {
assert.equal(plain(adfToPlainText(document({ content: [text('a\n')], type: 'codeBlock' }, line('b')))), 'a\n\nb')
})
test('writes a task item holding blocks as an item', () => {
const tasks: AdfNode = { content: [{ attrs: { localId: 'a', state: 'TODO' }, content: [line('a'), line('b')], type: 'blockTaskItem' }], type: 'taskList' }
assert.equal(plain(adfToPlainText(document(tasks))), '☐ a\n b')
})
test('writes a task list with a box checked for a done item, and a decision list as bullets', () => {
const tasks: AdfNode = { content: [{ attrs: { state: 'DONE' }, content: [text('Write the spec')], type: 'taskItem' }, { attrs: { state: 'TODO' }, content: [text('Ship')], type: 'taskItem' }], type: 'taskList' }
const decisions: AdfNode = { content: [{ attrs: { state: 'DECIDED' }, content: [text('Use Node')], type: 'decisionItem' }], type: 'decisionList' }
assert.equal(plain(adfToPlainText(document(tasks, decisions))), '• Write the spec\n• Ship\n\n• Use Node')
assert.equal(plain(adfToPlainText(document(tasks, decisions))), '☑ Write the spec\n☐ Ship\n\n• Use Node')
})
test('writes a mention, an emoji, a date and a status as the text they show', () => {
@@ -162,41 +213,39 @@ test('writes a code block\'s lines and a list item\'s continuation lines under t
assert.equal(plain(adfToPlainText(document(bullets(item(code))))), '• a\n\n b')
})
test('refuses what adfToHtml refuses, with the same code and path', () => {
const deep = (levels: number): AdfNode => {
let node = line('x')
for (let level = 0; level < levels; level += 1) node = { content: [node], type: 'blockquote' }
return node
}
const refused: AdfDocument[] = [
{ type: 'doc', version: Number.NaN },
{ type: 'doc', version: 2 },
document(deep(largestNesting + 1)),
document(line('a'), deep(largestNesting + 1)),
document(paragraph(text('See '), { attrs: { url: 'https://example.com/' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' })),
]
for (const input of refused) {
function deepQuote(levels: number): AdfNode {
let node = line('x')
for (let level = 0; level < levels; level += 1) node = { content: [node], type: 'blockquote' }
return node
}
const refusedDocuments: Record<string, AdfDocument> = {
'a document version that is no number': { type: 'doc', version: Number.NaN },
'a document version 2': { type: 'doc', version: 2 },
'nesting past the limit': document(deepQuote(largestNesting + 1)),
'nesting past the limit after a block': document(line('a'), deepQuote(largestNesting + 1)),
'a card with a link mark': document(paragraph(text('See '), { attrs: { url: 'https://example.com/' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' })),
}
for (const [name, input] of Object.entries(refusedDocuments)) {
test(`refuses ${name} as adfToHtml does, with the same code and path`, () => {
assert.notEqual(refusal(adfToHtml(input)), 'written')
assert.equal(refusal(adfToPlainText(input)), refusal(adfToHtml(input)))
}
})
})
}
test('counts a table\'s levels as adfToHtml does', () => {
const deep = (levels: number): AdfNode => {
let node = line('x')
for (let level = 0; level < levels; level += 1) node = { content: [node], type: 'blockquote' }
return node
for (const cell of ['tableCell', 'tableHeader']) {
const table = (levels: number): AdfDocument => document({ content: [{ content: [{ content: [deepQuote(levels)], type: cell }], type: 'tableRow' }], type: 'table' })
for (let levels = largestNesting - 3; levels <= largestNesting + 1; levels += 1) {
test(`counts the levels of a ${cell} holding ${levels} levels as adfToHtml does`, () => {
assert.equal(refusal(adfToPlainText(table(levels))), refusal(adfToHtml(table(levels))))
})
}
const outcomes = new Set<string>()
for (const cell of ['tableCell', 'tableHeader']) {
for (let levels = largestNesting - 3; levels <= largestNesting + 1; levels += 1) {
const table: AdfNode = { content: [{ content: [{ content: [deep(levels)], type: cell }], type: 'tableRow' }], type: 'table' }
assert.equal(refusal(adfToPlainText(document(table))), refusal(adfToHtml(document(table))))
outcomes.add(refusal(adfToPlainText(document(table))).slice(0, 12))
}
}
assert.deepEqual([...outcomes].sort(), ['unsupported-', 'written'])
})
test(`writes a ${cell} just inside the limit and refuses one past it`, () => {
assert.equal(refusal(adfToPlainText(table(largestNesting - 3))), 'written')
assert.equal(refusal(adfToPlainText(table(largestNesting + 1))).startsWith('unsupported-nesting-depth'), true)
})
}
test('writes blocks nested 500 levels', () => {
let node = line('x')
+55 -18
View File
@@ -2,23 +2,25 @@ import type { AdfDocument, AdfNode } from '../adf/document.ts'
import { blockNodeModel } from '../adf/block-nodes.ts'
import { failure, success, type ConvertErrorPath, type Result } from '../result.ts'
import { inlineNodeModel } from '../adf/inline-nodes.ts'
import { linkAttrs, type LinkAttrs } from '../adf/mark-attributes.ts'
import { largestNesting } from '../nesting.ts'
import { longestOutput, tooLong, withinEngineLimits } from '../length-bound.ts'
import { nodeAttrs, nodeContent, readAdfDocument, writableContent } from '../adf/document.ts'
import { nodeAttrs, nodeContent, nodeMarks, readAdfDocument, writableContent } from '../adf/document.ts'
import { shownText } from '../adf/shown-text.ts'
// `gap`: the line feeds the next text needs before it, at least; `leave`: the state a list item restores, its `length` telling whether the item wrote anything; `marker`: the list marker of the item a node is; `depth`: the nesting level, as `largestNesting` counts it; `parent`: the node holding this one, from which its path is built when a refusal needs it.
// `gap`: the line feeds the next text needs before it at least; `leave`: the state to restore after a list item, and its `length` at the start, which shows whether it wrote anything; `marker`: the marker this node has in its list; `suffix`: text written right after this node; `depth`: the nesting level as `largestNesting` counts it; `parent`: the node holding this one, from which a refusal's path is built.
type Place = { index: number; parent: Place | undefined }
type Step =
| { kind: 'gap'; lines: number }
| { kind: 'leave'; indent: string; lead: string | undefined; length: number }
| { depth: number; kind: 'node'; marker: string | undefined; node: AdfNode; place: Place | undefined }
| { depth: number; kind: 'node'; marker: string | undefined; node: AdfNode; place: Place | undefined; suffix: string | undefined }
// `lines`: the line feeds between a node's children; `level`: how much deeper they nest.
type Layout = { level: number; lines: number }
const bulletLists = ['bulletList', 'decisionList', 'taskList']
const itemTypes = ['blockTaskItem', 'listItem']
const markedItems = ['blockTaskItem', 'decisionItem', 'listItem', 'taskItem']
const shownTextTypes = ['date', 'emoji', 'mention', 'status']
export function adfToPlainText(document: AdfDocument): Result<string> {
@@ -39,13 +41,17 @@ function writeText(content: readonly AdfNode[]): Result<string> {
let lead: string | undefined
function emit(text: string): void {
if (text === '') return
const head = length === 0 || gap > 0 ? `${length === 0 ? '' : '\n'.repeat(gap)}${lead ?? indent}` : ''
const body = text.replaceAll(/\n(?=[^\n])/g, `\n${indent}`)
pieces.push(head, body)
length += head.length + body.length
gap = 0
lead = undefined
let end = text.length
while (end > 0 && text[end - 1] === '\n') end -= 1
if (end > 0) {
const head = length === 0 || gap > 0 ? `${length === 0 ? '' : '\n'.repeat(gap)}${lead ?? indent}` : ''
const body = text.slice(0, end).replaceAll(/\n(?=[^\n])/g, `\n${indent}`)
pieces.push(head, body)
length += head.length + body.length
gap = 0
lead = undefined
}
gap = Math.max(gap, text.length - end)
}
const pending: Step[] = childSteps(content, { level: 1, lines: 2 }, 0, undefined, () => undefined).reverse()
@@ -59,17 +65,18 @@ function writeText(content: readonly AdfNode[]): Result<string> {
lead = step.length === length ? step.lead : undefined
continue
}
const { depth, marker, node, place } = step
const { depth, marker, node, place, suffix } = step
if (depth > largestNesting) return failure('unsupported-nesting-depth', `the document nests deeper than the ${largestNesting} levels the writer carries`, pathTo(place))
if (marker !== undefined) {
pending.push({ indent, kind: 'leave', lead, length })
lead = lead === undefined ? `${indent}${marker}` : `${lead}${marker}`
indent = `${indent}${' '.repeat(marker.length)}`
}
if (node.type === 'hardBreak') gap = Math.max(gap, 1)
if (node.type === 'hardBreak') gap += 1
else if (shownTextTypes.includes(node.type)) emit(shownText(node) ?? '')
else {
if (typeof node.text === 'string') emit(node.text)
if (suffix !== undefined) emit(suffix)
const layout = layoutOf(node)
for (const child of childSteps(nodeContent(node), layout, depth + layout.level, place, markerFor(node)).reverse()) pending.push(child)
}
@@ -79,16 +86,37 @@ function writeText(content: readonly AdfNode[]): Result<string> {
}
// The steps for `nodes` in document order, `layout.lines` line feeds apart.
function childSteps(nodes: readonly AdfNode[], layout: Layout, depth: number, parent: Place | undefined, markerFor: (index: number) => string | undefined): Step[] {
function childSteps(nodes: readonly AdfNode[], layout: Layout, depth: number, parent: Place | undefined, markerFor: (index: number, node: AdfNode) => string | undefined): Step[] {
const steps: Step[] = []
const links = nodes.map(linkOf)
let run = ''
for (const [index, node] of nodes.entries()) {
if (index > 0) steps.push({ kind: 'gap', lines: layout.lines })
steps.push({ depth, kind: 'node', marker: markerFor(index), node, place: { index, parent } })
const link = links[index]
const before = links[index - 1]
run = link !== undefined && before !== undefined && sameLink(link, before) ? `${run}${node.text ?? ''}` : node.text ?? ''
const after = links[index + 1]
const ends = link !== undefined && !(after !== undefined && sameLink(link, after))
steps.push({ depth, kind: 'node', marker: markerFor(index, node), node, place: { index, parent }, suffix: ends && run !== '' ? linkSuffix(link, run) : undefined })
}
return steps
}
// Children are written in a line where they are inline, a line apart inside a list and an item, a blank line apart between blocks. A list's items nest one level below it, an item's blocks at its own.
function linkOf(node: AdfNode): LinkAttrs | undefined {
const mark = nodeMarks(node).find((held) => held.type === 'link')
return node.type === 'text' && mark !== undefined ? linkAttrs(nodeAttrs(mark)) : undefined
}
function sameLink(left: LinkAttrs, right: LinkAttrs): boolean {
return left.href === right.href && left.title === right.title
}
function linkSuffix(link: LinkAttrs, text: string): string | undefined {
if (link.title === undefined) return text === link.href ? undefined : ` (${link.href})`
return ` (${link.href}, "${link.title}")`
}
// Children stand on one line where they are inline, a line feed apart in a list and an item, a blank line apart between blocks. A list's items nest one level below it, an item's blocks at its own.
function layoutOf(node: AdfNode): Layout {
if (bulletLists.includes(node.type) || node.type === 'orderedList') return { level: 1, lines: 1 }
if (itemTypes.includes(node.type)) return { level: 0, lines: 1 }
@@ -98,12 +126,21 @@ function layoutOf(node: AdfNode): Layout {
return nodeContent(node).every((child) => child.type === 'text' || inlineNodeModel(child.type) !== undefined) ? { level: 0, lines: 0 } : { level: 1, lines: 2 }
}
function markerFor(node: AdfNode): (index: number) => string | undefined {
if (bulletLists.includes(node.type)) return () => '• '
// A child of a list that is no item, such as a task list nested in one, gets blank margin so it stands under the item before it.
function markerFor(node: AdfNode): (index: number, child: AdfNode) => string | undefined {
if (bulletLists.includes(node.type)) return (_index, child) => (markedItems.includes(child.type) ? bulletOf(child) : ' ')
if (node.type !== 'orderedList') return () => undefined
const order = nodeAttrs(node)['order']
const first = typeof order === 'number' && Number.isSafeInteger(order) ? order : 1
return (index) => `${first + index}. `
return (index, child) => {
const number = `${first + index}. `
return markedItems.includes(child.type) ? number : ' '.repeat(number.length)
}
}
function bulletOf(item: AdfNode): string {
if (item.type !== 'blockTaskItem' && item.type !== 'taskItem') return '• '
return nodeAttrs(item)['state'] === 'DONE' ? '☑ ' : '☐ '
}
function pathTo(place: Place | undefined): ConvertErrorPath {