diff --git a/src/markdown/commonmark/grammar.ts b/src/markdown/commonmark/grammar.ts index 963e2c4..84ae249 100644 --- a/src/markdown/commonmark/grammar.ts +++ b/src/markdown/commonmark/grammar.ts @@ -16,7 +16,7 @@ const htmlConstructNames = { processingInstruction: 'an HTML processing instruction', } -const controlCharacterRange = '\\u0000-\\u001f\\u007f' +export const controlCharacterRange = '\\u0000-\\u001f\\u007f' const autolinkSource = `[A-Za-z][A-Za-z0-9+.-]{1,31}:[^\\s<>${controlCharacterRange}]*` const nullCharacterSource = '\\u0000' const tagNameSource = '[A-Za-z][A-Za-z0-9-]*' diff --git a/src/markdown/commonmark/link-syntax.ts b/src/markdown/commonmark/link-syntax.ts index 5252dcf..8058a3c 100644 --- a/src/markdown/commonmark/link-syntax.ts +++ b/src/markdown/commonmark/link-syntax.ts @@ -1,6 +1,8 @@ -import { backslashEscape, decodeTextEscapes, holdsControlCharacter, holdsNullCharacter } from './grammar.ts' +import { backslashEscape, controlCharacterRange, decodeTextEscapes, holdsControlCharacter, holdsNullCharacter } from './grammar.ts' import { holdsEntityReference, readEntityReference } from './entity-references.ts' +export type InlineTarget = { definition: () => LinkDefinition; length: number } + export type LinkDefinition = { destination: string; title?: string } export type LinkPart = { length: number; value: string } @@ -15,6 +17,8 @@ export type TargetMemo = { destinationEnds: Int32Array; tails: Map\\]|\\[^\n])*)>/y +// The longest text an unbracketed destination can take: it stops at a space or a control character. +const destinationRun = new RegExp(`[^ ${controlCharacterRange}]*`, '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') @@ -40,27 +44,15 @@ export function readDestination(text: string, offset: number): LinkPart | undefi const bracketed = bracketedDestination.exec(text)?.[0] if (bracketed !== undefined) return { length: bracketed.length, value: decodeTextEscapes(bracketed.slice(1, -1)) } if (text.charAt(offset) === '<') return undefined - let depth = 0 - let index = offset - while (index < text.length) { - const character = text.charAt(index) - if (character === ' ' || holdsControlCharacter(character)) break - if (backslashEscape(text, index) !== undefined) { - index += 2 - continue - } - if (character === '(') depth += 1 - if (character === ')') { - depth -= 1 - if (depth < 0) break - } - index += 1 - } - return index <= offset ? undefined : { length: index - offset, value: decodeTextEscapes(text.slice(offset, index)) } + destinationRun.lastIndex = offset + destinationRun.test(text) + const run = text.slice(offset, destinationRun.lastIndex) + const length = targetMemo(run).destinationEnds[0] ?? 0 + return length === 0 ? undefined : { length, value: decodeTextEscapes(run.slice(0, 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 { +export function readInlineTarget(text: string, offset: number, memo: TargetMemo): InlineTarget | undefined { const start = skipLinkWhitespace(text, offset + 1) const end = destinationEnd(text, start, memo) if (end === undefined) return undefined @@ -75,7 +67,7 @@ export function readInlineTarget(text: string, offset: number, memo: TargetMemo) return { definition, length: tail.end - offset } } -// `readDestination`'s loop run for every start at once, with a stack of the starts still open. +// Every unbracketed destination's end in one pass, with a stack of the starts still open. The text's own start is one, which `readDestination` reads. export function targetMemo(text: string): TargetMemo { const destinationEnds = new Int32Array(text.length + 1) const open = [0] diff --git a/src/markdown/parse/inline-content.ts b/src/markdown/parse/inline-content.ts index 0b92642..1ca7cb1 100644 --- a/src/markdown/parse/inline-content.ts +++ b/src/markdown/parse/inline-content.ts @@ -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, TargetMemo } from '../commonmark/link-syntax.ts' +import type { InlineTarget, 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' @@ -427,7 +427,7 @@ 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): InlineTarget | undefined { const after = index + 1 if (scan.source.charAt(after) === '(') { scan.targets ??= targetMemo(scan.source)