mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-21 07:32:58 +01:00
Answer principles round 1: 1 🔴, 0 🟡 (0 misread); readDestination reads through targetMemo, so one loop holds the destination grammar
This commit is contained in:
@@ -16,7 +16,7 @@ const htmlConstructNames = {
|
|||||||
processingInstruction: 'an HTML processing instruction',
|
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 autolinkSource = `[A-Za-z][A-Za-z0-9+.-]{1,31}:[^\\s<>${controlCharacterRange}]*`
|
||||||
const nullCharacterSource = '\\u0000'
|
const nullCharacterSource = '\\u0000'
|
||||||
const tagNameSource = '[A-Za-z][A-Za-z0-9-]*'
|
const tagNameSource = '[A-Za-z][A-Za-z0-9-]*'
|
||||||
|
|||||||
@@ -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'
|
import { holdsEntityReference, readEntityReference } from './entity-references.ts'
|
||||||
|
|
||||||
|
export type InlineTarget = { definition: () => LinkDefinition; length: number }
|
||||||
|
|
||||||
export type LinkDefinition = { destination: string; title?: string }
|
export type LinkDefinition = { destination: string; title?: string }
|
||||||
|
|
||||||
export type LinkPart = { length: number; value: string }
|
export type LinkPart = { length: number; value: string }
|
||||||
@@ -15,6 +17,8 @@ export type TargetMemo = { destinationEnds: Int32Array; tails: Map<number, Tail
|
|||||||
export type TitleEscapes = 'lossless' | 'portable' | 'portable-table-cell'
|
export type TitleEscapes = 'lossless' | 'portable' | 'portable-table-cell'
|
||||||
|
|
||||||
const bracketedDestination = /<((?:[^\n<>\\]|\\[^\n])*)>/y
|
const bracketedDestination = /<((?:[^\n<>\\]|\\[^\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.
|
// CommonMark: a link label holds at most this many characters between its brackets, an escape counting as two.
|
||||||
export const labelLimit = 999
|
export const labelLimit = 999
|
||||||
const linkLabel = new RegExp(String.raw`\[((?:[^[\]\\]|\\[\s\S]){0,${labelLimit}})\]`, 'y')
|
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]
|
const bracketed = bracketedDestination.exec(text)?.[0]
|
||||||
if (bracketed !== undefined) return { length: bracketed.length, value: decodeTextEscapes(bracketed.slice(1, -1)) }
|
if (bracketed !== undefined) return { length: bracketed.length, value: decodeTextEscapes(bracketed.slice(1, -1)) }
|
||||||
if (text.charAt(offset) === '<') return undefined
|
if (text.charAt(offset) === '<') return undefined
|
||||||
let depth = 0
|
destinationRun.lastIndex = offset
|
||||||
let index = offset
|
destinationRun.test(text)
|
||||||
while (index < text.length) {
|
const run = text.slice(offset, destinationRun.lastIndex)
|
||||||
const character = text.charAt(index)
|
const length = targetMemo(run).destinationEnds[0] ?? 0
|
||||||
if (character === ' ' || holdsControlCharacter(character)) break
|
return length === 0 ? undefined : { length, value: decodeTextEscapes(run.slice(0, length)) }
|
||||||
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)) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// `offset` sits on the `(` a link text closes into. The definition is read on demand, so a target its link refuses costs no copy.
|
// `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 start = skipLinkWhitespace(text, offset + 1)
|
||||||
const end = destinationEnd(text, start, memo)
|
const end = destinationEnd(text, start, memo)
|
||||||
if (end === undefined) return undefined
|
if (end === undefined) return undefined
|
||||||
@@ -75,7 +67,7 @@ export function readInlineTarget(text: string, offset: number, memo: TargetMemo)
|
|||||||
return { definition, length: tail.end - offset }
|
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 {
|
export function targetMemo(text: string): TargetMemo {
|
||||||
const destinationEnds = new Int32Array(text.length + 1)
|
const destinationEnds = new Int32Array(text.length + 1)
|
||||||
const open = [0]
|
const open = [0]
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import type { Claims, Flavour } from '../flavour.ts'
|
|||||||
import type { EmphasisPairing } from '../commonmark/emphasis-matching.ts'
|
import type { EmphasisPairing } from '../commonmark/emphasis-matching.ts'
|
||||||
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
|
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
|
||||||
import type { LineContainer } from '../line-container.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 { centeredImage, externalMedia } from '../../adf/plain-image.ts'
|
||||||
import { commonMarkLink, linkDefinition, linkHref, linkMark, toLinkAttrs } from '../mark-spellings.ts'
|
import { commonMarkLink, linkDefinition, linkHref, linkMark, toLinkAttrs } from '../mark-spellings.ts'
|
||||||
import { decodeTextEscapes, trimTrailingSpace } from '../commonmark/grammar.ts'
|
import { decodeTextEscapes, trimTrailingSpace } from '../commonmark/grammar.ts'
|
||||||
@@ -427,7 +427,7 @@ function bracketNodes(bracket: Bracket | OpenBracket): AdfNode[] {
|
|||||||
return [{ text: bracket.image ? '![' : '[', type: 'text' }]
|
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
|
const after = index + 1
|
||||||
if (scan.source.charAt(after) === '(') {
|
if (scan.source.charAt(after) === '(') {
|
||||||
scan.targets ??= targetMemo(scan.source)
|
scan.targets ??= targetMemo(scan.source)
|
||||||
|
|||||||
Reference in New Issue
Block a user