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

Read and write unclosed backtick runs and HTML openers in time linear in the text

This commit is contained in:
2026-10-18 10:41:02 +02:00
committed by Lillem4n
parent ba15ca84b9
commit e12249e593
9 changed files with 138 additions and 35 deletions
+3
View File
@@ -16,6 +16,9 @@
table cell: `[alt](href) ([image](url))`, where it wrote `[alt](url)` and dropped the link's target.
- `losslessMarkdownToAdf` names the CommonMark spelling to write when it refuses a directive whose
node CommonMark spells, such as `[![alt](url)](href)` for a linked `!adf:mediaSingle`.
- Every reader and writer takes time linear in a text holding many unclosed backtick runs, or many
`<!--`, `<?`, `<![CDATA[` or `<!` openers without a terminator, where such a text took time
quadratic in its length: 160k characters of `<?` took seven seconds each way, now 50 ms.
- The README compares the package with seven other ADF converters, each checked against its
published version.
+17
View File
@@ -402,3 +402,20 @@ Read most (opens and searches):
- `src/markdown/portable/inline-reduction.ts` 4
- `CHANGELOG.md` 3
- `docs/decisions.md` 3
## Reading cost, chunk b3e66162c81c, finished 2026-10-17T21:28:11Z
156454 new input tokens before the first commit.
Read most (opens and searches):
- `todo.md` 6
- `README.md` 5
- `docs/decisions.md` 5
- `src/markdown/portable/adf-to-portable-markdown.test.ts` 5
- `CHANGELOG.md` 2
- `src/markdown/portable/inline-reduction.ts` 2
- `MIGRATION.md` 1
- `ci.sh` 1
- `docker-runner.sh` 1
- `spec/flavour.md` 1
@@ -0,0 +1,24 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { backtickRuns, closingRun } from './backtick-runs.ts'
test('a code span closes on the first later run of exactly its opener length', () => {
const runs = backtickRuns('`a``b```c`')
assert.equal(closingRun(runs, 1, 1), 9)
assert.equal(closingRun(runs, 1, 2), 2)
assert.equal(closingRun(runs, 1, 3), 5)
assert.equal(closingRun(runs, 1, 4), undefined)
})
test('a run before the opener closes nothing', () => {
const runs = backtickRuns('``a``b')
assert.equal(closingRun(runs, 2, 2), 3)
assert.equal(closingRun(runs, 5, 2), undefined)
})
test('an ask from earlier in the text than the last finds the earlier closer', () => {
const runs = backtickRuns('`a`b`c`')
assert.equal(closingRun(runs, 5, 1), 6)
assert.equal(closingRun(runs, 1, 1), 2)
})
+30
View File
@@ -1,9 +1,39 @@
import { runLength } from './emphasis-matching.ts'
// Each run's start by its length, read once, so asking for a closer at every opener stays linear in the text.
export interface BacktickRuns {
readonly asked: Map<number, number>
readonly starts: ReadonlyMap<number, readonly number[]>
}
export function backtickRun(text: string, index: number): number {
return text.charAt(index) === '`' ? runLength(text, index) : 0
}
export function backtickRuns(text: string): BacktickRuns {
const starts = new Map<number, number[]>()
for (let start = text.indexOf('`'); start !== -1; ) {
const length = runLength(text, start)
const known = starts.get(length)
if (known === undefined) starts.set(length, [start])
else known.push(start)
start = text.indexOf('`', start + length)
}
return { asked: new Map(), starts }
}
// As `closingBacktickRun`, for `from` past the end of an opener's run. Readers ask with `from` rising, which keeps
// each length's cursor moving forward; an ask behind the cursor restarts that length from its first run.
export function closingRun(runs: BacktickRuns, from: number, opener: number): number | undefined {
const starts = runs.starts.get(opener)
if (starts === undefined) return undefined
let at = runs.asked.get(opener) ?? 0
if ((starts[at - 1] ?? -1) >= from) at = 0
while ((starts[at] ?? Infinity) < from) at += 1
runs.asked.set(opener, at)
return starts[at]
}
// Where the run of exactly `opener` backticks closing a code span begins, `undefined` where none does.
export function closingBacktickRun(text: string, from: number, opener: number): number | undefined {
let cursor = from
+3 -2
View File
@@ -146,13 +146,14 @@ function htmlTagName(text: string): string {
return text.replace(tagName, '<$1>')
}
export function inlineHtmlConstruct(text: string, index: number): string | undefined {
// `terminated` says whether a terminator starts at or after `from`, which a caller asking at every opener answers from what it kept.
export function inlineHtmlConstruct(text: string, index: number, terminated: (terminator: string, from: number) => boolean): string | undefined {
for (const construct of inlineHtmlConstructs) {
construct.opener.lastIndex = index
const opened = construct.opener.exec(text)?.[0]
if (opened === undefined) continue
if (opened.endsWith(construct.terminator)) return construct.name
return text.includes(construct.terminator, index + opened.length) ? construct.name : undefined
return terminated(construct.terminator, index + opened.length) ? construct.name : undefined
}
htmlTag.lastIndex = index
const tag = htmlTag.exec(text)?.[0]
+8 -6
View File
@@ -1,8 +1,8 @@
import type { Claims, WrittenFlavour } from '../portable/conventions.ts'
import type { InlineToken } from '../inline-tokens.ts'
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
import type { LineContainer } from '../line-container.ts'
import { delimiterFlags, isWordCharacter, matchEmphasis, runLength } from '../commonmark/emphasis-matching.ts'
import { delimiterRunToken, readInlineToken } from '../inline-tokens.ts'
import { delimiterRunToken, inlineSource, readInlineToken } from '../inline-tokens.ts'
import { escapesLineClaim, type LinePosition } from '../commonmark/grammar.ts'
import { flavourClaims, highlightFlanking } from '../portable/conventions.ts'
import { isBareDelimiterRow } from '../pipe-table-syntax.ts'
@@ -45,6 +45,7 @@ type EscapeWalk = {
readonly container: LineContainer
readonly escapings: Escapings
readonly headingCloser: number | undefined
readonly inline: InlineSource
readonly linkClose: number
readonly scan: string
}
@@ -145,6 +146,7 @@ function escapeClaims(scan: string, escapings: Escapings, container: LineContain
container,
escapings,
headingCloser: container === 'heading' ? closingHashes(scan) : undefined,
inline: inlineSource(scan),
linkClose: lastLinkClose(scan, escapings),
scan,
}
@@ -161,7 +163,7 @@ function escapeClaims(scan: string, escapings: Escapings, container: LineContain
index = end
continue
}
const token = readInlineToken(scan, index, claims)
const token = readInlineToken(walk.inline, index, claims)
const claimed = claimsCharacter(walk, index, token, line)
if (claimed) escaped[index] = 1
// The reader takes a `==` whole, so its second `=` starts nothing unless the first is escaped.
@@ -185,12 +187,12 @@ function runHolds(escapings: Escapings, start: number, end: number, kind: (escap
return false
}
function claimsRunRest({ claims, container, escapings, scan }: EscapeWalk, index: number, end: number, line: ScanLine): boolean {
function claimsRunRest({ claims, container, escapings, inline, scan }: EscapeWalk, index: number, end: number, line: ScanLine): boolean {
const escaping = escapings(index)
if (escaping === 'bracketed-link-target') return readInlineToken(scan, index, claims).kind === 'code-span'
if (escaping === 'bracketed-link-target') return readInlineToken(inline, index, claims).kind === 'code-span'
if (!isEscapable(escaping)) return false
if (claimsLineStart(line, index, container)) return true
const token = scan.charAt(index) === '`' ? readInlineToken(scan, index, claims) : delimiterRunToken(scan, index, end - index, claims)
const token = scan.charAt(index) === '`' ? readInlineToken(inline, index, claims) : delimiterRunToken(scan, index, end - index, claims)
return token.kind !== 'text'
}
+32 -9
View File
@@ -1,7 +1,8 @@
import type { BacktickRuns } from './commonmark/backtick-runs.ts'
import type { ClaimedPrefix } from './directive-syntax.ts'
import type { Claims } from './portable/conventions.ts'
import { backslashEscape, inlineHtmlConstruct, readBracketedAutolink, readEmailAutolink } from './commonmark/grammar.ts'
import { backtickRun, closingBacktickRun } from './commonmark/backtick-runs.ts'
import { backtickRun, backtickRuns, closingRun } from './commonmark/backtick-runs.ts'
import { claimDirectivePrefix } from './directive-syntax.ts'
import { delimiterFlags, runLength } from './commonmark/emphasis-matching.ts'
import { highlightDelimiter, highlightFlanking } from './portable/conventions.ts'
@@ -18,10 +19,23 @@ export type InlineToken =
| { closes: boolean; kind: 'highlight'; opens: boolean; width: number }
| { construct: string; kind: 'html' }
export function readInlineToken(source: string, index: number, claims: Claims): InlineToken {
// A text and what its openers look ahead for, read once, so a walk reading a token at every index stays linear in it.
export type InlineSource = {
readonly backticks: BacktickRuns
// Each HTML construct terminator's last start in the text, -1 for none, read the first time an opener asks.
readonly terminators: Map<string, number>
readonly text: string
}
export function inlineSource(text: string): InlineSource {
return { backticks: backtickRuns(text), terminators: new Map(), text }
}
export function readInlineToken(inline: InlineSource, index: number, claims: Claims): InlineToken {
const source = inline.text
switch (source.charAt(index)) {
case '<':
return angleToken(source, index)
return angleToken(inline, index)
case '!': {
const prefix = claims.directives ? claimDirectivePrefix(source, index) : undefined
if (prefix !== undefined) return { kind: 'directive', prefix }
@@ -41,7 +55,7 @@ export function readInlineToken(source: string, index: number, claims: Claims):
return reference === undefined ? text(1) : { kind: 'entity', width: reference.length }
}
case '`':
return backtickToken(source, index)
return backtickToken(inline, index)
case '*':
case '_':
case '~':
@@ -61,21 +75,30 @@ export function delimiterRunToken(source: string, index: number, length: number,
return flags.canOpen || flags.canClose ? { ...flags, kind: 'delimiter-run', width: length } : text(length)
}
function angleToken(source: string, index: number): InlineToken {
function angleToken(inline: InlineSource, index: number): InlineToken {
const source = inline.text
const bracketed = readBracketedAutolink(source, index)
if (bracketed !== undefined) return { email: false, kind: 'autolink', width: bracketed }
const email = readEmailAutolink(source, index)
if (email !== undefined) return { email: true, kind: 'autolink', width: email }
const construct = inlineHtmlConstruct(source, index)
const construct = inlineHtmlConstruct(source, index, (terminator, from) => lastStart(inline, terminator) >= from)
return construct === undefined ? text(1) : { construct, kind: 'html' }
}
function backtickToken(source: string, index: number): InlineToken {
const opener = backtickRun(source, index)
const closer = closingBacktickRun(source, index + opener, opener)
function backtickToken(inline: InlineSource, index: number): InlineToken {
const opener = backtickRun(inline.text, index)
const closer = closingRun(inline.backticks, index + opener, opener)
return closer === undefined ? text(opener) : { kind: 'code-span', opener, width: closer + opener - index }
}
function lastStart(inline: InlineSource, terminator: string): number {
const known = inline.terminators.get(terminator)
if (known !== undefined) return known
const last = inline.text.lastIndexOf(terminator)
inline.terminators.set(terminator, last)
return last
}
function text(width: number): InlineToken {
return { kind: 'text', width }
}
+3 -2
View File
@@ -20,7 +20,7 @@ import { openingLinkTakesDirective } from '../emit/inline-line.ts'
import { readCarriedInline } from '../opaque-carry.ts'
import { readDirectiveMark } from './directive-marks.ts'
import { readInlineDirectiveNode } from './directive-nodes.ts'
import { readInlineToken } from '../inline-tokens.ts'
import { inlineSource, readInlineToken } from '../inline-tokens.ts'
import { readTextDirective } from '../text-directive.ts'
// Only a `paragraph` block takes `image`, the `mediaSingle` a lone image builds; every other container takes `nodes`, where the image is its linked alt text.
@@ -109,9 +109,10 @@ function freshScan(source: string, shared: SharedScan, own: Pick<Scan, 'claims'
}
function scanInline(scan: Scan): Result<Scanned> {
const inline = inlineSource(scan.source)
let index = 0
while (index < scan.source.length) {
const read = readToken(scan, index, readInlineToken(scan.source, index, scan.claims))
const read = readToken(scan, index, readInlineToken(inline, index, scan.claims))
if (!read.ok) return read
index = read.value
}
+18 -16
View File
@@ -6,13 +6,12 @@
`Bar = 0.1`
`Next ID = 119`
`Next ID = 120`
## Items
| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 96 | 0.3.0 | | defect | **Read and write a text holding many unclosed backtick runs, or many `<!--`, `<?`, `<![CDATA[` or `<!` openers without a terminator, in time linear in the text.** | 2 | 1 | 2 | 3 | 3 | 9 by 3 | 9.0 |
| 98 | 0.3.0 | | defect | **Read blank lines inside deeply nested list items in time linear in the markdown.** | 2 | 1 | 2 | 3 | 3 | 9 by 2 | 6.0 |
| 115 | 0.3.0 | | defect | **Build text holding no `border` mark in `losslessMarkdownToAdf`, and have `adfToLosslessMarkdown` carry text holding one.** | 2 | 1 | 2 | 3 | 3 | 4 by 3 | 4.5 |
| 100 | 0.3.0 | | decision | **Put the reading of an image's title as its caption back to a reader panel, now that a writer panel reads a title as hover text.** | 1 | 2 | 2 | 3 | 3 | 6 by 4 | 4.0 |
@@ -25,7 +24,8 @@
| 102 | 0.3.0 | | decision | **Place a block a portable task list refuses at its path in the returned document without replaying `splitTaskList` on stand-in nodes.** | 2 | 1 | 2 | 3 | 4 | 10 by 4 | 2.0 |
| 59 | 0.3.0 | | decision | **Group the directive grammar into `src/markdown/directive/`, move `Read<T>` to `result.ts`, and move `Flavour` and `flavourClaims` to `markdown/flavour.ts`.** | 3 | 1 | 2 | 4 | 5 | 10 by 5 | 1.5 |
| 62 | 0.3.0 | | decision | **Move the portable flavour's reading out of `parse/` and its writing out of `emit/` into `markdown/portable/`, so `emit/` no longer imports `portable/`.** | 3 | 1 | 2 | 4 | 5 | 10 by 5 | 1.5 |
| 73 | 0.3.0 | 33, 59, 60, 62, 91, 92, 95, 96, 98, 100, 102, 109, 115 | | **Announce the package where someone needing an ADF converter already reads: JRACLOUD-77436, the Atlassian developer community and Stack Overflow's ADF-to-markdown questions.** | 1 | 2 | 2 | 3 | 2 | 3 by 8 | 32.0 |
| 73 | 0.3.0 | 33, 59, 60, 62, 91, 92, 95, 98, 100, 102, 109, 115 | | **Announce the package where someone needing an ADF converter already reads: JRACLOUD-77436, the Atlassian developer community and Stack Overflow's ADF-to-markdown questions.** | 1 | 2 | 2 | 3 | 2 | 3 by 8 | 32.0 |
| 119 | 0.3.0 | | defect | **Read a directive's content holding many unclosed backtick runs in time linear in the text.** | 2 | 1 | 2 | 3 | 2 | 9 by 2 | 8.0 |
| 81 | 0.4.0 | | | **Fail the gate unless the README's error tables list exactly the codes `ConvertErrorCode` holds.** | 1 | 1 | 1 | 1 | 2 | 3 by 2 | 8.0 |
| 45 | 0.4.0 | | defect | **Replace `isAdfDocument` with a reader returning `Result<AdfDocument>`.** | 2 | 1 | 3 | 4 | 3 | 1 by 2 | 6.0 |
| 6 | 0.4.0 | | decision | **Specify the HTML dialect.** | 3 | 2 | 1 | 4 | 6 | | 0.0 |
@@ -59,14 +59,6 @@
## Details
### 96. Read and write a text holding many unclosed backtick runs, or many `<!--`, `<?`, `<![CDATA[` or `<!` openers without a terminator, in time linear in the text.
`readInlineToken` (`src/markdown/inline-tokens.ts`) searches the rest of the text at every opener:
`closingBacktickRun` for a run of the same length, and `inlineHtmlConstruct` for the terminator. So
in a text of length n, backtick runs of lengths 1 to k with no closers cost about k·n, and `<!--`
repeated with no `-->` is quadratic, in the reader and the emitter's escape walk alike.
commonmark.js avoids the backtick cost by caching backtick runs by length.
### 98. Read blank lines inside deeply nested list items in time linear in the markdown.
`matchContainers` in `src/markdown/parse/blocks.ts` continues every open item holding a block on
@@ -136,11 +128,11 @@ parts by ADF and format true as they grow.
### 92. Lower the reading cost of the files a chunk reads most before its first commit.
The last five chunks' median is 113495 new input tokens before their first commit, the five before
them 96564. Files read most over the last five chunks, counting opens and searches: `README.md` 38,
`todo.md` 36, `spec/flavour.md` 22, `docs/decisions.md` 20,
`src/markdown/parse/markdown-to-adf.test.ts` 18, `src/markdown/emit/adf-to-markdown.test.ts` 14,
`CHANGELOG.md` 12, `src/markdown/directive-syntax.ts` 10, `src/conformance/adf-schema.test.ts` 8,
`src/markdown/parse/inline-content.ts` 7.
them 108874. Files read most over the last five chunks, counting opens and searches: `README.md` 39,
`todo.md` 36, `docs/decisions.md` 20, `src/markdown/parse/markdown-to-adf.test.ts` 18,
`CHANGELOG.md` 14, `spec/flavour.md` 13, `src/markdown/emit/adf-to-markdown.test.ts` 8,
`src/markdown/parse/inline-content.ts` 7, `MIGRATION.md` 6,
`src/conformance/commonmark-spec.test.ts` 6.
### 102. Place a block a portable task list refuses at its path in the returned document without replaying `splitTaskList` on stand-in nodes.
@@ -175,6 +167,16 @@ posting, set `converter-comparison/run.sh`'s pins to each package's latest versi
its versions and its date in step with the output. We draft each post for the place it goes; the
maintainer posts them once `0.3.0` is published. Found while planning discoverability, 2026-10-04.
### 119. Read a directive's content holding many unclosed backtick runs in time linear in the text.
`readCodeSpanEnd` (`src/markdown/directive-syntax.ts`) calls `closingBacktickRun`, which searches
the rest of the text at every opener, so backtick runs of lengths 1 to k with no closers inside an
inline directive's `[content]` cost about k·n. Measured on 2026-10-18: 24 ms at 40k characters,
126 ms at 160k and 1004 ms at 640k, where the same runs outside a directive take 6, 18 and 63 ms.
The inline reader reads such runs through `backtickRuns` and `closingRun`
(`src/markdown/commonmark/backtick-runs.ts`), kept once per text; the directive reader would need
them kept for the source it reads from.
### 81. Fail the gate unless the README's error tables list exactly the codes `ConvertErrorCode` holds.
`src/result.test.ts` checks the union against the call sites only, so a code added or removed