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:
@@ -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)
|
||||
})
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user