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

Answer stability round 1: 0 🔴, 4 🟡 (0 misread); cap the every mode alone, name the cap's steps, pin links and highlights at the cap

This commit is contained in:
2026-10-30 21:00:05 +01:00
committed by Lillem4n
parent d0b2e22aa8
commit b0a94fb896
5 changed files with 19 additions and 9 deletions
+3 -2
View File
@@ -452,12 +452,13 @@ test('carries the mark spelling that cannot close where it sits, and keeps the l
assert.equal(emitted(marked('^', { type: 'strike' }), b, marked('c', { type: 'strike' })), '!adf:carry{json="{\\"marks\\":[{\\"type\\":\\"strike\\"}],\\"text\\":\\"^\\",\\"type\\":\\"text\\"}"}b~~c~~\n')
})
test('carries every node holding em, strong or strike in a line still misread after 32 passes', () => {
test('carries every node holding em, strike or strong, and no other, in a line still misread after 32 passes', () => {
const marked = (text: string, ...types: string[]): AdfNode => ({ marks: types.map((type) => ({ type })), text, type: 'text' })
const carry = (node: AdfNode): string => `!adf:carry{json="${JSON.stringify(node).replaceAll('"', '\\"')}"}`
const repeat = [marked(' ~~ ', 'em', 'strong'), marked(')', 'em', 'strike', 'strong'), { text: 'a', type: 'text' }, marked('.unun', 'strike', 'strong')]
const written = repeat.map((node) => (node.marks === undefined ? 'a' : carry(node))).join('')
assert.equal(markdown(adfToLosslessMarkdown(document(paragraph(...Array.from({ length: 40 }, () => repeat).flat())))), `${written.repeat(40)}\n`)
const line = [...Array.from({ length: 40 }, () => repeat).flat(), { text: ' ', type: 'text' }, { marks: [{ attrs: { href: 'u' }, type: 'link' }], text: 'b', type: 'text' }]
assert.equal(markdown(adfToLosslessMarkdown(document(paragraph(...line)))), `${written.repeat(40)} [b](u)\n`)
})
test('refuses a node carrying one mark type twice', () => {
+7 -3
View File
@@ -27,7 +27,7 @@ type Emission = { carries: [NodeRange, ...NodeRange[]]; segments?: undefined } |
type Fallback = [NodeRange, ...NodeRange[]] | 'opening-link'
// How many fallbacks one pass applies: `every`, or the `first` alone; a property checks that both write the same line.
// How many fallbacks one pass applies: `every`, or the `first` alone and with no cap; a property checks that both write the same line.
export type FallbacksPerPass = 'every' | 'first'
type InlineContext = {
@@ -47,7 +47,7 @@ type LineAttempt = { fallback: Fallback; line?: undefined } | { fallback?: undef
type LineFallbacks = { carried: Set<number>; flavour: WrittenFlavour; openingLinkAsDirective: boolean }
// docs/decisions.md §A line reads back at most 32 times, then takes back every emphasis run.
// docs/decisions.md §After 32 passes, a line carries or drops every em, strike and strong mark.
export const readBackPasses = 32
export type PortableLineFallback = { kind: 'opening-link' } | { kind: 'unspellable-run'; runs: [MarkRun, ...MarkRun[]] }
@@ -103,10 +103,14 @@ function emitLine(nodes: readonly AdfNode[], container: LineContainer, path: Con
if (attempt.fallback === undefined) return success({ line: attempt.line, openingLinkAsDirective: fallbacks.openingLinkAsDirective, segments: attempt.segments })
const taken = takeFallback(fallbacks, takenThisPass(attempt.fallback, perPass), path)
if (!taken.ok) return taken
if (pass === readBackPasses) for (const [index, node] of nodes.entries()) if (nodeMarks(node).some(isEmphasis)) fallbacks.carried.add(index)
if (perPass === 'every' && pass === readBackPasses) carryEveryEmphasis(fallbacks, nodes)
}
}
function carryEveryEmphasis(fallbacks: LineFallbacks, nodes: readonly AdfNode[]): void {
for (const [index, node] of nodes.entries()) if (nodeMarks(node).some(isEmphasis)) fallbacks.carried.add(index)
}
function takenThisPass(fallback: Fallback, perPass: FallbacksPerPass): Fallback {
return fallback === 'opening-link' || perPass === 'every' ? fallback : [fallback[0]]
}