mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-21 15:42:58 +01:00
Escape a line against the inline reader's own tokens, in time linear in a run
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
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 { claimsDirectivePrefix } from './directive-syntax.ts'
|
||||
import { delimiterFlags, runLength } from './commonmark/emphasis-matching.ts'
|
||||
import { highlightDelimiter, highlightFlanking } from './portable/conventions.ts'
|
||||
import { readEntityReference } from './commonmark/entity-references.ts'
|
||||
|
||||
// What the inline reader reads from a token start, `width` characters on; `text` reads literally.
|
||||
export type InlineToken =
|
||||
| { kind: 'code-span'; opener: number; width: number }
|
||||
| { closes: boolean; kind: 'highlight'; opens: boolean; width: number }
|
||||
| { canClose: boolean; canOpen: boolean; kind: 'delimiter-run'; width: number }
|
||||
| { construct: string; kind: 'html'; width: 1 }
|
||||
| { email: boolean; kind: 'autolink'; width: number }
|
||||
| { image: boolean; kind: 'bracket'; width: number }
|
||||
| { kind: 'bracket-close' | 'directive' | 'escape' | 'hard-break' | 'line-ending' | 'entity' | 'text'; width: number }
|
||||
|
||||
export function readInlineToken(source: string, index: number, claims: Claims): InlineToken {
|
||||
switch (source.charAt(index)) {
|
||||
case '<':
|
||||
return angleToken(source, index)
|
||||
case '!':
|
||||
if (claims.directives && claimsDirectivePrefix(source, index)) return { kind: 'directive', width: 1 }
|
||||
return source.charAt(index + 1) === '[' ? { image: true, kind: 'bracket', width: 2 } : text(1)
|
||||
case '[':
|
||||
return { image: false, kind: 'bracket', width: 1 }
|
||||
case ']':
|
||||
return { kind: 'bracket-close', width: 1 }
|
||||
case '\\':
|
||||
if (source.charAt(index + 1) === '\n') return { kind: 'hard-break', width: 2 }
|
||||
return backslashEscape(source, index) === undefined ? text(1) : { kind: 'escape', width: 2 }
|
||||
case '\n':
|
||||
return { kind: 'line-ending', width: 1 }
|
||||
case '&': {
|
||||
const reference = readEntityReference(source, index)
|
||||
return reference === undefined ? text(1) : { kind: 'entity', width: reference.length }
|
||||
}
|
||||
case '`':
|
||||
return backtickToken(source, index)
|
||||
case '*':
|
||||
case '_':
|
||||
case '~':
|
||||
return delimiterRunToken(source, index, runLength(source, index), claims)
|
||||
case '=':
|
||||
return claims.highlights && source.startsWith(highlightDelimiter, index) ? { ...highlightFlanking(source, index), kind: 'highlight', width: highlightDelimiter.length } : text(1)
|
||||
default:
|
||||
return text(1)
|
||||
}
|
||||
}
|
||||
|
||||
// The run of `length` delimiters from `index`, which a caller knowing the run's end reads without rescanning it.
|
||||
export function delimiterRunToken(source: string, index: number, length: number, claims: Claims): InlineToken {
|
||||
const character = source.charAt(index)
|
||||
if (character === '~' && (!claims.strikethrough || length !== 2)) return text(length)
|
||||
const flags = delimiterFlags(character, source.charAt(index - 1), source.charAt(index + length))
|
||||
return flags.canOpen || flags.canClose ? { ...flags, kind: 'delimiter-run', width: length } : text(length)
|
||||
}
|
||||
|
||||
function angleToken(source: string, index: number): InlineToken {
|
||||
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)
|
||||
return construct === undefined ? text(1) : { construct, kind: 'html', width: 1 }
|
||||
}
|
||||
|
||||
function backtickToken(source: string, index: number): InlineToken {
|
||||
const opener = backtickRun(source, index)
|
||||
const closer = closingBacktickRun(source, index + opener, opener)
|
||||
return closer === undefined ? text(opener) : { kind: 'code-span', opener, width: closer + opener - index }
|
||||
}
|
||||
|
||||
function text(width: number): InlineToken {
|
||||
return { kind: 'text', width }
|
||||
}
|
||||
Reference in New Issue
Block a user