3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45: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
@@ -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]