From 2f9a1b3540371d40e3f95587d3b47a6ec834af08 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 28 Oct 2026 18:10:03 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=202:=201=20?= =?UTF-8?q?=F0=9F=94=B4,=200=20=F0=9F=9F=A1=20(0=20misread);=20a=20line=20?= =?UTF-8?q?gives=20up=20every=20highlight=20run=20whose=20own=20delimiters?= =?UTF-8?q?=20cannot=20flank=20in=20one=20pass,=20as=20shipped,=20so=20a?= =?UTF-8?q?=20line=20of=20them=20re-emits=20once,=20and=20the=20reader=20r?= =?UTF-8?q?ecords=20no=20highlight=20pairs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/markdown/emit/inline-line.ts | 8 +++--- src/markdown/emit/line-escaping.ts | 26 +++++++++++-------- src/markdown/inline-content.ts | 20 ++++++-------- .../portable/adf-to-portable-markdown.test.ts | 8 ++++++ src/markdown/portable/inline-reduction.ts | 12 ++++++--- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/markdown/emit/inline-line.ts b/src/markdown/emit/inline-line.ts index 97fafb1..c198da7 100644 --- a/src/markdown/emit/inline-line.ts +++ b/src/markdown/emit/inline-line.ts @@ -41,7 +41,7 @@ type LineAttempt = { fallback: NodeRange | 'opening-link'; line?: undefined } | type LineFallbacks = { carried: Set; flavour: WrittenFlavour; openingLinkAsDirective: boolean } -export type PortableLineFallback = { kind: 'opening-link' } | { kind: 'unspellable-run'; run: MarkRun } +export type PortableLineFallback = { kind: 'opening-link' } | { kind: 'unspellable-run'; runs: [MarkRun, ...MarkRun[]] } export function emitInlineLine(nodes: readonly AdfNode[], container: LineContainer, path: ConvertErrorPath, flavour: WrittenFlavour): Result { const emitted = emitLine(nodes, container, path, flavour) @@ -136,7 +136,7 @@ function lineSegments(nodes: readonly AdfNode[], container: LineContainer, path: function attemptLine(segments: readonly InlineSegment[], container: LineContainer, flavour: WrittenFlavour): LineAttempt { const verdict = lineVerdict(segments, container, flavour) if (verdict.kind === 'opening-link') return { fallback: 'opening-link' } - if (verdict.kind === 'unspellable-run') return { fallback: verdict.run } + if (verdict.kind === 'unspellable-run') return { fallback: verdict.runs[0] } return { line: verdict.text } } @@ -144,8 +144,8 @@ function attemptLine(segments: readonly InlineSegment[], container: LineContaine function lineVerdict(segments: readonly InlineSegment[], container: LineContainer, flavour: WrittenFlavour): PortableLineFallback | { kind: 'line'; text: string } { const assembled = assembleInlineLine(segments, container, flavour) if (assembled.openingLinkAsDirective) return { kind: 'opening-link' } - const run = assembled.unspellableRun - return run === undefined ? { kind: 'line', text: assembled.line } : { kind: 'unspellable-run', run } + const [run, ...others] = assembled.unspellableRuns + return run === undefined ? { kind: 'line', text: assembled.line } : { kind: 'unspellable-run', runs: [run, ...others] } } // A space or tab CommonMark strips at a line edge rides !adf:text{text="…"}. diff --git a/src/markdown/emit/line-escaping.ts b/src/markdown/emit/line-escaping.ts index 4080b6b..91d6dc0 100644 --- a/src/markdown/emit/line-escaping.ts +++ b/src/markdown/emit/line-escaping.ts @@ -2,7 +2,7 @@ import type { Claims, WrittenFlavour } from '../flavour.ts' import type { InlineReading } from '../inline-content.ts' import type { InlineSource, InlineToken } from '../inline-tokens.ts' import type { LineContainer } from '../line-container.ts' -import { delimiterRunToken, inlineSource, readInlineToken } from '../inline-tokens.ts' +import { delimiterRunToken, highlightFlanking, inlineSource, readInlineToken } from '../inline-tokens.ts' import { escapesLineClaim, type LinePosition } from '../commonmark/grammar.ts' import { flavourClaims } from '../flavour.ts' import { isBareDelimiterRow } from '../pipe-table-syntax.ts' @@ -26,7 +26,7 @@ export type InlineSegment = | { emphasis?: undefined; escaping: 'none'; highlight?: undefined; nodes: NodeRange; text: string } | { emphasis?: undefined; escaping: InlineEscaping; highlight?: undefined; nodes?: undefined; text: string } -export type AssembledLine = { line: string; openingLinkAsDirective?: true; unspellableRun: MarkRun | undefined } +export type AssembledLine = { line: string; openingLinkAsDirective?: true; unspellableRuns: MarkRun[] } type ScanLine = { position: LinePosition; start: number; text: string } @@ -91,7 +91,7 @@ function resolveEmphasis(segments: readonly InlineSegment[]): InlineSegment[] { function escape(segments: readonly InlineSegment[], container: LineContainer, claims: Claims): AssembledLine { const { output, placed, reading } = readBack(segments, container, claims) - return { ...guardLinkDefinition(output, placed, container), unspellableRun: unspellableRun(placed, reading) } + return { ...guardLinkDefinition(output, placed, container), unspellableRuns: unspellableRuns(placed, reading, output) } } function readBack(segments: readonly InlineSegment[], container: LineContainer, claims: Claims): ReadBack { @@ -267,21 +267,25 @@ function claimsCharacter({ container, escapings, headingCloser, inline: { text: } } -// Of the runs the line does not read back as written, the first emphasis run to close, else the first highlight run: one misread pair can re-pair the delimiters after it. -function unspellableRun(placed: readonly PlacedSegment[], reading: InlineReading): MarkRun | undefined { +// Of the runs the line does not read back as written, the first emphasis run to close, else every highlight run: one misread emphasis pair can re-pair the delimiters after it. +function unspellableRuns(placed: readonly PlacedSegment[], reading: InlineReading, output: string): MarkRun[] { + const misflanked: MarkRun[] = [] const openers: { nodes: MarkRun; start: number }[] = [] - let highlight: { nodes: MarkRun; start: number } | undefined - let unreadHighlight: MarkRun | undefined for (const { segment, start } of placed) { if (segment.emphasis === 'open') openers.push({ nodes: segment.nodes, start }) - if (segment.highlight === 'open') highlight = { nodes: segment.nodes, start } + // No `==` in text can open or close, and highlights never nest, so a highlight run reads back where both its delimiters flank. + if (segment.highlight !== undefined && !highlightFlanks(output, start, segment.highlight) && misflanked.at(-1) !== segment.nodes) misflanked.push(segment.nodes) if (segment.emphasis === 'close') { const opener = openers.pop() - if (opener !== undefined && !readAsWritten(reading, opener.start, start, segment.text.length)) return opener.nodes + if (opener !== undefined && !readAsWritten(reading, opener.start, start, segment.text.length)) return [opener.nodes] } - if (segment.highlight === 'close' && highlight !== undefined && reading.highlights.get(highlight.start) !== start) unreadHighlight ??= highlight.nodes } - return unreadHighlight + return misflanked +} + +function highlightFlanks(output: string, start: number, role: DelimiterRole): boolean { + const flanking = highlightFlanking(output, start) + return role === 'open' ? flanking.opens : flanking.closes } function readAsWritten(reading: InlineReading, opener: number, closer: number, width: number): boolean { diff --git a/src/markdown/inline-content.ts b/src/markdown/inline-content.ts index 128f260..766ef80 100644 --- a/src/markdown/inline-content.ts +++ b/src/markdown/inline-content.ts @@ -50,17 +50,17 @@ type Nodes = { kind: 'nodes'; nodes: Inline[] } type Resolved = ImageLink | Nodes -type HighlightDelimiter = { closes: boolean; holder: AdfNode; index: number; offset: number; opens: boolean; position: number; stretch: number } +type HighlightDelimiter = { closes: boolean; holder: AdfNode; index: number; opens: boolean; position: number; stretch: number } -type HighlightPair = { closer: number; closerOffset: number; opener: number; openerOffset: number } +type HighlightPair = { closer: number; opener: number } // `index` is the bracket's position in `pieces`; `start` is where its text starts in `source`. type OpenBracket = { image: boolean; index: number; start: number } type Pairing = EmphasisPairing +type HighlightPiece = { closes: boolean; kind: 'highlight'; opens: boolean } // `offset` is where the delimiter starts in the outermost content. -type HighlightPiece = { closes: boolean; kind: 'highlight'; offset: number; opens: boolean } type RunPiece = { canClose: boolean; canOpen: boolean; character: string; kind: 'run'; length: number; offset: number } type Piece = Bracket | HighlightPiece | Image | ImageLink | Nodes | OwnMarks | RunPiece | TextBreak @@ -73,13 +73,11 @@ export type InlineReading = { readonly codeSpans: number[] // Each emphasis pairing, keyed by where its opening delimiters start. readonly emphasis: Map - // Each highlight pairing: its opening `==` to its closing one. - readonly highlights: Map // Where the `[` of each link or image stands. readonly links: number[] } -type ReadingEntry = { at: number; closer: number; kind: 'emphasis'; width: number } | { at: number; closer: number; kind: 'highlights' } | { at: number; kind: 'codeSpans' | 'links' } +type ReadingEntry = { at: number; closer: number; kind: 'emphasis'; width: number } | { at: number; kind: 'codeSpans' | 'links' } // `container` is `undefined` inside a directive's content slot, the emitter's `bracketed`. type Scan = { @@ -137,7 +135,7 @@ export function parseInlineContent(source: string, definitions: LinkDefinitions, // The reading of a line that faults still holds what the reader took before the fault. export function readLineBack(line: string, container: LineContainer, claims: Claims): { faulted: boolean; reading: InlineReading } { - const reading: InlineReading = { codeSpans: [], emphasis: new Map(), highlights: new Map(), links: [] } + const reading: InlineReading = { codeSpans: [], emphasis: new Map(), links: [] } const scan = freshScan(line, { definitions: noDefinitions, path: [], reading, targets: targetMemo(line) }, { claims, container, inLink: false, offset: 0, spans: noSpans }) return { faulted: !scanInline(scan).ok, reading } } @@ -145,7 +143,6 @@ export function readLineBack(line: string, container: LineContainer, claims: Cla function record({ reading }: SharedScan, entry: ReadingEntry): void { if (reading === undefined) return if (entry.kind === 'emphasis') reading.emphasis.set(entry.at, { closer: entry.closer, width: entry.width }) - else if (entry.kind === 'highlights') reading.highlights.set(entry.at, entry.closer) else reading[entry.kind].push(entry.at) } @@ -202,7 +199,7 @@ function readToken(scan: Scan, inline: InlineSource, index: number, token: Inlin return success(end) case 'highlight': flush(scan, false) - scan.pieces.push({ closes: token.closes, kind: 'highlight', offset: scan.offset + index, opens: token.opens }) + scan.pieces.push({ closes: token.closes, kind: 'highlight', opens: token.opens }) return success(end) case 'line-ending': return success(readLineEnding(scan, index)) @@ -525,7 +522,6 @@ function markedNodes(pieces: readonly Piece[], scan: Scan, highlights: boolean): writeUnpaired(nodes, runs, pairings) if (!markPairings(pieces, nodes, pairings)) return failure('unsupported-node-shape', ownMarksInMark, scan.shared.path) const paired = highlights ? pairedHighlights(pieces, nodes) : [] - for (const { closerOffset, openerOffset } of paired) record(scan.shared, { at: openerOffset, closer: closerOffset, kind: 'highlights' }) markHighlights(pieces, nodes, paired) return success(nodes) } @@ -634,7 +630,7 @@ function highlightDelimiters(pieces: readonly Piece[], nodes: readonly Inline[][ const held = adfNodes(nodes[index] ?? []) const [holder] = held if (piece.kind === 'highlight' && holder !== undefined) { - found.push({ closes: piece.closes, holder, index, offset: piece.offset, opens: piece.opens, position, stretch }) + found.push({ closes: piece.closes, holder, index, opens: piece.opens, position, stretch }) position += highlightDelimiter.length continue } @@ -662,7 +658,7 @@ function pairedHighlights(pieces: readonly Piece[], nodes: readonly Inline[][]): } if (candidate === undefined) break if (candidate.stretch !== opener.stretch || !identicalMarks(nodeMarks(opener.holder), nodeMarks(candidate.holder))) continue - paired.push({ closer: candidate.index, closerOffset: candidate.offset, opener: opener.index, openerOffset: opener.offset }) + paired.push({ closer: candidate.index, opener: opener.index }) resume = candidate.position + highlightDelimiter.length } return paired diff --git a/src/markdown/portable/adf-to-portable-markdown.test.ts b/src/markdown/portable/adf-to-portable-markdown.test.ts index 1c1a1de..844402f 100644 --- a/src/markdown/portable/adf-to-portable-markdown.test.ts +++ b/src/markdown/portable/adf-to-portable-markdown.test.ts @@ -4,6 +4,7 @@ import test from 'node:test' import type { AdfAttributes, AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts' import { adfToPortableMarkdown, reduceToPortable } from './adf-to-portable-markdown.ts' import { largestNesting } from '../../nesting.ts' +import { portableLineFallback } from '../emit/inline-line.ts' import { portableMarkdownToAdf } from './portable-markdown-to-adf.ts' const code: AdfMark = { type: 'code' } @@ -393,6 +394,13 @@ test('drops the mark of the run that cannot close where it sits, and keeps the l assert.equal(portable(paragraph(text('~', highlight), text('b'), text('日本', highlight))), '~b==日本==\n') }) +test('gives up every highlight run whose own delimiter cannot flank in one fallback, so a line of them re-emits once however many it holds', () => { + const highlight: AdfMark = { type: 'backgroundColor' } + const line = [text('a '), text('x', highlight), text('a '), text('x', highlight), text('a '), text('x', highlight), text('a')] + const runs = [1, 3, 5].map((index) => ({ depth: 0, first: index, last: index })) + assert.deepEqual(portableLineFallback(line, 'paragraph', []), { ok: true, value: { kind: 'unspellable-run', runs } }) +}) + 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') diff --git a/src/markdown/portable/inline-reduction.ts b/src/markdown/portable/inline-reduction.ts index 4a35548..0c73a70 100644 --- a/src/markdown/portable/inline-reduction.ts +++ b/src/markdown/portable/inline-reduction.ts @@ -230,7 +230,7 @@ function spellableLine(leaves: AdfNode[], container: LineContainer, path: Conver } function withoutFallback(leaves: readonly AdfNode[], fallback: PortableLineFallback): AdfNode[] | undefined { - if (fallback.kind === 'unspellable-run') return withoutMark(leaves, fallback.run) + if (fallback.kind === 'unspellable-run') return withoutMarks(leaves, fallback.runs) const mark = nodeMarks(leaves[0] ?? {})[0] if (mark === undefined || mark.type !== 'link') return undefined let last = 0 @@ -240,6 +240,12 @@ function withoutFallback(leaves: readonly AdfNode[], fallback: PortableLineFallb return spans ? leaves.map((leaf, index) => (index > last ? leaf : withMarks(leaf, nodeMarks(leaf).filter((held) => held.type !== 'code')))) : undefined } -function withoutMark(leaves: readonly AdfNode[], run: MarkRun): AdfNode[] { - return leaves.map((leaf, index) => (index < run.first || index > run.last ? leaf : withMarks(leaf, nodeMarks(leaf).filter((_, held) => held !== run.depth)))) +// The runs cover disjoint leaves, so one pass drops them all. +function withoutMarks(leaves: readonly AdfNode[], runs: readonly MarkRun[]): AdfNode[] { + const depths = new Map() + for (const run of runs) for (let index = run.first; index <= run.last; index += 1) depths.set(index, run.depth) + return leaves.map((leaf, index) => { + const depth = depths.get(index) + return depth === undefined ? leaf : withMarks(leaf, nodeMarks(leaf).filter((_, held) => held !== depth)) + }) }