mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer stability round 2: 1 🔴, 0 🟡 (0 misread); a line gives up every highlight run whose own delimiters cannot flank in one pass, as shipped, so a line of them re-emits once, and the reader records no highlight pairs
This commit is contained in:
@@ -41,7 +41,7 @@ type LineAttempt = { fallback: NodeRange | 'opening-link'; line?: undefined } |
|
||||
|
||||
type LineFallbacks = { carried: Set<number>; 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<string> {
|
||||
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="…"}.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<Run>
|
||||
|
||||
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<number, { closer: number; width: number }>
|
||||
// Each highlight pairing: its opening `==` to its closing one.
|
||||
readonly highlights: Map<number, number>
|
||||
// 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
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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<number, number>()
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user