mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer prose round 1: 0 🔴, 14 🟡 (0 misread); correct comments that misstated the code, fix item 174's figures and tagline, retitle item 173
This commit is contained in:
@@ -35,7 +35,7 @@ export type PlacedBlocks = { blocks: readonly PlacedBlock[]; headroom: number }
|
||||
// CommonMark starts a new list where the marker changes, so every second list in a run of one kind takes `alternateText`.
|
||||
type SpelledBlock = { spelling: 'commonmark' | 'directive'; text: BlockText } | { alternateText: BlockText; interruptsParagraph: boolean; ordered: boolean; spelling: 'list'; text: BlockText }
|
||||
type Spell = () => Result<EmittedBlock> | undefined
|
||||
// ask: spell the node, keeping the spelling unless refused; take: spell the node, reading and dropping what is kept.
|
||||
// take: read and drop the node's kept spelling, else spell it; ask: as take, keeping a fresh spelling unless refused.
|
||||
export type SpellingMemo = Readonly<Record<'ask' | 'take', (node: AdfNode, depth: number, spell: Spell) => Result<EmittedBlock> | undefined>>
|
||||
type WalkedItem = { node: AdfNode; walk: PlacedBlocks }
|
||||
export type WrittenAs = { flavour: 'lossless' } | { blocks: FlavourBlockWriters; flavour: 'portable' }
|
||||
|
||||
@@ -30,7 +30,7 @@ type Emission = { carries: [NodeRange, ...NodeRange[]]; segments?: undefined } |
|
||||
|
||||
type Fallback = [NodeRange, ...NodeRange[]] | 'opening-link'
|
||||
|
||||
// How a line is judged and what its fallbacks do: the lossless writer carries nodes, the portable reduction drops marks.
|
||||
// How a line is judged and what its fallbacks do: the writer carries nodes, the portable reduction drops marks.
|
||||
export type FallbackAction<Line, LineFallback, Written> = {
|
||||
giveUpEmphasis: (line: Line) => Line
|
||||
judge: (line: Line, grouping: RunGrouping) => Result<LineJudgement<Line, LineFallback, Written>>
|
||||
@@ -118,7 +118,7 @@ function emitLine(nodes: readonly AdfNode[], container: LineContainer, path: Con
|
||||
|
||||
// spec/flavour.md, Marks: the spanned line where it reads back, else the parted line, taking one fallback a pass.
|
||||
export function settledLine<Line, LineFallback, Written>(line: Line, action: FallbackAction<Line, LineFallback, Written>): Result<Written> {
|
||||
// Terminates because `take` refuses a pass that changes nothing.
|
||||
// Terminates while each `take` refuses or moves one way: the writer carries more nodes, the reduction keeps fewer marks.
|
||||
for (let current = line, pass = 1; ; pass += 1) {
|
||||
let parting = current
|
||||
if (pass === 1 || action.spansEachPass) {
|
||||
|
||||
@@ -26,7 +26,7 @@ type PairSweep = {
|
||||
// How another pair stands when the judged one falls: `moves` where a carry or a merge may move its delimiters, `unsettled` where it may stand or have fallen.
|
||||
type Standing = 'fallen' | 'moves' | 'stands' | 'unsettled'
|
||||
|
||||
// `merges` where a drop may merge runs at the pair's ends, so its delimiters may move; `falls` where it goes back with the first misread pair, itself included unless it merges; `kept` otherwise.
|
||||
// `merges` where a drop may merge runs at the pair's ends, so its delimiters may move; `falls` where it goes back with the first misread pair, as the first pair does unless it merges; `kept` otherwise.
|
||||
type Verdict = 'falls' | 'kept' | 'merges'
|
||||
|
||||
// The runs this pass takes back: the first emphasis run the line misreads and every run that falls with it, else every misflanked highlight run.
|
||||
@@ -109,7 +109,6 @@ function endsAlone(edge: AdfNode | undefined, beside: AdfNode | undefined, mark:
|
||||
return marks.slice(marks.findIndex((held) => identicalMark(held, mark)) + 1).every((held) => !besideTypes.has(held.type))
|
||||
}
|
||||
|
||||
// A pair falls with the first where it falls cleanly, and cannot read whichever character it is written with.
|
||||
function fallsWithFirst(sweep: PairSweep, judged: number): boolean {
|
||||
if (!fallsCleanly(sweep, judged)) return false
|
||||
const spellings = pairSpellings(sweep, judged, judged)
|
||||
|
||||
@@ -40,7 +40,7 @@ export function editorNormalForm(document: AdfDocument): { normal: AdfDocument;
|
||||
return { normal, sources }
|
||||
}
|
||||
|
||||
// A node's index in a list of siblings, or in a list of one, counted in the document the siblings were normalized from.
|
||||
// The node's offset from the list's first node, counted in the document they were normalized from; `index` where either is not in `sources`.
|
||||
export function sourceIndex(nodes: readonly AdfNode[], index: number, sources: SourceIndexes): number {
|
||||
const first = nodes[0]
|
||||
const node = nodes[index]
|
||||
|
||||
@@ -17,7 +17,7 @@ import { spellDestination } from '../commonmark/link-syntax.ts'
|
||||
// What stands beside a leaf's edge: whether a line edge does, and how many runs hold the leaf and its neighbour both.
|
||||
type Beside = { lineEdge: boolean; sharedRuns: number }
|
||||
|
||||
// How many of the leaf's marks edge whitespace keeps, or that it drops at a line edge.
|
||||
// `keep`: how many of the leaf's marks its edge whitespace keeps; `drop`: the whitespace goes, at a line edge.
|
||||
type EdgeOutcome = { kind: 'drop' } | { depth: number; kind: 'keep' }
|
||||
|
||||
type PlacedLeaf = { after: Beside; before: Beside; leaf: AdfNode }
|
||||
@@ -34,7 +34,7 @@ export function reduceInline(nodes: readonly AdfNode[], container: LineContainer
|
||||
const leaves = portableLeaves(nodes, container, place)
|
||||
if (!leaves.ok) return leaves
|
||||
const { path } = place
|
||||
// Ends: withoutFallback refuses a pass that drops no mark, and trimmedEdges adds none.
|
||||
// Terminates: withoutFallback refuses a pass that drops no mark, and trimmedEdges adds none.
|
||||
return settledLine<readonly AdfNode[], PortableLineFallback, AdfNode[]>(leaves.value, {
|
||||
giveUpEmphasis: withoutEmphasis,
|
||||
judge: (line, grouping) => portableJudgement(line, grouping, container, path),
|
||||
@@ -43,7 +43,7 @@ export function reduceInline(nodes: readonly AdfNode[], container: LineContainer
|
||||
})
|
||||
}
|
||||
|
||||
// The leaves before the spanned trim `portableJudgement` gives them.
|
||||
// Trimmed once for `highlightsInsideSharedMarks`; `portableJudgement` trims each grouping's line again.
|
||||
export function portableLeaves(nodes: readonly AdfNode[], container: LineContainer, place: ReductionPlace): Result<AdfNode[]> {
|
||||
const leaves = inlineLeaves(nodes, container, place)
|
||||
if (!leaves.ok) return leaves
|
||||
@@ -248,7 +248,7 @@ function edgeOutcome(marks: readonly AdfMark[], { lineEdge, sharedRuns }: Beside
|
||||
return depth === 0 && lineEdge ? { kind: 'drop' } : { depth, kind: 'keep' }
|
||||
}
|
||||
|
||||
// Each grouping's line trimmed by its own runs, which a fallback's indexes name.
|
||||
// Trims the line by `grouping`'s runs and judges it; a fallback's indexes name the trimmed line.
|
||||
export function portableJudgement(leaves: readonly AdfNode[], grouping: RunGrouping, container: LineContainer, path: ConvertErrorPath): Result<LineJudgement<readonly AdfNode[], PortableLineFallback, AdfNode[]>> {
|
||||
const line = trimmedEdges(leaves, grouping)
|
||||
const fallback = portableLineFallback(line, container, path, grouping)
|
||||
|
||||
Reference in New Issue
Block a user