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

Drop the edge whitespace of the passes to come after each trim pass, so blanks at a hard break trim in 2 passes

This commit is contained in:
2026-11-15 19:05:13 +01:00
committed by Lillem4n
parent d6a6206568
commit e0285f5d79
7 changed files with 163 additions and 25 deletions
+1
View File
@@ -83,6 +83,7 @@ In `docs/decisions.md`:
- Readers scan by index - Readers scan by index
- The spelling memo - The spelling memo
- Cost fixes are measured, never timed - Cost fixes are measured, never timed
- After each trim pass, the reducer drops the edge whitespace the passes to come would drop, a round per pass
- Only the hard break holds a raw newline - Only the hard break holds a raw newline
- Emphasis follows CommonMark's matching - Emphasis follows CommonMark's matching
- Where a text backtick or `[` opens a code span, a link or an image, every text backtick or `[` in its line escapes - Where a text backtick or `[` opens a code span, a link or an image, every text backtick or `[` in its line escapes
+1
View File
@@ -2,6 +2,7 @@
## Unreleased ## Unreleased
- `adfToPortableMarkdown` writes a line of whitespace-only text nodes beside a hard break, alternating linked and plain or emphasized, in time linear in its length, where `0.3.1` took time quadratic in it.
- `adfToPortableMarkdown` writes a line of code spans alternating with emphasized spaces in time linear in its length, where `0.3.1` took time quadratic in it. - `adfToPortableMarkdown` writes a line of code spans alternating with emphasized spaces in time linear in its length, where `0.3.1` took time quadratic in it.
- `adfToHtml` writes a media single, a media group and a caption as `<figure class="adf-media-single">` holding an - `adfToHtml` writes a media single, a media group and a caption as `<figure class="adf-media-single">` holding an
`<img>` and a `<figcaption>`, and as `<div class="adf-media-group">` of `<span class="adf-file">` files, where `<img>` and a `<figcaption>`, and as `<div class="adf-media-group">` of `<span class="adf-file">` files, where
+20
View File
@@ -1185,6 +1185,26 @@ The shape walk of `readAdfDocument` and the depth walk of `writableContent` stay
gives depth its own code — at 52 ms for a 9 MB document the emit takes 314 ms over. The 500-level guard runs after the block walk, gives depth its own code — at 52 ms for a 9 MB document the emit takes 314 ms over. The 500-level guard runs after the block walk,
so it doesn't bound the walk's cost. so it doesn't bound the walk's cost.
## After each trim pass, the reducer drops the edge whitespace the passes to come would drop, a round per pass
2026-11-15, item 190's chunk. Goal 9. Valid while `link` is the only kept mark that is not
edge-stripping, and every hard break the reducer hands the trim is alike.
`trimPass` judges every leaf against one snapshot, so a blank leaf reaches a line edge only the
pass after its neighbour dropped there: n blanks at a hard break took n + 1 passes, each linear.
`withoutEdgeWhitespace` replays those drops in rounds, one per pass to come, and a round judges only
the leaves beside a leaf the round before dropped, so all rounds together are linear. A round is
exact because whitespace at a line edge goes or stays by three facts no pass changes: its leaf's
outermost mark, the outermost mark of the leaf across the hard break, and whether a hard break lies
between. Three things keep it so, each found by the property test against the one-leaf-per-pass
reference: a round judges all its leaves before it changes any, since a leaf goes by a neighbour
that goes in the same pass; a round also judges the edge whitespace of a leaf holding text; and the
replay merges as `trimPass` does, since merged leaves reach an edge as one. The replay runs after a
pass, never before the first: a pass also takes marks off whitespace by the line as it stands, and
the first pass must see the line as it came. Measured in passes
(§Cost fixes are measured, never timed): both of item 190's lines took 7, 101 and 1 001 passes at
6, 100 and 1 000 blanks, and take 2 at each, as at 100 000.
## Only the hard break holds a raw newline ## Only the hard break holds a raw newline
2026-08-26, the maintainer. Goal 1. Valid while the whitespace carry finds a line edge by its raw 2026-08-26, the maintainer. Goal 1. Valid while the whitespace carry finds a line edge by its raw
+17
View File
@@ -1197,3 +1197,20 @@ Read most (opens and searches):
- `converter-comparison/run.sh` 2 - `converter-comparison/run.sh` 2
- `.github/workflows/ci.yml` 1 - `.github/workflows/ci.yml` 1
- `.gitignore` 1 - `.gitignore` 1
## Reading cost, chunk 3f3fce26685e, finished 2026-11-15T13:55:18Z
104756 new input tokens before the first commit.
Read most (opens and searches):
- `src/markdown/portable/inline-reduction.ts` 5
- `src/conformance/property-harness.ts` 4
- `src/markdown/emit/inline-runs.ts` 4
- `src/markdown/portable/inline-reduction.test.ts` 4
- `todo.md` 3
- `README.md` 2
- `CHANGELOG.md` 1
- `ci.sh` 1
- `docs/decisions.md` 1
- `package.json` 1
+28 -10
View File
@@ -8,7 +8,7 @@ import type { PortableLineFallback } from '../emit/inline-line.ts'
import type { RunGrouping, RunNode } from '../emit/inline-runs.ts' import type { RunGrouping, RunNode } from '../emit/inline-runs.ts'
import { inlineRuns } from '../emit/inline-runs.ts' import { inlineRuns } from '../emit/inline-runs.ts'
import { lineContainer, markedLine, propertyRuns } from '../../conformance/property-harness.ts' import { lineContainer, markedLine, propertyRuns } from '../../conformance/property-harness.ts'
import { portableJudgement, portableLeaves, reduceInline, trimmedEdges, trimmingRuns, trimPass, withoutFallback } from './inline-reduction.ts' import { portableJudgement, portableLeaves, reduceInline, trimmedEdges, trimmedEdgesPass, trimmingRuns, trimPass, withoutFallback } from './inline-reduction.ts'
import { success, type Result } from '../../result.ts' import { success, type Result } from '../../result.ts'
test('reduces a line alike dropping every run a pass may or the first alone', () => { test('reduces a line alike dropping every run a pass may or the first alone', () => {
@@ -68,7 +68,7 @@ const trimMarks = fc.oneof(
fc.constantFrom<AdfMark[]>([{ type: 'code' }], [link, { type: 'code' }]), fc.constantFrom<AdfMark[]>([{ type: 'code' }], [link, { type: 'code' }]),
) )
const trimLeaf = fc.oneof( const trimLeaf = fc.oneof(
{ arbitrary: fc.record({ marks: trimMarks, text: fc.string({ maxLength: 3, minLength: 1, unit: fc.constantFrom(' ', ' ', '\t', 'x') }) }).map((held): AdfNode => ({ ...held, type: 'text' })), weight: 6 }, { arbitrary: fc.record({ marks: trimMarks, text: fc.string({ maxLength: 3, minLength: 1, unit: fc.constantFrom(' ', ' ', '\t', 'x') }) }).map(({ marks, text }): AdfNode => (marks.length === 0 ? { text, type: 'text' } : { marks, text, type: 'text' })), weight: 6 },
{ arbitrary: fc.constant<AdfNode>({ type: 'hardBreak' }), weight: 1 }, { arbitrary: fc.constant<AdfNode>({ type: 'hardBreak' }), weight: 1 },
) )
const trimLine = fc.oneof( const trimLine = fc.oneof(
@@ -80,8 +80,8 @@ for (const grouping of groupings) {
test(`trims a ${grouping} line as the reference counts trim it, in no more passes`, () => { test(`trims a ${grouping} line as the reference counts trim it, in no more passes`, () => {
fc.assert( fc.assert(
fc.property(trimLine, (line) => { fc.property(trimLine, (line) => {
const peeled = trimmedWith(line, (leaves) => runsSharedWithNext(leaves, grouping)) const peeled = trimmedWith(line, (leaves) => trimPass(leaves, (held) => runsSharedWithNext(held, grouping)))
const trimmed = trimmedWith(line, (leaves) => trimmingRuns(leaves, grouping)) const trimmed = trimmedWith(line, (leaves) => trimmedEdgesPass(leaves, grouping))
assert.deepEqual(trimmedEdges(line, grouping), peeled.line) assert.deepEqual(trimmedEdges(line, grouping), peeled.line)
assert.ok(trimmed.passes <= peeled.passes, `${trimmed.passes} passes against ${peeled.passes}`) assert.ok(trimmed.passes <= peeled.passes, `${trimmed.passes} passes against ${peeled.passes}`)
}), }),
@@ -123,16 +123,34 @@ const alternatingLines: readonly [string, AdfNode, AdfNode][] = [
for (const [name, code, emphasized] of alternatingLines) { for (const [name, code, emphasized] of alternatingLines) {
test(`trims a line of ${name}, 1 000 leaves long, in as many passes as its 6-leaf form`, () => { test(`trims a line of ${name}, 1 000 leaves long, in as many passes as its 6-leaf form`, () => {
const alternating = (pairs: number) => Array.from({ length: pairs }, () => [code, emphasized]).flat() const alternating = (pairs: number) => Array.from({ length: pairs }, () => [code, emphasized]).flat()
assert.equal(trimmedWith(alternating(500), (leaves) => trimmingRuns(leaves, 'spanned')).passes, trimmedWith(alternating(3), (leaves) => trimmingRuns(leaves, 'spanned')).passes) assert.equal(trimmedWith(alternating(500), spannedPass).passes, trimmedWith(alternating(3), spannedPass).passes)
}) })
} }
// Runs `trimPass` on `shared`'s counts until it settles. const blankLines: readonly [string, (blanks: number) => AdfNode[]][] = [
function trimmedWith(leaves: readonly AdfNode[], shared: (leaves: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; passes: number } { ['blanks alternating linked and unmarked, then a hard break and a linked leaf', (blanks) => [...alternatingBlanks(blanks, marked(' ', link), marked(' ')), { type: 'hardBreak' }, marked('x', link)]],
['an emphasized leaf and a hard break, then blanks alternating emphasized and linked, then an emphasized leaf', (blanks) => [marked('a', { type: 'em' }), { type: 'hardBreak' }, ...alternatingBlanks(blanks, marked(' ', { type: 'em' }), marked(' ', { type: 'em' }, link)), marked('b', { type: 'em' })]],
]
for (const [name, line] of blankLines) {
test(`trims ${name}, 1 000 blanks long, in as many passes as its 6-blank form`, () => {
assert.equal(trimmedWith(line(1000), spannedPass).passes, trimmedWith(line(6), spannedPass).passes)
})
}
function alternatingBlanks(blanks: number, first: AdfNode, second: AdfNode): AdfNode[] {
return Array.from({ length: blanks }, (_, index) => (index % 2 === 0 ? first : second))
}
function spannedPass(leaves: readonly AdfNode[]): { line: AdfNode[]; settled: boolean } {
return trimmedEdgesPass(leaves, 'spanned')
}
// Runs `pass` until it settles.
function trimmedWith(leaves: readonly AdfNode[], pass: (leaves: readonly AdfNode[]) => { line: AdfNode[]; settled: boolean }): { line: AdfNode[]; passes: number } {
for (let current = leaves, passes = 1; ; passes += 1) { for (let current = leaves, passes = 1; ; passes += 1) {
const pass = trimPass(current, shared) const step = pass(current)
if (pass.settled) return { line: pass.line, passes } if (step.settled) return { line: step.line, passes }
current = pass.line current = step.line
} }
} }
+96 -1
View File
@@ -21,6 +21,9 @@ type Beside = { lineEdge: boolean; sharedRuns: number }
// `keep`: how many of the leaf's marks its edge whitespace keeps; `drop`: the whitespace goes, 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 EdgeOutcome = { kind: 'drop' } | { depth: number; kind: 'keep' }
// A leaf but a hard break in `withoutEdgeWhitespace`'s list, with the hard break between it and the leaf before it, if any.
type LinkedLeaf = { breakBefore: AdfNode | undefined; dropped: boolean; leaf: AdfNode; next: number; previous: number }
// Where a reduction stands: its path, counted in the caller's document by `sources`, and its level as the emitter counts it. // Where a reduction stands: its path, counted in the caller's document by `sources`, and its level as the emitter counts it.
export type ReductionPlace = { depth: number; path: ConvertErrorPath; sources: SourceIndexes } export type ReductionPlace = { depth: number; path: ConvertErrorPath; sources: SourceIndexes }
@@ -185,12 +188,104 @@ function withMarks(leaf: AdfNode, marks: readonly AdfMark[]): AdfNode {
export function trimmedEdges(leaves: readonly AdfNode[], grouping: RunGrouping): AdfNode[] { export function trimmedEdges(leaves: readonly AdfNode[], grouping: RunGrouping): AdfNode[] {
// Terminates: a pass that changes a leaf drops whitespace from the leaf's edge or takes a mark off that whitespace, and merging adds no whitespace or mark. // Terminates: a pass that changes a leaf drops whitespace from the leaf's edge or takes a mark off that whitespace, and merging adds no whitespace or mark.
for (let current = leaves; ; ) { for (let current = leaves; ; ) {
const pass = trimPass(current, (line) => trimmingRuns(line, grouping)) const pass = trimmedEdgesPass(current, grouping)
if (pass.settled) return pass.line if (pass.settled) return pass.line
current = pass.line current = pass.line
} }
} }
// The replay follows the pass, never leads it: the first pass takes marks off whitespace by the line as it came.
export function trimmedEdgesPass(leaves: readonly AdfNode[], grouping: RunGrouping): { line: AdfNode[]; settled: boolean } {
const pass = trimPass(leaves, (line) => trimmingRuns(line, grouping))
return pass.settled ? pass : { line: withoutEdgeWhitespace(mergeAdjacentText(pass.line, joinsWhenEditorNormal), grouping), settled: false }
}
// Drops the whitespace `trimPass` drops at line edges, a round per pass, each round judging only the leaves beside a leaf the round before dropped.
function withoutEdgeWhitespace(leaves: readonly AdfNode[], grouping: RunGrouping): AdfNode[] {
const linked: LinkedLeaf[] = []
let gap: AdfNode | undefined
for (const leaf of leaves) {
if (leaf.type === 'hardBreak') gap = leaf
else {
linked.push({ breakBefore: gap, dropped: false, leaf, next: linked.length + 1, previous: linked.length - 1 })
gap = undefined
}
}
// Terminates: a round judges only leaves beside a leaf the round before dropped, and each leaf drops once.
for (let judged = new Set(linked.keys()); judged.size > 0; ) {
// Every leaf of a round is judged before any changes, as `trimPass` judges against one snapshot.
const outcomes = [...judged].flatMap((position) => {
const node = linked[position]
const leaf = node === undefined || node.dropped ? undefined : withoutLineEdges(node, linked, grouping)
return node === undefined || node.dropped || leaf === node.leaf ? [] : [{ leaf, node }]
})
judged = new Set()
for (const { leaf, node } of outcomes) {
if (leaf !== undefined) {
node.leaf = leaf
continue
}
unlink(node, linked)
judged.add(node.previous).add(node.next)
}
// `trimPass` merges before it judges, so leaves a drop brings together reach an edge as one.
for (const position of judged) {
const node = linked[position]
if (node === undefined || node.dropped) continue
for (let next = linked[node.next]; next !== undefined && next.breakBefore === undefined; next = linked[node.next]) {
const [merged, unmerged] = mergeAdjacentText([node.leaf, next.leaf], joinsWhenEditorNormal)
if (merged === undefined || unmerged !== undefined) break
node.leaf = merged
unlink(next, linked)
}
}
}
const kept: AdfNode[] = []
let position = 0
for (const leaf of leaves) {
if (leaf.type === 'hardBreak') {
kept.push(leaf)
continue
}
const node = linked[position]
position += 1
if (node !== undefined && !node.dropped) kept.push(node.leaf)
}
return kept
}
function unlink(node: LinkedLeaf, linked: readonly LinkedLeaf[]): void {
node.dropped = true
const previous = linked[node.previous]
const next = linked[node.next]
if (previous !== undefined) previous.next = node.next
if (next !== undefined) {
next.previous = node.previous
next.breakBefore ??= node.breakBefore
}
}
// The leaf without the whitespace that goes at a line edge, itself where none does, and `undefined` where all of it does.
function withoutLineEdges(node: LinkedLeaf, linked: readonly LinkedLeaf[], grouping: RunGrouping): AdfNode | undefined {
const { breakBefore, leaf } = node
const marks = nodeMarks(leaf)
const text = leaf.text
if (text === undefined || marks.some((mark) => mark.type === 'code')) return leaf
const previous = linked[node.previous]
const next = linked[node.next]
const lead = text.slice(0, text.search(/[^ \t]|$/))
const trail = text.slice(text.search(/[ \t]*$/))
const leadGoes = (previous === undefined || breakBefore !== undefined) && edgeOutcome(marks, { lineEdge: true, sharedRuns: runsAcross(previous?.leaf, breakBefore, leaf, grouping) }, lead).kind === 'drop'
const trailGoes = (next === undefined || next.breakBefore !== undefined) && edgeOutcome(marks, { lineEdge: true, sharedRuns: runsAcross(leaf, next?.breakBefore, next?.leaf, grouping) }, trail).kind === 'drop'
if (!leadGoes && !trailGoes) return leaf
return lead === text ? undefined : textLeaf(text.slice(leadGoes ? lead.length : 0, text.length - (trailGoes ? trail.length : 0)), marks)
}
// The reducer's hard breaks are all alike, so one stands for every hard break between two leaves.
function runsAcross(left: AdfNode | undefined, hardBreak: AdfNode | undefined, right: AdfNode | undefined, grouping: RunGrouping): number {
return left === undefined || hardBreak === undefined || right === undefined ? 0 : (trimmingRuns([left, hardBreak, right], grouping)[0] ?? 0)
}
// Judges every leaf against one snapshot of `shared`'s counts. // Judges every leaf against one snapshot of `shared`'s counts.
export function trimPass(leaves: readonly AdfNode[], shared: (line: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; settled: boolean } { export function trimPass(leaves: readonly AdfNode[], shared: (line: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; settled: boolean } {
const merged = withoutEdgeBreaks(mergeAdjacentText(leaves, joinsWhenEditorNormal)) const merged = withoutEdgeBreaks(mergeAdjacentText(leaves, joinsWhenEditorNormal))
-14
View File
@@ -12,7 +12,6 @@
| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | | ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |
|---|---|---|---|---|---|---|---|---|---|---|---| |---|---|---|---|---|---|---|---|---|---|---|---|
| 190 | 0.4.0 | | defect | **Bound the passes `trimmedEdges` takes by a constant, so blank leaves dropping one by one at a hard break trim in linear time.** | 3 | 1 | 2 | 4 | 7 | 9 by 3 | 1.9 |
| 51 | 0.4.0 | | | **Match a reference label to its definition under Unicode case folding.** | 2 | 1 | 2 | 3 | 4 | 4 by 2 | 1.7 | | 51 | 0.4.0 | | | **Match a reference label to its definition under Unicode case folding.** | 2 | 1 | 2 | 3 | 4 | 4 by 2 | 1.7 |
| 148 | 0.4.0 | | | **Ship conversion between ADF and Jira wiki markup both ways, under a README goal for wiki markup.** | 3 | 2 | 2 | 5 | 7 | 3 by 5 | 0.8 | | 148 | 0.4.0 | | | **Ship conversion between ADF and Jira wiki markup both ways, under a README goal for wiki markup.** | 3 | 2 | 2 | 5 | 7 | 3 by 5 | 0.8 |
| 149 | 0.4.0 | | | **Read any HTML into ADF, in a call or option a developer panel picks, under a README goal for foreign HTML.** | 3 | 2 | 2 | 5 | 7 | 3 by 5 | 0.8 | | 149 | 0.4.0 | | | **Read any HTML into ADF, in a call or option a developer panel picks, under a README goal for foreign HTML.** | 3 | 2 | 2 | 5 | 7 | 3 by 5 | 0.8 |
@@ -45,19 +44,6 @@
## Details ## Details
### 190. Bound the passes `trimmedEdges` takes by a constant, so blank leaves dropping one by one at a hard break trim in linear time.
Each pass of `trimmedEdges` (`src/markdown/portable/inline-reduction.ts`) drops a blank leaf at a line
edge, and only then is its neighbour at the edge. Blank leaves alternating linked and unmarked before
a hard break, then a linked leaf, take n + 1 passes: 101, 1 001 and 10 001 at n = 100, 1 000 and
10 000, so the trim is quadratic, against goal 9. An unmarked blank drops at the edge; a linked blank
drops once the link run crosses the hard break to the linked leaf, which the unmarked blank between
breaks until it drops. Any run crossing a hard break cascades the same way, one blank per pass, an em
run too: `'a'` em, a hard break, blanks alternating em and `[em, link]`, then `'b'` em take 7, 101 and
1 001 passes at n = 6, 100 and 1 000. Item 174's runs, counted between their non-blank holders, leave
these blanks inside. Count passes to measure, never time (§Cost fixes are measured, never timed).
Found by item 174's chunk, 2026-11-15.
### 51. Match a reference label to its definition under Unicode case folding. ### 51. Match a reference label to its definition under Unicode case folding.
`link-syntax.ts` normalizes a label with `toLowerCase`, so `[ẞ]` misses its `[SS]` definition (spec `link-syntax.ts` normalizes a label with `toLowerCase`, so `[ẞ]` misses its `[SS]` definition (spec