3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 06:35:44 +01:00

Answer stability round 1: 1 🔴, 6 🟡 (0 misread); a carry refuses where the walk finds it, tests pin each batching guard, the changelog names the nested emphasis still quadratic, item 158 files it

This commit is contained in:
2026-10-29 16:19:12 +01:00
committed by Lillem4n
parent 1ff3ad6e9b
commit dd4ff03909
9 changed files with 89 additions and 19 deletions
+4 -1
View File
@@ -206,10 +206,13 @@ const positions = fc.letrec<Positions>((tie) => {
})
// Short texts under a few marks beside nodes only the carry spells: the lines where one misread or carried run sits beside another.
const emphasisStack = fc.uniqueArray(fc.constantFrom<AdfMark>({ type: 'em' }, { type: 'strike' }, { type: 'strong' }), { maxLength: 3, minLength: 2, selector: (held) => held.type })
const lineMark = fc.constantFrom<AdfMark>({ type: 'backgroundColor' }, { type: 'code' }, { type: 'em' }, { attrs: { href: 'u' }, type: 'link' }, { type: 'link' }, { type: 'odd' }, { type: 'strike' }, { type: 'strong' })
const lineMarks = fc.uniqueArray(lineMark, { maxLength: 3, selector: (held) => held.type })
const lineText = fc.string({ maxLength: 3, minLength: 1, unit: fc.constantFrom(' ', '(', '*', '-', '.', '0', '=', '[', '_', '`', 'a', 'un') })
const lineLeaf = fc.oneof(
{ arbitrary: fc.record({ marks: lineMarks, text: fc.string({ maxLength: 3, minLength: 1, unit: fc.constantFrom(' ', '*', '-', '.', '0', '=', '[', '_', '`', 'a', 'un') }) }).map((held): AdfNode => ({ ...held, type: 'text' })), weight: 8 },
{ arbitrary: fc.record({ marks: emphasisStack, text: lineText }).map((held): AdfNode => ({ ...held, type: 'text' })), weight: 4 },
{ arbitrary: fc.record({ marks: lineMarks, text: lineText }).map((held): AdfNode => ({ ...held, type: 'text' })), weight: 8 },
{ arbitrary: fc.constant<AdfNode>({ type: 'hardBreak' }), weight: 1 },
{ arbitrary: fc.record({ attrs: fc.constantFrom({ id: 'a' }, { id: 'a', odd: 'b' }), marks: lineMarks }).map((held): AdfNode => ({ ...held, type: 'mention' })), weight: 1 },
)
@@ -40,6 +40,15 @@ test('names the node a refusal came from, and no source the emitter never read',
assert.equal(position(adfToLosslessMarkdown(document(paragraph({ text: 'x', type: 'text' }), list))), undefined)
})
test('refuses a node whose carry nests too deep ahead of a later node in its line', () => {
let id: JsonValue = 'a'
for (let depth = 0; depth < largestNesting; depth += 1) id = { id }
const twiceStrong: AdfNode = { marks: [{ type: 'strong' }, { type: 'strong' }], text: 'x', type: 'text' }
const refused = adfToLosslessMarkdown(document(paragraph({ attrs: { id }, type: 'mention' }, twiceStrong)))
assert.equal(code(refused), 'unsupported-nesting-depth')
assert.deepEqual(path(refused), ['content', 0, 'content', 0])
})
test('refuses a value that is not an ADF document, naming the check it failed', () => {
assert.equal(markdown(adfToLosslessMarkdown({ type: 'doc', version: Number.NaN })), 'not-an-adf-document: an ADF document holds a version number: found NaN')
})
+2 -3
View File
@@ -83,11 +83,10 @@ export function writtenText(text: BlockText): string {
// Innermost first: whether a line is empty when an outer prefix meets it depends on the prefixes inside.
function prefixedLine(line: string, open: readonly Prefix[], innermostQuote: number | undefined, started: number): string {
// Started prefixes inside the innermost quote leave an empty line empty, so its walk begins at that quote.
// An empty line takes no indent, so where every prefix has started its walk begins at the innermost quote.
const walked = line === '' && started === open.length ? open.slice(0, (innermostQuote ?? -1) + 1) : open
return walked.reduceRight((written, prefix, depth) => {
if (prefix.kind === 'quoted') return written === '' ? '>' : `> ${written}`
if (depth >= started) return `${prefix.head}${written}`
return written === '' ? '' : `${prefix.indent}${written}`
return depth >= started ? `${prefix.head}${written}` : `${prefix.indent}${written}`
}, line)
}
+9 -1
View File
@@ -10,6 +10,14 @@ test('spells a line alike taking every fallback a pass finds or the first alone'
fc.property(markedLine, lineContainer, (nodes, container) => {
assert.deepEqual(emitInlineLine(nodes, container, [], 'lossless'), emitInlineLine(nodes, container, [], 'lossless', 'first'))
}),
propertyRuns(3000),
// Lines where carrying one run lets a neighbour read as written, which the seed's runs rarely reach.
{
...propertyRuns(3000),
examples: [
[[{ marks: [{ type: 'em' }], text: '-', type: 'text' }, { text: '0', type: 'text' }, { marks: [{ type: 'em' }, { type: 'strong' }], text: 'un', type: 'text' }, { text: '0', type: 'text' }], 'paragraph'],
[[{ marks: [{ type: 'em' }, { type: 'strike' }], text: 'a', type: 'text' }, { text: 'a', type: 'text' }, { marks: [{ type: 'strong' }, { type: 'em' }], text: 'a', type: 'text' }, { marks: [{ type: 'strong' }], text: 'a', type: 'text' }], 'paragraph'],
[[{ marks: [{ type: 'em' }], text: 'a', type: 'text' }, { marks: [{ type: 'em' }, { type: 'strike' }, { type: 'strong' }], text: 'a', type: 'text' }, { text: 'a', type: 'text' }], 'paragraph'],
],
},
)
})
+17 -7
View File
@@ -278,8 +278,19 @@ function emitLeaf(node: AdfNode, context: InlineContext, index: number): Result<
if (model === undefined) return success(emitText(node, context, index))
const held = heldContentOrText(node)
if (held !== undefined) return failure('unsupported-node-shape', `a ${node.type} node holds neither content nor text: this one holds ${held}`, nodePath(context, index))
if (node.type === 'hardBreak') return success(emitHardBreak(node, model, context, index))
return success(emitInlineDirective(node, model, index))
const emission = node.type === 'hardBreak' ? emitHardBreak(node, model, context, index) : emitInlineDirective(node, model, index)
return checkedCarry(emission, [node], context)
}
// Builds a carry's JSON where the walk finds it, so its refusal comes ahead of any later node's.
function checkedCarry(emission: Emission, nodes: readonly AdfNode[], context: InlineContext): Result<Emission> {
const range = emission.carries?.[0]
if (range === undefined) return success(emission)
for (const [offset, node] of nodes.entries()) {
const carried = carriedInline(node, nodePath(context, range.first + offset))
if (!carried.ok) return carried
}
return success(emission)
}
function emitHardBreak(node: AdfNode, model: InlineNodeModel, context: InlineContext, index: number): Emission {
@@ -311,10 +322,9 @@ function emitMarkedRun(nodes: readonly AdfNode[], mark: AdfMark, depth: number,
const range: NodeRange = { first: index, last: index + nodes.length - 1 }
if (mark.type === 'backgroundColor' && flavourClaims[context.flavour].highlights) return emitHighlight(nodes, depth, range, context)
const spelling = markSpelling(mark.type)
if (spelling === undefined) return success({ carries: [range] })
const attributes = spellMarkAttributes(mark, spelling)
if (attributes === undefined) return success({ carries: [range] })
if (spelling.kind === 'code') return success(emitCodeSpan(nodes, depth, range))
const attributes = spelling === undefined ? undefined : spellMarkAttributes(mark, spelling)
if (spelling === undefined || attributes === undefined) return checkedCarry({ carries: [range] }, nodes, context)
if (spelling.kind === 'code') return checkedCarry(emitCodeSpan(nodes, depth, range), nodes, context)
if (spelling.kind === 'emphasis') return emitEmphasis(nodes, spelling.spelling, depth, range, context)
const link = spelling.kind === 'link' ? tryLink(nodes, mark, depth, range, context) : undefined
if (link !== undefined) return link
@@ -366,7 +376,7 @@ function needsPadding(text: string): boolean {
function tryLink(nodes: readonly AdfNode[], mark: AdfMark, depth: number, range: NodeRange, context: InlineContext): Result<Emission> | undefined {
const href = linkHref(nodeAttrs(mark))
if (href === undefined) return success({ carries: [range] })
if (href === undefined) return checkedCarry({ carries: [range] }, nodes, context)
const opening = depth === 0 && range.first === 0 && context.openingLinkAsDirective
const commonMark = opening ? undefined : commonMarkLink(nodeAttrs(mark), href, nodes, depth + 1, context.bracketed, titleEscapes(context.flavour, context.container))
if (commonMark === undefined) return undefined
@@ -146,6 +146,8 @@ test('spells a task list as a bullet list whose items lead with their state', ()
const blockTask = node('blockTaskItem', { state: 'DONE' }, said('First.'), said('Second.'))
const codeTask = node('blockTaskItem', { state: 'TODO' }, { content: [text('x')], type: 'codeBlock' })
assert.equal(portable(node('taskList', {}, blockTask, codeTask)), '- [x] First.\n\n Second.\n- [ ]\n\n```\nx\n```\n')
const brokenTask = node('blockTaskItem', { state: 'TODO' }, paragraph(text('a'), { type: 'hardBreak' }, text('b')), said('c'))
assert.equal(portable(node('taskList', {}, brokenTask)), '- [ ] a\\\n b\n\n c\n')
const parent = node('blockTaskItem', { state: 'TODO' }, said('q'), said('q2'))
assert.equal(portable(node('taskList', {}, parent, node('taskList', {}, task('DONE', 'n')), task('DONE', 'B'))), '- [ ] q\n\n q2\n - [x] n\n- [x] B\n')
const holding = node('blockTaskItem', { state: 'TODO' }, said('A'), node('taskList', {}, task('TODO', 'c')))
@@ -177,6 +179,9 @@ test('escapes text a renderer would take as a flavour marker, and only there', (
assert.equal(portable({ content: [said('[!NOTE] x'), said('[!TIP]')], type: 'blockquote' }), '> \\[!NOTE] x\n>\n> [!TIP]\n')
assert.equal(portable({ content: [said('[!NOTE]x')], type: 'blockquote' }, said('[!NOTE]')), '> [!NOTE]x\n\n[!NOTE]\n')
assert.equal(portable(bulletList(item(said('[x] a')), item(said('[ ]')))), '- \\[x] a\n- \\[ ]\n')
const broken = (value: string): AdfNode => paragraph(text(value), { type: 'hardBreak' }, text('a'))
assert.equal(portable(bulletList(item(broken('[x]')), item(said('[x]\\')))), '- \\[x]\\\n a\n- [x]\\\n')
assert.equal(portable({ content: [broken('[!NOTE]')], type: 'blockquote' }), '> \\[!NOTE]\\\n> a\n')
assert.equal(portable(bulletList(item(said('[x] a')), item(said('b'))), node('orderedList', { order: 1 }, item(said('[x] c')))), '- \\[x] a\n- b\n\n1. \\[x] c\n')
const task = (state: string, value: string): AdfNode => node('taskItem', { state }, text(value))
assert.equal(portable(node('taskList', {}, task('DONE', '[x] a'), task('TODO', '==b=='))), '- [x] [x] a\n- [ ] \\==b\\==\n')
@@ -403,6 +408,11 @@ test('gives up every highlight run whose own delimiter cannot flank in one fallb
assert.deepEqual(portableLineFallback(line, 'paragraph', []), { ok: true, value: { kind: 'unspellable-run', runs } })
})
test('refuses a portable line holding a node only the carry spells, at the line', () => {
const refused = portableLineFallback([text('x', { type: 'odd' })], 'paragraph', ['content', 0])
assert.deepEqual(refused.ok ? refused : [refused.error.code, refused.error.path], ['unsupported-node-shape', ['content', 0]])
})
test('breaks a line at a newline and trims whitespace at every edge CommonMark strips', () => {
assert.equal(portable(paragraph(text(' \n a \n b\n'))), 'a\\\nb\n')
assert.equal(portable(paragraph({ type: 'hardBreak' }, text('a'), { attrs: { text: '\n' }, type: 'hardBreak' }, text('b'), { type: 'hardBreak' })), 'a\\\nb\n')
@@ -10,7 +10,13 @@ test('reduces a line alike dropping every run a pass may or the first alone', ()
fc.property(markedLine, lineContainer, (nodes, container) => {
assert.deepEqual(reduceInline(nodes, container, [], 0), reduceInline(nodes, container, [], 0, 'first'))
}),
// A line holding a later run that must not drop with the first, which the seed's runs rarely reach.
{ ...propertyRuns(3000), examples: [[[{ attrs: { id: 'a' }, type: 'mention' }, { marks: [{ type: 'strike' }, { type: 'strong' }], text: 'un*', type: 'text' }, { marks: [{ type: 'strong' }], text: '[', type: 'text' }, { text: 'a ', type: 'text' }, { text: ' ', type: 'text' }], 'heading']] },
// Lines holding a later run that must not drop with the first, which the seed's runs rarely reach.
{
...propertyRuns(3000),
examples: [
[[{ attrs: { id: 'a' }, type: 'mention' }, { marks: [{ type: 'strike' }, { type: 'strong' }], text: 'un*', type: 'text' }, { marks: [{ type: 'strong' }], text: '[', type: 'text' }, { text: 'a ', type: 'text' }, { text: ' ', type: 'text' }], 'heading'],
[[{ marks: [{ type: 'strong' }], text: '-', type: 'text' }, { text: '0', type: 'text' }, { marks: [{ type: 'em' }, { type: 'strong' }], text: 'un ', type: 'text' }, { text: ' ', type: 'text' }], 'heading'],
],
},
)
})