From 1ff27164f81ba599c0971281b31161bdb6729e6a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 4 Oct 2026 23:01:16 +0200 Subject: [PATCH] Answer architecture round 2: the spelling memo, headroom and the escaped backtick lengths state their rule at the code, and the highlight counter is named for what it counts --- src/markdown/emit/adf-to-markdown.ts | 4 ++-- src/markdown/emit/line-escaping.ts | 1 + src/markdown/parse/inline-content.ts | 13 +++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/markdown/emit/adf-to-markdown.ts b/src/markdown/emit/adf-to-markdown.ts index 99d6436..35ed05c 100644 --- a/src/markdown/emit/adf-to-markdown.ts +++ b/src/markdown/emit/adf-to-markdown.ts @@ -19,6 +19,7 @@ import { tryPipeTable } from './pipe-table.ts' type BlockContainer = 'directive' | 'document' | 'list-item' type BlockSpelling = 'commonmark' | 'directive' | 'list' +// headroom: the nesting levels the deepest node below may still spend before `largestNesting` refuses it. type EmittedBlock = { headroom: number; spelling: BlockSpelling; text: string } type KeptSpelling = { block: EmittedBlock | undefined; depth: number } type PlacedBlock = Omit & { node: AdfNode } @@ -47,7 +48,6 @@ export function writeMarkdown(document: AdfDocument, flavour: Flavour): Result { let headroom = largestNesting - depth if (headroom < 0) return tooDeep(path) @@ -114,7 +114,7 @@ function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, wri const kept = memo?.get(node) if (kept !== undefined) { if (kept.block === undefined) return undefined - // A read below the fill would skip the depth guards the walk it replaces runs (docs/decisions.md §The spelling memo). + // An entry holds the spelling at its fill depth: a read at or above it rebases headroom, a read below re-spells, since a hit skips the depth guards the walk runs. if (depth <= kept.depth) return success({ ...kept.block, headroom: kept.block.headroom + kept.depth - depth }) } const spelled = spellReadableBlock(node, path, depth, writing) diff --git a/src/markdown/emit/line-escaping.ts b/src/markdown/emit/line-escaping.ts index d50c4d6..fa09f12 100644 --- a/src/markdown/emit/line-escaping.ts +++ b/src/markdown/emit/line-escaping.ts @@ -124,6 +124,7 @@ function claimsHighlight(scan: string, index: number): boolean { // CommonMark reads no escape inside a code span, so a backtick string an escape forms or splits off still closes one an earlier bare run opens. function escapeClosedRuns(scan: string, escapings: readonly InlineEscaping[], claimed: ReadonlySet): ReadonlySet { const escaped = new Set(claimed) + // The lengths of the backtick strings an escape forms or splits off: a bare run that long closes the span one opens. const formed = new Set() let end = scan.length - 1 while (end >= 0) { diff --git a/src/markdown/parse/inline-content.ts b/src/markdown/parse/inline-content.ts index 6ca3bba..5f4dda0 100644 --- a/src/markdown/parse/inline-content.ts +++ b/src/markdown/parse/inline-content.ts @@ -37,7 +37,7 @@ export type LinkDefinitions = ReadonlyMap type Bracket = { active: boolean; image: boolean; kind: 'open'; start: number } -type HighlightDelimiter = { closes: boolean; holder: AdfNode; index: number; line: number; opens: boolean; position: number } +type HighlightDelimiter = { closes: boolean; holder: AdfNode; index: number; opens: boolean; position: number; stretch: number } type Pairing = EmphasisPairing @@ -586,25 +586,26 @@ function markPairings(pieces: readonly Piece[], nodes: Inline[][], pairings: rea function highlightDelimiters(pieces: readonly Piece[], nodes: readonly Inline[][]): HighlightDelimiter[] { const found: HighlightDelimiter[] = [] - let line = 0 + // A stretch is a run of text no other inline node breaks. + let stretch = 0 let position = 0 for (const [index, piece] of pieces.entries()) { const held = adfNodes(nodes[index] ?? []) const [holder] = held if (piece.kind === 'highlight' && holder !== undefined) { - found.push({ closes: piece.closes, holder, index, line, opens: piece.opens, position }) + found.push({ closes: piece.closes, holder, index, opens: piece.opens, position, stretch }) position += highlightDelimiter.length continue } for (const node of held) { if (node.type === 'text') position += node.text?.length ?? 0 - else line += 1 + else stretch += 1 } } return found } -// Each opener takes the next closer holding at least one character after it, both in one line and under the same marks. +// Each opener takes the next closer holding at least one character after it, both in one stretch and under the same marks. function pairedHighlights(pieces: readonly Piece[], nodes: readonly Inline[][]): { closer: number; opener: number }[] { const found = highlightDelimiters(pieces, nodes) const paired: { closer: number; opener: number }[] = [] @@ -616,7 +617,7 @@ function pairedHighlights(pieces: readonly Piece[], nodes: readonly Inline[][]): let candidate = found[closer] while (candidate !== undefined && (!candidate.closes || candidate.position < earliest)) candidate = found[(closer += 1)] if (candidate === undefined) break - if (candidate.line !== opener.line || !identicalMarks(nodeMarks(opener.holder), nodeMarks(candidate.holder))) continue + if (candidate.stretch !== opener.stretch || !identicalMarks(nodeMarks(opener.holder), nodeMarks(candidate.holder))) continue paired.push({ closer: candidate.index, opener: opener.index }) resume = candidate.position + highlightDelimiter.length }