mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 14:45: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:
@@ -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')
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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'],
|
||||
],
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user