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

Count a stripping run only between its non-blank leaves, so the trim takes two passes over alternating code and emphasized blanks

This commit is contained in:
2026-11-15 14:17:20 +01:00
committed by Lillem4n
parent 14b5680304
commit 08abdd117f
7 changed files with 158 additions and 81 deletions
+1
View File
@@ -2,6 +2,7 @@
## Unreleased
- `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
`<img>` and a `<figcaption>`, and as `<div class="adf-media-group">` of `<span class="adf-file">` files, where
`0.3.1` wrote the carry element, and `htmlToAdf` reads them back. A link around the image is the media's link
+17
View File
@@ -1180,3 +1180,20 @@ Read most (opens and searches):
- `docs/reading-cost.md` 1
- `package.json` 1
- `todo.md` 1
## Reading cost, chunk 2dc66dc1c2fd, finished 2026-11-15T13:02:04Z
81039 new input tokens before the first commit.
Read most (opens and searches):
- `README.md` 11
- `docs/decisions.md` 8
- `ci.sh` 4
- `converter-comparison/compare.mjs` 4
- `todo.md` 4
- `AGENTS.md` 3
- `publish.sh` 3
- `converter-comparison/run.sh` 2
- `.github/workflows/ci.yml` 1
- `.gitignore` 1
-20
View File
@@ -1,20 +0,0 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import type { AdfMark, AdfNode } from '../../adf/document.ts'
import { runsSharedWithNext } from './inline-runs.ts'
const em: AdfMark = { type: 'em' }
const strong: AdfMark = { type: 'strong' }
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
test('counts a run crossing a code span to its next holder where the line is spanned, and none where it is parted', () => {
const line = [marked('Run ', strong), marked('npm ci', { type: 'code' }), marked(' first', strong)]
assert.deepEqual(runsSharedWithNext(line, 'spanned'), [1, 1, 0])
assert.deepEqual(runsSharedWithNext(line, 'parted'), [0, 0, 0])
})
test('counts every nested run crossing a hard break, and none crossing a hard break holding an empty marks', () => {
assert.deepEqual(runsSharedWithNext([marked('a', em, strong), { type: 'hardBreak' }, marked('b', em, strong)], 'spanned'), [2, 2, 0])
assert.deepEqual(runsSharedWithNext([marked('a', em), { marks: [], type: 'hardBreak' }, marked('b', em)], 'spanned'), [0, 0, 0])
})
-16
View File
@@ -55,19 +55,3 @@ function runCrosses(node: AdfNode, mark: AdfMark): boolean {
const kind = markSpelling(mark.type)?.kind
return (kind === 'emphasis' || kind === 'link') && !takesMark(node, mark.type) && node.marks?.length !== 0
}
// For each leaf but the last, how many runs hold both it and the next leaf, grouped as the writers group them, so the reducer trims edge whitespace by runs they spell.
export function runsSharedWithNext(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
const shared = leaves.map(() => 0)
countSharedRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
return shared
}
// Recurses once per mark of a leaf, and the reducer's leaves hold one mark of each kept type.
function countSharedRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
if (run.kind === 'plain') continue
for (let index = run.index; index < run.index + run.nodes.length - 1; index += 1) shared[index] = (shared[index] ?? 0) + 1
countSharedRuns(run.nodes, run.index, grouping, shared)
}
}
+86 -2
View File
@@ -2,11 +2,13 @@ import assert from 'node:assert/strict'
import fc from 'fast-check'
import test from 'node:test'
import type { AdfNode } from '../../adf/document.ts'
import type { AdfMark, AdfNode } from '../../adf/document.ts'
import type { LineContainer } from '../line-container.ts'
import type { PortableLineFallback } from '../emit/inline-line.ts'
import type { RunGrouping, RunNode } from '../emit/inline-runs.ts'
import { inlineRuns } from '../emit/inline-runs.ts'
import { lineContainer, markedLine, propertyRuns } from '../../conformance/property-harness.ts'
import { portableJudgement, portableLeaves, reduceInline, withoutFallback } from './inline-reduction.ts'
import { portableJudgement, portableLeaves, reduceInline, trimmedEdges, trimmingRuns, trimPass, withoutFallback } from './inline-reduction.ts'
import { success, type Result } from '../../result.ts'
test('reduces a line alike dropping every run a pass may or the first alone', () => {
@@ -58,6 +60,88 @@ test('refuses an unspellable-run fallback that takes no mark', () => {
assert.equal(refused.ok ? 'kept' : refused.error.code, 'unsupported-node-shape')
})
const groupings: readonly RunGrouping[] = ['parted', 'spanned']
const link: AdfMark = { attrs: { href: 'u' }, type: 'link' }
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
const trimMarks = fc.oneof(
fc.uniqueArray(fc.constantFrom<AdfMark>({ type: 'backgroundColor' }, { type: 'em' }, link, { attrs: { href: 'v' }, type: 'link' }, { type: 'strike' }, { type: 'strong' }), { maxLength: 3, selector: (held) => held.type }),
fc.constantFrom<AdfMark[]>([{ type: 'code' }], [link, { type: 'code' }]),
)
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.constant<AdfNode>({ type: 'hardBreak' }), weight: 1 },
)
const trimLine = fc.oneof(
fc.array(trimLeaf, { maxLength: 12, minLength: 1 }),
fc.tuple(fc.array(trimLeaf, { maxLength: 2 }), fc.array(trimLeaf, { maxLength: 3, minLength: 1 }), fc.integer({ max: 8, min: 2 }), fc.array(trimLeaf, { maxLength: 2 })).map(([head, unit, repeats, tail]) => [...head, ...Array.from({ length: repeats }, () => unit).flat(), ...tail]),
)
for (const grouping of groupings) {
test(`trims a line ${grouping} as peeling each run's end leaf by leaf does, in no more passes`, () => {
fc.assert(
fc.property(trimLine, (line) => {
const peeled = trimmedWith(line, (leaves) => runsSharedWithNext(leaves, grouping))
const trimmed = trimmedWith(line, (leaves) => trimmingRuns(leaves, grouping))
assert.deepEqual(trimmedEdges(line, grouping), peeled.line)
assert.ok(trimmed.passes <= peeled.passes, `${trimmed.passes} passes against ${peeled.passes}`)
}),
propertyRuns(3000),
)
})
}
test('counts a run crossing a code span to its next holder where the line is spanned, and none where it is parted', () => {
const line = [marked('Run ', { type: 'strong' }), marked('npm ci', { type: 'code' }), marked(' first', { type: 'strong' })]
assert.deepEqual(trimmingRuns(line, 'spanned'), [1, 1, 0])
assert.deepEqual(trimmingRuns(line, 'parted'), [0, 0, 0])
})
test('counts every nested run crossing a hard break, and none crossing a hard break holding an empty marks', () => {
assert.deepEqual(trimmingRuns([marked('a', { type: 'em' }, { type: 'strong' }), { type: 'hardBreak' }, marked('b', { type: 'em' }, { type: 'strong' })], 'spanned'), [2, 2, 0])
assert.deepEqual(trimmingRuns([marked('a', { type: 'em' }), { marks: [], type: 'hardBreak' }, marked('b', { type: 'em' })], 'spanned'), [0, 0, 0])
})
test('counts an emphasis run from its first to its last leaf holding more than spaces and tabs, and a link run to its last leaf', () => {
const line = [marked(' \t', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked('a', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked('b', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked(' ', link, { type: 'em' })]
assert.deepEqual(trimmingRuns(line, 'spanned'), [1, 1, 2, 2, 1, 1, 0])
})
const alternatingLines: readonly [string, AdfNode, AdfNode][] = [
['a code space beside an emphasized space', marked(' ', { type: 'code' }), marked(' ', { type: 'em' })],
['code x beside an emphasized space', marked('x', { type: 'code' }), marked(' ', { type: 'em' })],
['linked code x beside a linked emphasized space', marked('x', link, { type: 'code' }), marked(' ', link, { type: 'em' })],
]
for (const [name, code, emphasized] of alternatingLines) {
test(`trims a line of ${name}, 1 000 leaves long, in the passes its 6-leaf form takes`, () => {
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)
})
}
// The trim's passes and line where each pass judges every leaf against `shared`'s counts.
function trimmedWith(leaves: readonly AdfNode[], shared: (leaves: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; passes: number } {
for (let current = leaves, passes = 1; ; passes += 1) {
const pass = trimPass(current, shared)
if (pass.settled) return { line: pass.line, passes }
current = pass.line
}
}
// The reference counts: every run reaches its last leaf holding its mark, so the trim peels a run's end leaves one pass each.
function runsSharedWithNext(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
const shared = leaves.map(() => 0)
countSharedRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
return shared
}
function countSharedRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
if (run.kind === 'plain') continue
for (let index = run.index; index < run.index + run.nodes.length - 1; index += 1) shared[index] = (shared[index] ?? 0) + 1
countSharedRuns(run.nodes, run.index, grouping, shared)
}
}
// Judges each pass spanned, else parted, dropping only the first run it finds, with every misflanked highlight, and takes no cap.
function firstRunLine(nodes: readonly AdfNode[], container: LineContainer): Result<AdfNode[]> {
const leaves = portableLeaves(nodes, container, { depth: 0, path: [], sources: new Map() })
+43 -22
View File
@@ -7,7 +7,7 @@ import { codeTextMarkTypes } from '../../adf/inline-nodes.ts'
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { identicalMark, mergeAdjacentText, nodeAttrs, nodeContent, nodeMarks } from '../../adf/document.ts'
import { imageAside } from '../image-aside.ts'
import { insideRun, runsSharedWithNext } from '../emit/inline-runs.ts'
import { inlineRuns, insideRun, type RunNode } from '../emit/inline-runs.ts'
import { isEmphasis } from '../mark-spellings.ts'
import { joinsWhenEditorNormal, sourceIndex, type SourceIndexes } from './editor-normal.ts'
import { largestNesting } from '../../nesting.ts'
@@ -21,8 +21,6 @@ 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.
type EdgeOutcome = { kind: 'drop' } | { depth: number; kind: 'keep' }
type PlacedLeaf = { after: Beside; before: Beside; leaf: AdfNode }
// 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 }
@@ -187,23 +185,30 @@ function withMarks(leaf: AdfNode, marks: readonly AdfMark[]): 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.
for (let current = leaves; ; ) {
const merged = withoutEdgeBreaks(mergeAdjacentText(current, joinsWhenEditorNormal))
let changed = false
const trimmed: AdfNode[] = []
for (const { after, before, leaf } of placedLeaves(merged, grouping)) {
const edges = leafEdges(leaf, before, after)
if (edges === undefined) {
trimmed.push(leaf)
continue
}
changed = true
for (const edge of edges) trimmed.push(edge)
}
if (!changed) return merged
current = trimmed
const pass = trimPass(current, (line) => trimmingRuns(line, grouping))
if (pass.settled) return pass.line
current = pass.line
}
}
// Judges every leaf against one snapshot of `shared`'s counts; `settled`, with the merged line, where no leaf changes.
export function trimPass(leaves: readonly AdfNode[], shared: (line: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; settled: boolean } {
const merged = withoutEdgeBreaks(mergeAdjacentText(leaves, joinsWhenEditorNormal))
const counts = shared(merged)
let changed = false
const trimmed: AdfNode[] = []
for (const [index, leaf] of merged.entries()) {
const edges = leafEdges(leaf, beside(merged[index - 1], counts[index - 1]), beside(merged[index + 1], counts[index]))
if (edges === undefined) {
trimmed.push(leaf)
continue
}
changed = true
for (const edge of edges) trimmed.push(edge)
}
return changed ? { line: trimmed, settled: false } : { line: merged, settled: true }
}
function withoutEdgeBreaks(leaves: readonly AdfNode[]): AdfNode[] {
let first = 0
let last = leaves.length - 1
@@ -212,11 +217,6 @@ function withoutEdgeBreaks(leaves: readonly AdfNode[]): AdfNode[] {
return leaves.slice(first, last + 1)
}
function placedLeaves(leaves: readonly AdfNode[], grouping: RunGrouping): PlacedLeaf[] {
const shared = runsSharedWithNext(leaves, grouping)
return leaves.map((leaf, index) => ({ after: beside(leaves[index + 1], shared[index]), before: beside(leaves[index - 1], shared[index - 1]), leaf }))
}
function beside(neighbour: AdfNode | undefined, sharedRuns = 0): Beside {
return { lineEdge: neighbour === undefined || neighbour.type === 'hardBreak', sharedRuns }
}
@@ -250,6 +250,27 @@ function edgeOutcome(marks: readonly AdfMark[], { lineEdge, sharedRuns }: Beside
return depth === 0 && lineEdge ? { kind: 'drop' } : { depth, kind: 'keep' }
}
// For each leaf but the last, how many of `grouping`'s runs hold it and the next leaf; an edge-stripping mark's run spans only its leaves holding more than spaces and tabs, as the trim strips the mark off blank leaves past them, else one pass each.
export function trimmingRuns(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
const shared = leaves.map(() => 0)
countTrimmingRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
return shared
}
// Recurses once per mark of a leaf, and the reducer's leaves hold one mark of each kept type.
function countTrimmingRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
if (run.kind === 'plain') continue
const holds = edgeStrippingMarks.includes(run.mark.type) ? ({ node }: RunNode) => nodeMarks(node).some((mark) => identicalMark(mark, run.mark)) && /[^ \t]/.test(node.text ?? '') : () => true
const holders = run.nodes.flatMap((runNode, offset) => (holds(runNode) ? [offset] : []))
const first = holders[0]
const last = holders.at(-1)
if (first === undefined || last === undefined) continue
for (let index = first; index < last; index += 1) shared[run.index + index] = (shared[run.index + index] ?? 0) + 1
countTrimmingRuns(run.nodes.slice(first, last + 1), run.index + first, grouping, shared)
}
}
// 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)
+11 -21
View File
@@ -6,13 +6,13 @@
`Bar = 0.1`
`Next ID = 190`
`Next ID = 191`
## Items
| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 174 | 0.4.0 | | defect | **Bound the passes `trimmedEdges` takes by a constant, so a line of alternating code and emphasized blanks trims in linear time.** | 3 | 1 | 2 | 4 | 7 | 9 by 3 | 1.9 |
| 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 |
| 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 |
@@ -45,26 +45,16 @@
## Details
### 174. Bound the passes `trimmedEdges` takes by a constant, so a line of alternating code and emphasized blanks trims in linear time.
### 190. Bound the passes `trimmedEdges` takes by a constant, so blank leaves dropping one by one at a hard break trim in linear time.
`trimmedEdges` (`src/markdown/portable/inline-reduction.ts`) repeats a pass until nothing changes, and
each pass costs O(leaves) while leaf text is short; the trailing-blank scan inside one long leaf is
item 42's. Leaves alternating `' '` marked code with `' '` marked em take
ceil(n/4) + 1 passes: 26, 251 and 2 501 at n = 100, 1 000 and 10 000, so the trim is quadratic,
against goal 9. The same holds for `'x'` code beside `' '` em, and for `[link, code] 'x'` beside
`[link, em] ' '`. The smallest line is six leaves `' '` marked code, em, code, em, code, em, which
takes 3 passes. The passes in a random search of 20 000 short lines never went above 4.
The cause: under `spanned` grouping an em or link run crosses the code leaves, which can't hold
it, so one run spans the line and only its two end leaves stand beside a neighbour outside it. Each
pass strips em from those two, and the next pair is exposed. A single pass cannot take the fixpoint
directly: every pass judges all leaves against one snapshot, and `edgeOutcome` (drop at a line edge
while a run is shared, keep when not) isn't monotone. A hard break between `[link, em] ' '` and
`[link, em] ' b'` trims to `b` today, but to a linked space and `b` if the first leaf's drop is seen
first. Two ways forward: make the edge rule monotone, which changes output in edge cases and
needs a decision and a writer panel; keep run extents under truncation in `runsSharedWithNext`
(`emit/inline-runs.ts`), exact but large. Count passes
to measure, never time (§Cost fixes are measured, never timed). Found by the principles audit of the release review of 0.3.0, 2026-11-08.
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. Item 174's truncated runs leave this cascade as it was, since a link doesn't
strip edge whitespace. 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.