mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Read an inline link's target in time linear in the inline markdown
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
|
||||
import { readInlineTarget, targetMemo } from './link-syntax.ts'
|
||||
|
||||
// Every `](` of the text, read in order through one memo, as a reader closes its brackets.
|
||||
function targets(text: string): unknown[] {
|
||||
const memo = targetMemo(text)
|
||||
const read: unknown[] = []
|
||||
for (let offset = text.indexOf('](') + 1; offset > 0; offset = text.indexOf('](', offset) + 1) {
|
||||
const target = readInlineTarget(text, offset, memo)
|
||||
read.push(target === undefined ? undefined : { ...target.definition(), length: target.length })
|
||||
}
|
||||
return read
|
||||
}
|
||||
|
||||
test('a target nothing closes reads as none, at every `](`', () => {
|
||||
assert.deepEqual(targets('[a](b'.repeat(3)), [undefined, undefined, undefined])
|
||||
})
|
||||
|
||||
test('targets sharing a destination end share its title and its failure', () => {
|
||||
assert.deepEqual(targets('[a](b[c](d "t" x)'), [undefined, undefined])
|
||||
assert.deepEqual(targets('[a](b[c](d "t" x'), [undefined, undefined])
|
||||
})
|
||||
|
||||
test('an unbracketed destination ends at its first unmatched `)`', () => {
|
||||
assert.deepEqual(targets('[a](b(c)d) [e](f)'), [{ destination: 'b(c)d', length: 7 }, { destination: 'f', length: 3 }])
|
||||
assert.deepEqual(targets('[a](b\\)c)'), [{ destination: 'b)c', length: 6 }])
|
||||
})
|
||||
|
||||
test('a target reads its whitespace, a bracketed destination and a title', () => {
|
||||
assert.deepEqual(targets('[a]( b\n "t" ) [c](<d e> \'f\') [g]()'), [
|
||||
{ destination: 'b', length: 10, title: 't' },
|
||||
{ destination: 'd e', length: 11, title: 'f' },
|
||||
{ destination: '', length: 2 },
|
||||
])
|
||||
})
|
||||
@@ -5,6 +5,12 @@ export type LinkDefinition = { destination: string; title?: string }
|
||||
|
||||
export type LinkPart = { length: number; value: string }
|
||||
|
||||
// What follows a destination ending at an index, the same for every destination ending there: the index after the `)`, and the title.
|
||||
type Tail = { end: number; title: string | undefined }
|
||||
|
||||
// `destinationEnds[start]` is where an unbracketed destination starting there stops, for every start a target reads from: one after a `(`, a space or a control character.
|
||||
export type TargetMemo = { destinationEnds: Int32Array; tails: Map<number, Tail | undefined> }
|
||||
|
||||
// The lossless flavour escapes a quote alone, writing a title holding a backslash or an entity reference as a directive (spec/flavour.md, Marks); the portable flavour writes no directive, so it escapes those too, and a table cell's `|` (README, Portable markdown).
|
||||
export type TitleEscapes = 'lossless' | 'portable' | 'portable-table-cell'
|
||||
|
||||
@@ -12,6 +18,7 @@ const bracketedDestination = /<((?:[^\n<>\\]|\\[^\n])*)>/y
|
||||
// CommonMark: a link label holds at most this many characters between its brackets, an escape counting as two.
|
||||
export const labelLimit = 999
|
||||
const linkLabel = new RegExp(String.raw`\[((?:[^[\]\\]|\\[\s\S]){0,${labelLimit}})\]`, 'y')
|
||||
const linkWhitespace = /[ \t]*\n?[ \t]*/y
|
||||
const titleClosers: Readonly<Record<string, string>> = { '"': '"', "'": "'", '(': ')' }
|
||||
|
||||
// The label is matched by its normalized raw text, escapes and all.
|
||||
@@ -52,22 +59,55 @@ export function readDestination(text: string, offset: number): LinkPart | undefi
|
||||
return index <= offset ? undefined : { length: index - offset, value: decodeTextEscapes(text.slice(offset, index)) }
|
||||
}
|
||||
|
||||
// `offset` sits on the `(` a link text closes into.
|
||||
export function readInlineTarget(text: string, offset: number): { definition: LinkDefinition; length: number } | undefined {
|
||||
let index = skipLinkWhitespace(text, offset + 1)
|
||||
let destination = ''
|
||||
if (text.charAt(index) !== ')') {
|
||||
const read = readDestination(text, index)
|
||||
if (read === undefined) return undefined
|
||||
destination = read.value
|
||||
index += read.length
|
||||
// `offset` sits on the `(` a link text closes into. The definition is read on demand, so a target its link refuses costs no copy.
|
||||
export function readInlineTarget(text: string, offset: number, memo: TargetMemo): { definition: () => LinkDefinition; length: number } | undefined {
|
||||
const start = skipLinkWhitespace(text, offset + 1)
|
||||
const end = destinationEnd(text, start, memo)
|
||||
if (end === undefined) return undefined
|
||||
const tail = memo.tails.has(end) ? memo.tails.get(end) : readTail(text, end)
|
||||
memo.tails.set(end, tail)
|
||||
if (tail === undefined) return undefined
|
||||
const definition = (): LinkDefinition => {
|
||||
const edge = text.charAt(start) === '<' ? 1 : 0
|
||||
const destination = decodeTextEscapes(text.slice(start + edge, end - edge))
|
||||
return tail.title === undefined ? { destination } : { destination, title: tail.title }
|
||||
}
|
||||
const afterDestination = index
|
||||
index = skipLinkWhitespace(text, index)
|
||||
const title = index > afterDestination ? readTitle(text, index) : undefined
|
||||
return { definition, length: tail.end - offset }
|
||||
}
|
||||
|
||||
// `readDestination`'s loop run for every start at once, with a stack of the starts still open.
|
||||
export function targetMemo(text: string): TargetMemo {
|
||||
const destinationEnds = new Int32Array(text.length + 1)
|
||||
const open = [0]
|
||||
for (let index = 0; index < text.length; index += backslashEscape(text, index) === undefined ? 1 : 2) {
|
||||
const character = text.charAt(index)
|
||||
if (character === '(') open.push(index + 1)
|
||||
const start = character === ')' ? open.pop() : undefined
|
||||
if (start !== undefined) destinationEnds[start] = index
|
||||
if (character !== ' ' && !holdsControlCharacter(character)) continue
|
||||
for (const stopped of open.splice(0)) destinationEnds[stopped] = index
|
||||
open.push(index + 1)
|
||||
}
|
||||
for (const stopped of open) destinationEnds[stopped] = text.length
|
||||
return { destinationEnds, tails: new Map() }
|
||||
}
|
||||
|
||||
function destinationEnd(text: string, start: number, memo: TargetMemo): number | undefined {
|
||||
if (text.charAt(start) === ')') return start
|
||||
if (text.charAt(start) === '<') {
|
||||
bracketedDestination.lastIndex = start
|
||||
const bracketed = bracketedDestination.exec(text)?.[0]
|
||||
return bracketed === undefined ? undefined : start + bracketed.length
|
||||
}
|
||||
const end = memo.destinationEnds[start] ?? start
|
||||
return end > start ? end : undefined
|
||||
}
|
||||
|
||||
function readTail(text: string, end: number): Tail | undefined {
|
||||
let index = skipLinkWhitespace(text, end)
|
||||
const title = index > end ? readTitle(text, index) : undefined
|
||||
if (title !== undefined) index = skipLinkWhitespace(text, index + title.length)
|
||||
if (text.charAt(index) !== ')') return undefined
|
||||
return { definition: title === undefined ? { destination } : { destination, title: title.value }, length: index + 1 - offset }
|
||||
return text.charAt(index) === ')' ? { end: index + 1, title: title?.value } : undefined
|
||||
}
|
||||
|
||||
export function readTitle(text: string, offset: number): LinkPart | undefined {
|
||||
@@ -90,8 +130,10 @@ export function readTitle(text: string, offset: number): LinkPart | undefined {
|
||||
|
||||
// The label, the destination and the title each take at most one line ending with them.
|
||||
export function skipLinkWhitespace(text: string, offset: number): number {
|
||||
const rest = text.slice(offset)
|
||||
return offset + rest.length - rest.replace(/^[ \t]*\n?[ \t]*/, '').length
|
||||
linkWhitespace.lastIndex = offset
|
||||
// The pattern matches empty, so the test always moves `lastIndex` past the whitespace.
|
||||
linkWhitespace.test(text)
|
||||
return linkWhitespace.lastIndex
|
||||
}
|
||||
|
||||
export function spellDestination(href: string): string | undefined {
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { Claims, Flavour } from '../flavour.ts'
|
||||
import type { EmphasisPairing } from '../commonmark/emphasis-matching.ts'
|
||||
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
|
||||
import type { LinkDefinition, TargetMemo } from '../commonmark/link-syntax.ts'
|
||||
import { centeredImage, externalMedia } from '../../adf/plain-image.ts'
|
||||
import { commonMarkLink, linkDefinition, linkHref, linkMark, toLinkAttrs } from '../mark-spellings.ts'
|
||||
import { decodeTextEscapes, trimTrailingSpace } from '../commonmark/grammar.ts'
|
||||
@@ -15,7 +15,7 @@ import { identicalMarks, mergeAdjacentText, nodeAttrs, nodeMarks } from '../../a
|
||||
import { imageAside } from '../image-aside.ts'
|
||||
import { inlineNodeModel, takesMark } from '../../adf/inline-nodes.ts'
|
||||
import { joinsWhenRead, textBreakName, textBreakSpelling } from '../adjacent-text.ts'
|
||||
import { labelLimit, normalizeLabel, readInlineTarget, readLabel } from '../commonmark/link-syntax.ts'
|
||||
import { labelLimit, normalizeLabel, readInlineTarget, readLabel, targetMemo } from '../commonmark/link-syntax.ts'
|
||||
import { matchEmphasis } from '../commonmark/emphasis-matching.ts'
|
||||
import { noSpans, readInlineDirective } from '../directive/syntax.ts'
|
||||
import { openingLinkTakesDirective } from '../emit/spelling-questions.ts'
|
||||
@@ -87,6 +87,8 @@ type Scan = {
|
||||
pieces: Piece[]
|
||||
source: string
|
||||
spans: NestedSpans
|
||||
// Built at the first inline target the scan reads.
|
||||
targets: TargetMemo | undefined
|
||||
}
|
||||
|
||||
// What a directive's content slot inherits from the content holding it.
|
||||
@@ -117,7 +119,7 @@ export function parseInlineContent(source: string, definitions: LinkDefinitions,
|
||||
}
|
||||
|
||||
function freshScan(source: string, shared: SharedScan, own: Pick<Scan, 'claims' | 'container' | 'inLink' | 'spans'>): Scan {
|
||||
return { ...shared, ...own, brackets: [], linkOpenersFrom: 0, openingSpellableLink: false, pending: '', pieces: [], source }
|
||||
return { ...shared, ...own, brackets: [], linkOpenersFrom: 0, openingSpellableLink: false, pending: '', pieces: [], source, targets: undefined }
|
||||
}
|
||||
|
||||
function scanInline(scan: Scan): Result<Scanned> {
|
||||
@@ -395,7 +397,7 @@ function closeBracket(scan: Scan, index: number): Result<number> {
|
||||
if (!open.image) scan.linkOpenersFrom = scan.brackets.length
|
||||
// Before `linkPieces`' own-marks guard: brackets that go literal mark nothing, so a carry inside them is no error.
|
||||
if (!open.image && holdsLink(inner)) return success(unopened(scan, open, index))
|
||||
const closed = open.image ? imagePieces(scan, inner, target.definition) : linkPieces(scan, inner, target.definition)
|
||||
const closed = open.image ? imagePieces(scan, inner, target.definition()) : linkPieces(scan, inner, target.definition())
|
||||
if (!closed.ok) return closed
|
||||
scan.pieces.length = open.index
|
||||
for (const piece of closed.value) scan.pieces.push(piece)
|
||||
@@ -425,11 +427,12 @@ function bracketNodes(bracket: Bracket | OpenBracket): AdfNode[] {
|
||||
return [{ text: bracket.image ? '![' : '[', type: 'text' }]
|
||||
}
|
||||
|
||||
function resolveTarget(scan: Scan, start: number, index: number): { definition: LinkDefinition; length: number } | undefined {
|
||||
function resolveTarget(scan: Scan, start: number, index: number): { definition: () => LinkDefinition; length: number } | undefined {
|
||||
const after = index + 1
|
||||
if (scan.source.charAt(after) === '(') {
|
||||
const inline = readInlineTarget(scan.source, after)
|
||||
if (inline !== undefined) return { definition: inline.definition, length: inline.length }
|
||||
scan.targets ??= targetMemo(scan.source)
|
||||
const inline = readInlineTarget(scan.source, after, scan.targets)
|
||||
if (inline !== undefined) return inline
|
||||
}
|
||||
const label = scan.source.charAt(after) === '[' ? readLabel(scan.source, after) : undefined
|
||||
const labelIsText = label === undefined || label.value === ''
|
||||
@@ -437,7 +440,7 @@ function resolveTarget(scan: Scan, start: number, index: number): { definition:
|
||||
const name = labelIsText ? scan.source.slice(start, index) : label.value
|
||||
const definition = scan.definitions.get(normalizeLabel(name))
|
||||
if (definition === undefined) return undefined
|
||||
return { definition, length: label?.length ?? 0 }
|
||||
return { definition: () => definition, length: label?.length ?? 0 }
|
||||
}
|
||||
|
||||
// An image alone in the link text keeps its piece, so a paragraph holding nothing else builds the linked image.
|
||||
|
||||
Reference in New Issue
Block a user