mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-17 21:55:44 +01:00
14 - the CommonMark subset moves under src/markdown/commonmark/
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { runLength } from './emphasis-matching.ts'
|
||||
|
||||
export function backtickRun(text: string, index: number): number {
|
||||
return text.charAt(index) === '`' ? runLength(text, index) : 0
|
||||
}
|
||||
|
||||
// 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
|
||||
while (cursor < text.length) {
|
||||
const run = backtickRun(text, cursor)
|
||||
if (run === opener) return cursor
|
||||
cursor += run === 0 ? 1 : run
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function fencedCodeBlock(info: string, body: string): string {
|
||||
const fence = '`'.repeat(Math.max(3, longestBacktickRun(body) + 1))
|
||||
return body === '' ? `${fence}${info}\n${fence}` : `${fence}${info}\n${body}\n${fence}`
|
||||
}
|
||||
|
||||
export function longestBacktickRun(text: string): number {
|
||||
let longest = 0
|
||||
let current = 0
|
||||
for (const character of text) {
|
||||
current = character === '`' ? current + 1 : 0
|
||||
longest = Math.max(longest, current)
|
||||
}
|
||||
return longest
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { isUnicodeWhitespace } from './grammar.ts'
|
||||
|
||||
type DelimiterRun = { canClose: boolean; canOpen: boolean; character: string; length: number }
|
||||
|
||||
export type EmphasisPairing<Run> = { closer: Run; closerOffset: number; opener: Run; openerOffset: number; used: number }
|
||||
|
||||
type Candidate<Run> = {
|
||||
head: number
|
||||
next: Candidate<Run> | undefined
|
||||
original: number
|
||||
previous: Candidate<Run> | undefined
|
||||
remaining: number
|
||||
run: Run
|
||||
tail: number
|
||||
}
|
||||
|
||||
const unicodePunctuation = /[\p{P}\p{S}]/u
|
||||
|
||||
export function delimiterFlags(character: string, before: string, after: string): { canClose: boolean; canOpen: boolean } {
|
||||
const left = isLeftFlanking(before, after)
|
||||
const right = isRightFlanking(before, after)
|
||||
if (character !== '_') return { canClose: right, canOpen: left }
|
||||
return { canClose: right && (!left || isPunctuation(after)), canOpen: left && (!right || isPunctuation(before)) }
|
||||
}
|
||||
|
||||
export function isWordCharacter(character: string): boolean {
|
||||
return character !== '' && !isWhitespace(character) && !isPunctuation(character)
|
||||
}
|
||||
|
||||
export function matchEmphasis<Run extends DelimiterRun>(runs: readonly Run[]): EmphasisPairing<Run>[] {
|
||||
const pairings: EmphasisPairing<Run>[] = []
|
||||
const bottoms = new Map<string, Candidate<Run> | undefined>()
|
||||
let closer = candidates(runs)
|
||||
while (closer !== undefined) {
|
||||
if (!closer.run.canClose) {
|
||||
closer = closer.next
|
||||
continue
|
||||
}
|
||||
const key = `${closer.run.character}${closer.run.canOpen}${closer.original % 3}`
|
||||
const bottom = bottoms.get(key)
|
||||
let opener = closer.previous
|
||||
while (opener !== undefined && opener !== bottom && !pairs(opener, closer)) opener = opener.previous
|
||||
if (opener === undefined || opener === bottom) {
|
||||
bottoms.set(key, closer.previous)
|
||||
const following = closer.next
|
||||
if (!closer.run.canOpen) unlink(closer)
|
||||
closer = following
|
||||
continue
|
||||
}
|
||||
const used = closer.remaining >= 2 && opener.remaining >= 2 ? 2 : 1
|
||||
opener.remaining -= used
|
||||
opener.tail -= used
|
||||
pairings.push({ closer: closer.run, closerOffset: closer.head, opener: opener.run, openerOffset: opener.tail, used })
|
||||
closer.head += used
|
||||
closer.remaining -= used
|
||||
opener.next = closer
|
||||
closer.previous = opener
|
||||
if (opener.remaining === 0) unlink(opener)
|
||||
if (closer.remaining > 0) continue
|
||||
const following = closer.next
|
||||
unlink(closer)
|
||||
closer = following
|
||||
}
|
||||
return pairings
|
||||
}
|
||||
|
||||
export function runLength(text: string, index: number): number {
|
||||
const character = text.charAt(index)
|
||||
let length = 0
|
||||
while (text.charAt(index + length) === character) length += 1
|
||||
return length
|
||||
}
|
||||
|
||||
function candidates<Run extends DelimiterRun>(runs: readonly Run[]): Candidate<Run> | undefined {
|
||||
let first: Candidate<Run> | undefined
|
||||
let previous: Candidate<Run> | undefined
|
||||
for (const run of runs) {
|
||||
const candidate: Candidate<Run> = {
|
||||
head: 0,
|
||||
next: undefined,
|
||||
original: run.length,
|
||||
previous,
|
||||
remaining: run.length,
|
||||
run,
|
||||
tail: run.length,
|
||||
}
|
||||
if (previous === undefined) first = candidate
|
||||
else previous.next = candidate
|
||||
previous = candidate
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
function pairs<Run extends DelimiterRun>(opener: Candidate<Run>, closer: Candidate<Run>): boolean {
|
||||
if (!opener.run.canOpen || opener.run.character !== closer.run.character) return false
|
||||
const odd = (closer.run.canOpen || opener.run.canClose) && closer.original % 3 !== 0 && (opener.original + closer.original) % 3 === 0
|
||||
return !odd
|
||||
}
|
||||
|
||||
function unlink<Run>(candidate: Candidate<Run>): void {
|
||||
if (candidate.previous !== undefined) candidate.previous.next = candidate.next
|
||||
if (candidate.next !== undefined) candidate.next.previous = candidate.previous
|
||||
}
|
||||
|
||||
function isLeftFlanking(before: string, after: string): boolean {
|
||||
if (isWhitespace(after)) return false
|
||||
if (!isPunctuation(after)) return true
|
||||
return isWhitespace(before) || isPunctuation(before)
|
||||
}
|
||||
|
||||
function isRightFlanking(before: string, after: string): boolean {
|
||||
if (isWhitespace(before)) return false
|
||||
if (!isPunctuation(before)) return true
|
||||
return isWhitespace(after) || isPunctuation(after)
|
||||
}
|
||||
|
||||
function isPunctuation(character: string): boolean {
|
||||
return character !== '' && unicodePunctuation.test(character)
|
||||
}
|
||||
|
||||
function isWhitespace(character: string): boolean {
|
||||
return character === '' || isUnicodeWhitespace(character)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { dirname, join } from 'node:path'
|
||||
import test from 'node:test'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import { readEntityReference } from './entity-references.ts'
|
||||
|
||||
const source = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'entity-references.ts'), 'utf8')
|
||||
|
||||
// A value holding a space or a tilde mis-splits into a wrong name and a lost one, silently, at load.
|
||||
test('every packed reference parts into one name and one value the table decodes', () => {
|
||||
const packed = /const packedReferences =\n {2}'([\s\S]*?)'\n/.exec(source)?.[1]
|
||||
assert.ok(packed !== undefined)
|
||||
const references = packed.split(/(?:\\\n)? +/)
|
||||
assert.equal(references.length, 2125)
|
||||
for (const reference of references) {
|
||||
assert.match(reference, /^[A-Za-z][A-Za-z0-9]*~[^ ~]+$/)
|
||||
assert.ok((readEntityReference(`&${reference.slice(0, reference.indexOf('~'))};`, 0)?.text ?? '') !== '')
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,238 @@
|
||||
type EntityReference = { length: number; text: string }
|
||||
|
||||
const entityReferenceSource = '&(?:[A-Za-z][A-Za-z0-9]{1,31}|#\\d{1,7}|#[Xx][A-Fa-f0-9]{1,6});'
|
||||
const anchoredEntityReference = new RegExp(`(?:${entityReferenceSource})`, 'y')
|
||||
const decimalReference = /^&#(\d+);/
|
||||
const hexadecimalReference = /^&#[Xx]([A-Fa-f0-9]+);/
|
||||
const largestCodePoint = 0x10ffff
|
||||
export const replacementCharacter = '\ufffd'
|
||||
const surrogates = { first: 0xd800, last: 0xdfff }
|
||||
|
||||
// HTML5's named character references (https://html.spec.whatwg.org/entities.json), the semicolon-terminated half
|
||||
// CommonMark decodes. Name and characters part on a tilde, references on a space, neither of which any value holds.
|
||||
const packedReferences =
|
||||
'AElig~Æ AMP~& Aacute~Á Abreve~Ă Acirc~Â Acy~А Afr~𝔄 Agrave~À Alpha~Α Amacr~Ā And~⩓ Aogon~Ą Aopf~𝔸\
|
||||
ApplyFunction~ Aring~Å Ascr~𝒜 Assign~≔ Atilde~Ã Auml~Ä Backslash~∖ Barv~⫧ Barwed~⌆ Bcy~Б Because~∵ Bernoullis~ℬ\
|
||||
Beta~Β Bfr~𝔅 Bopf~𝔹 Breve~˘ Bscr~ℬ Bumpeq~≎ CHcy~Ч COPY~© Cacute~Ć Cap~⋒ CapitalDifferentialD~ⅅ Cayleys~ℭ\
|
||||
Ccaron~Č Ccedil~Ç Ccirc~Ĉ Cconint~∰ Cdot~Ċ Cedilla~¸ CenterDot~· Cfr~ℭ Chi~Χ CircleDot~⊙ CircleMinus~⊖\
|
||||
CirclePlus~⊕ CircleTimes~⊗ ClockwiseContourIntegral~∲ CloseCurlyDoubleQuote~” CloseCurlyQuote~’ Colon~∷ Colone~⩴\
|
||||
Congruent~≡ Conint~∯ ContourIntegral~∮ Copf~ℂ Coproduct~∐ CounterClockwiseContourIntegral~∳ Cross~⨯ Cscr~𝒞 Cup~⋓\
|
||||
CupCap~≍ DD~ⅅ DDotrahd~⤑ DJcy~Ђ DScy~Ѕ DZcy~Џ Dagger~‡ Darr~↡ Dashv~⫤ Dcaron~Ď Dcy~Д Del~∇ Delta~Δ Dfr~𝔇\
|
||||
DiacriticalAcute~´ DiacriticalDot~˙ DiacriticalDoubleAcute~˝ DiacriticalGrave~` DiacriticalTilde~˜ Diamond~⋄\
|
||||
DifferentialD~ⅆ Dopf~𝔻 Dot~¨ DotDot~⃜ DotEqual~≐ DoubleContourIntegral~∯ DoubleDot~¨ DoubleDownArrow~⇓\
|
||||
DoubleLeftArrow~⇐ DoubleLeftRightArrow~⇔ DoubleLeftTee~⫤ DoubleLongLeftArrow~⟸ DoubleLongLeftRightArrow~⟺\
|
||||
DoubleLongRightArrow~⟹ DoubleRightArrow~⇒ DoubleRightTee~⊨ DoubleUpArrow~⇑ DoubleUpDownArrow~⇕ DoubleVerticalBar~∥\
|
||||
DownArrow~↓ DownArrowBar~⤓ DownArrowUpArrow~⇵ DownBreve~̑ DownLeftRightVector~⥐ DownLeftTeeVector~⥞\
|
||||
DownLeftVector~↽ DownLeftVectorBar~⥖ DownRightTeeVector~⥟ DownRightVector~⇁ DownRightVectorBar~⥗ DownTee~⊤\
|
||||
DownTeeArrow~↧ Downarrow~⇓ Dscr~𝒟 Dstrok~Đ ENG~Ŋ ETH~Ð Eacute~É Ecaron~Ě Ecirc~Ê Ecy~Э Edot~Ė Efr~𝔈 Egrave~È\
|
||||
Element~∈ Emacr~Ē EmptySmallSquare~◻ EmptyVerySmallSquare~▫ Eogon~Ę Eopf~𝔼 Epsilon~Ε Equal~⩵ EqualTilde~≂\
|
||||
Equilibrium~⇌ Escr~ℰ Esim~⩳ Eta~Η Euml~Ë Exists~∃ ExponentialE~ⅇ Fcy~Ф Ffr~𝔉 FilledSmallSquare~◼\
|
||||
FilledVerySmallSquare~▪ Fopf~𝔽 ForAll~∀ Fouriertrf~ℱ Fscr~ℱ GJcy~Ѓ GT~> Gamma~Γ Gammad~Ϝ Gbreve~Ğ Gcedil~Ģ\
|
||||
Gcirc~Ĝ Gcy~Г Gdot~Ġ Gfr~𝔊 Gg~⋙ Gopf~𝔾 GreaterEqual~≥ GreaterEqualLess~⋛ GreaterFullEqual~≧ GreaterGreater~⪢\
|
||||
GreaterLess~≷ GreaterSlantEqual~⩾ GreaterTilde~≳ Gscr~𝒢 Gt~≫ HARDcy~Ъ Hacek~ˇ Hat~^ Hcirc~Ĥ Hfr~ℌ HilbertSpace~ℋ\
|
||||
Hopf~ℍ HorizontalLine~─ Hscr~ℋ Hstrok~Ħ HumpDownHump~≎ HumpEqual~≏ IEcy~Е IJlig~IJ IOcy~Ё Iacute~Í Icirc~Î Icy~И\
|
||||
Idot~İ Ifr~ℑ Igrave~Ì Im~ℑ Imacr~Ī ImaginaryI~ⅈ Implies~⇒ Int~∬ Integral~∫ Intersection~⋂ InvisibleComma~\
|
||||
InvisibleTimes~ Iogon~Į Iopf~𝕀 Iota~Ι Iscr~ℐ Itilde~Ĩ Iukcy~І Iuml~Ï Jcirc~Ĵ Jcy~Й Jfr~𝔍 Jopf~𝕁 Jscr~𝒥\
|
||||
Jsercy~Ј Jukcy~Є KHcy~Х KJcy~Ќ Kappa~Κ Kcedil~Ķ Kcy~К Kfr~𝔎 Kopf~𝕂 Kscr~𝒦 LJcy~Љ LT~< Lacute~Ĺ Lambda~Λ Lang~⟪\
|
||||
Laplacetrf~ℒ Larr~↞ Lcaron~Ľ Lcedil~Ļ Lcy~Л LeftAngleBracket~⟨ LeftArrow~← LeftArrowBar~⇤ LeftArrowRightArrow~⇆\
|
||||
LeftCeiling~⌈ LeftDoubleBracket~⟦ LeftDownTeeVector~⥡ LeftDownVector~⇃ LeftDownVectorBar~⥙ LeftFloor~⌊\
|
||||
LeftRightArrow~↔ LeftRightVector~⥎ LeftTee~⊣ LeftTeeArrow~↤ LeftTeeVector~⥚ LeftTriangle~⊲ LeftTriangleBar~⧏\
|
||||
LeftTriangleEqual~⊴ LeftUpDownVector~⥑ LeftUpTeeVector~⥠ LeftUpVector~↿ LeftUpVectorBar~⥘ LeftVector~↼\
|
||||
LeftVectorBar~⥒ Leftarrow~⇐ Leftrightarrow~⇔ LessEqualGreater~⋚ LessFullEqual~≦ LessGreater~≶ LessLess~⪡\
|
||||
LessSlantEqual~⩽ LessTilde~≲ Lfr~𝔏 Ll~⋘ Lleftarrow~⇚ Lmidot~Ŀ LongLeftArrow~⟵ LongLeftRightArrow~⟷\
|
||||
LongRightArrow~⟶ Longleftarrow~⟸ Longleftrightarrow~⟺ Longrightarrow~⟹ Lopf~𝕃 LowerLeftArrow~↙ LowerRightArrow~↘\
|
||||
Lscr~ℒ Lsh~↰ Lstrok~Ł Lt~≪ Map~⤅ Mcy~М MediumSpace~ Mellintrf~ℳ Mfr~𝔐 MinusPlus~∓ Mopf~𝕄 Mscr~ℳ Mu~Μ NJcy~Њ\
|
||||
Nacute~Ń Ncaron~Ň Ncedil~Ņ Ncy~Н NegativeMediumSpace~ NegativeThickSpace~ NegativeThinSpace~\
|
||||
NegativeVeryThinSpace~ NestedGreaterGreater~≫ NestedLessLess~≪ NewLine~\n Nfr~𝔑 NoBreak~ NonBreakingSpace~ \
|
||||
Nopf~ℕ Not~⫬ NotCongruent~≢ NotCupCap~≭ NotDoubleVerticalBar~∦ NotElement~∉ NotEqual~≠ NotEqualTilde~≂̸\
|
||||
NotExists~∄ NotGreater~≯ NotGreaterEqual~≱ NotGreaterFullEqual~≧̸ NotGreaterGreater~≫̸ NotGreaterLess~≹\
|
||||
NotGreaterSlantEqual~⩾̸ NotGreaterTilde~≵ NotHumpDownHump~≎̸ NotHumpEqual~≏̸ NotLeftTriangle~⋪\
|
||||
NotLeftTriangleBar~⧏̸ NotLeftTriangleEqual~⋬ NotLess~≮ NotLessEqual~≰ NotLessGreater~≸ NotLessLess~≪̸\
|
||||
NotLessSlantEqual~⩽̸ NotLessTilde~≴ NotNestedGreaterGreater~⪢̸ NotNestedLessLess~⪡̸ NotPrecedes~⊀\
|
||||
NotPrecedesEqual~⪯̸ NotPrecedesSlantEqual~⋠ NotReverseElement~∌ NotRightTriangle~⋫ NotRightTriangleBar~⧐̸\
|
||||
NotRightTriangleEqual~⋭ NotSquareSubset~⊏̸ NotSquareSubsetEqual~⋢ NotSquareSuperset~⊐̸ NotSquareSupersetEqual~⋣\
|
||||
NotSubset~⊂⃒ NotSubsetEqual~⊈ NotSucceeds~⊁ NotSucceedsEqual~⪰̸ NotSucceedsSlantEqual~⋡ NotSucceedsTilde~≿̸\
|
||||
NotSuperset~⊃⃒ NotSupersetEqual~⊉ NotTilde~≁ NotTildeEqual~≄ NotTildeFullEqual~≇ NotTildeTilde~≉ NotVerticalBar~∤\
|
||||
Nscr~𝒩 Ntilde~Ñ Nu~Ν OElig~Œ Oacute~Ó Ocirc~Ô Ocy~О Odblac~Ő Ofr~𝔒 Ograve~Ò Omacr~Ō Omega~Ω Omicron~Ο Oopf~𝕆\
|
||||
OpenCurlyDoubleQuote~“ OpenCurlyQuote~‘ Or~⩔ Oscr~𝒪 Oslash~Ø Otilde~Õ Otimes~⨷ Ouml~Ö OverBar~‾ OverBrace~⏞\
|
||||
OverBracket~⎴ OverParenthesis~⏜ PartialD~∂ Pcy~П Pfr~𝔓 Phi~Φ Pi~Π PlusMinus~± Poincareplane~ℌ Popf~ℙ Pr~⪻\
|
||||
Precedes~≺ PrecedesEqual~⪯ PrecedesSlantEqual~≼ PrecedesTilde~≾ Prime~″ Product~∏ Proportion~∷ Proportional~∝\
|
||||
Pscr~𝒫 Psi~Ψ QUOT~" Qfr~𝔔 Qopf~ℚ Qscr~𝒬 RBarr~⤐ REG~® Racute~Ŕ Rang~⟫ Rarr~↠ Rarrtl~⤖ Rcaron~Ř Rcedil~Ŗ Rcy~Р\
|
||||
Re~ℜ ReverseElement~∋ ReverseEquilibrium~⇋ ReverseUpEquilibrium~⥯ Rfr~ℜ Rho~Ρ RightAngleBracket~⟩ RightArrow~→\
|
||||
RightArrowBar~⇥ RightArrowLeftArrow~⇄ RightCeiling~⌉ RightDoubleBracket~⟧ RightDownTeeVector~⥝ RightDownVector~⇂\
|
||||
RightDownVectorBar~⥕ RightFloor~⌋ RightTee~⊢ RightTeeArrow~↦ RightTeeVector~⥛ RightTriangle~⊳ RightTriangleBar~⧐\
|
||||
RightTriangleEqual~⊵ RightUpDownVector~⥏ RightUpTeeVector~⥜ RightUpVector~↾ RightUpVectorBar~⥔ RightVector~⇀\
|
||||
RightVectorBar~⥓ Rightarrow~⇒ Ropf~ℝ RoundImplies~⥰ Rrightarrow~⇛ Rscr~ℛ Rsh~↱ RuleDelayed~⧴ SHCHcy~Щ SHcy~Ш\
|
||||
SOFTcy~Ь Sacute~Ś Sc~⪼ Scaron~Š Scedil~Ş Scirc~Ŝ Scy~С Sfr~𝔖 ShortDownArrow~↓ ShortLeftArrow~← ShortRightArrow~→\
|
||||
ShortUpArrow~↑ Sigma~Σ SmallCircle~∘ Sopf~𝕊 Sqrt~√ Square~□ SquareIntersection~⊓ SquareSubset~⊏\
|
||||
SquareSubsetEqual~⊑ SquareSuperset~⊐ SquareSupersetEqual~⊒ SquareUnion~⊔ Sscr~𝒮 Star~⋆ Sub~⋐ Subset~⋐\
|
||||
SubsetEqual~⊆ Succeeds~≻ SucceedsEqual~⪰ SucceedsSlantEqual~≽ SucceedsTilde~≿ SuchThat~∋ Sum~∑ Sup~⋑ Superset~⊃\
|
||||
SupersetEqual~⊇ Supset~⋑ THORN~Þ TRADE~™ TSHcy~Ћ TScy~Ц Tab~\t Tau~Τ Tcaron~Ť Tcedil~Ţ Tcy~Т Tfr~𝔗 Therefore~∴\
|
||||
Theta~Θ ThickSpace~ ThinSpace~ Tilde~∼ TildeEqual~≃ TildeFullEqual~≅ TildeTilde~≈ Topf~𝕋 TripleDot~⃛ Tscr~𝒯\
|
||||
Tstrok~Ŧ Uacute~Ú Uarr~↟ Uarrocir~⥉ Ubrcy~Ў Ubreve~Ŭ Ucirc~Û Ucy~У Udblac~Ű Ufr~𝔘 Ugrave~Ù Umacr~Ū UnderBar~_\
|
||||
UnderBrace~⏟ UnderBracket~⎵ UnderParenthesis~⏝ Union~⋃ UnionPlus~⊎ Uogon~Ų Uopf~𝕌 UpArrow~↑ UpArrowBar~⤒\
|
||||
UpArrowDownArrow~⇅ UpDownArrow~↕ UpEquilibrium~⥮ UpTee~⊥ UpTeeArrow~↥ Uparrow~⇑ Updownarrow~⇕ UpperLeftArrow~↖\
|
||||
UpperRightArrow~↗ Upsi~ϒ Upsilon~Υ Uring~Ů Uscr~𝒰 Utilde~Ũ Uuml~Ü VDash~⊫ Vbar~⫫ Vcy~В Vdash~⊩ Vdashl~⫦ Vee~⋁\
|
||||
Verbar~‖ Vert~‖ VerticalBar~∣ VerticalLine~| VerticalSeparator~❘ VerticalTilde~≀ VeryThinSpace~ Vfr~𝔙 Vopf~𝕍\
|
||||
Vscr~𝒱 Vvdash~⊪ Wcirc~Ŵ Wedge~⋀ Wfr~𝔚 Wopf~𝕎 Wscr~𝒲 Xfr~𝔛 Xi~Ξ Xopf~𝕏 Xscr~𝒳 YAcy~Я YIcy~Ї YUcy~Ю Yacute~Ý\
|
||||
Ycirc~Ŷ Ycy~Ы Yfr~𝔜 Yopf~𝕐 Yscr~𝒴 Yuml~Ÿ ZHcy~Ж Zacute~Ź Zcaron~Ž Zcy~З Zdot~Ż ZeroWidthSpace~ Zeta~Ζ Zfr~ℨ\
|
||||
Zopf~ℤ Zscr~𝒵 aacute~á abreve~ă ac~∾ acE~∾̳ acd~∿ acirc~â acute~´ acy~а aelig~æ af~ afr~𝔞 agrave~à alefsym~ℵ\
|
||||
aleph~ℵ alpha~α amacr~ā amalg~⨿ amp~& and~∧ andand~⩕ andd~⩜ andslope~⩘ andv~⩚ ang~∠ ange~⦤ angle~∠ angmsd~∡\
|
||||
angmsdaa~⦨ angmsdab~⦩ angmsdac~⦪ angmsdad~⦫ angmsdae~⦬ angmsdaf~⦭ angmsdag~⦮ angmsdah~⦯ angrt~∟ angrtvb~⊾\
|
||||
angrtvbd~⦝ angsph~∢ angst~Å angzarr~⍼ aogon~ą aopf~𝕒 ap~≈ apE~⩰ apacir~⩯ ape~≊ apid~≋ apos~\' approx~≈ approxeq~≊\
|
||||
aring~å ascr~𝒶 ast~* asymp~≈ asympeq~≍ atilde~ã auml~ä awconint~∳ awint~⨑ bNot~⫭ backcong~≌ backepsilon~϶\
|
||||
backprime~‵ backsim~∽ backsimeq~⋍ barvee~⊽ barwed~⌅ barwedge~⌅ bbrk~⎵ bbrktbrk~⎶ bcong~≌ bcy~б bdquo~„ becaus~∵\
|
||||
because~∵ bemptyv~⦰ bepsi~϶ bernou~ℬ beta~β beth~ℶ between~≬ bfr~𝔟 bigcap~⋂ bigcirc~◯ bigcup~⋃ bigodot~⨀\
|
||||
bigoplus~⨁ bigotimes~⨂ bigsqcup~⨆ bigstar~★ bigtriangledown~▽ bigtriangleup~△ biguplus~⨄ bigvee~⋁ bigwedge~⋀\
|
||||
bkarow~⤍ blacklozenge~⧫ blacksquare~▪ blacktriangle~▴ blacktriangledown~▾ blacktriangleleft~◂ blacktriangleright~▸\
|
||||
blank~␣ blk12~▒ blk14~░ blk34~▓ block~█ bne~=⃥ bnequiv~≡⃥ bnot~⌐ bopf~𝕓 bot~⊥ bottom~⊥ bowtie~⋈ boxDL~╗ boxDR~╔\
|
||||
boxDl~╖ boxDr~╓ boxH~═ boxHD~╦ boxHU~╩ boxHd~╤ boxHu~╧ boxUL~╝ boxUR~╚ boxUl~╜ boxUr~╙ boxV~║ boxVH~╬ boxVL~╣\
|
||||
boxVR~╠ boxVh~╫ boxVl~╢ boxVr~╟ boxbox~⧉ boxdL~╕ boxdR~╒ boxdl~┐ boxdr~┌ boxh~─ boxhD~╥ boxhU~╨ boxhd~┬ boxhu~┴\
|
||||
boxminus~⊟ boxplus~⊞ boxtimes~⊠ boxuL~╛ boxuR~╘ boxul~┘ boxur~└ boxv~│ boxvH~╪ boxvL~╡ boxvR~╞ boxvh~┼ boxvl~┤\
|
||||
boxvr~├ bprime~‵ breve~˘ brvbar~¦ bscr~𝒷 bsemi~⁏ bsim~∽ bsime~⋍ bsol~\\ bsolb~⧅ bsolhsub~⟈ bull~• bullet~• bump~≎\
|
||||
bumpE~⪮ bumpe~≏ bumpeq~≏ cacute~ć cap~∩ capand~⩄ capbrcup~⩉ capcap~⩋ capcup~⩇ capdot~⩀ caps~∩︀ caret~⁁ caron~ˇ\
|
||||
ccaps~⩍ ccaron~č ccedil~ç ccirc~ĉ ccups~⩌ ccupssm~⩐ cdot~ċ cedil~¸ cemptyv~⦲ cent~¢ centerdot~· cfr~𝔠 chcy~ч\
|
||||
check~✓ checkmark~✓ chi~χ cir~○ cirE~⧃ circ~ˆ circeq~≗ circlearrowleft~↺ circlearrowright~↻ circledR~® circledS~Ⓢ\
|
||||
circledast~⊛ circledcirc~⊚ circleddash~⊝ cire~≗ cirfnint~⨐ cirmid~⫯ cirscir~⧂ clubs~♣ clubsuit~♣ colon~: colone~≔\
|
||||
coloneq~≔ comma~, commat~@ comp~∁ compfn~∘ complement~∁ complexes~ℂ cong~≅ congdot~⩭ conint~∮ copf~𝕔 coprod~∐\
|
||||
copy~© copysr~℗ crarr~↵ cross~✗ cscr~𝒸 csub~⫏ csube~⫑ csup~⫐ csupe~⫒ ctdot~⋯ cudarrl~⤸ cudarrr~⤵ cuepr~⋞ cuesc~⋟\
|
||||
cularr~↶ cularrp~⤽ cup~∪ cupbrcap~⩈ cupcap~⩆ cupcup~⩊ cupdot~⊍ cupor~⩅ cups~∪︀ curarr~↷ curarrm~⤼ curlyeqprec~⋞\
|
||||
curlyeqsucc~⋟ curlyvee~⋎ curlywedge~⋏ curren~¤ curvearrowleft~↶ curvearrowright~↷ cuvee~⋎ cuwed~⋏ cwconint~∲\
|
||||
cwint~∱ cylcty~⌭ dArr~⇓ dHar~⥥ dagger~† daleth~ℸ darr~↓ dash~‐ dashv~⊣ dbkarow~⤏ dblac~˝ dcaron~ď dcy~д dd~ⅆ\
|
||||
ddagger~‡ ddarr~⇊ ddotseq~⩷ deg~° delta~δ demptyv~⦱ dfisht~⥿ dfr~𝔡 dharl~⇃ dharr~⇂ diam~⋄ diamond~⋄ diamondsuit~♦\
|
||||
diams~♦ die~¨ digamma~ϝ disin~⋲ div~÷ divide~÷ divideontimes~⋇ divonx~⋇ djcy~ђ dlcorn~⌞ dlcrop~⌍ dollar~$ dopf~𝕕\
|
||||
dot~˙ doteq~≐ doteqdot~≑ dotminus~∸ dotplus~∔ dotsquare~⊡ doublebarwedge~⌆ downarrow~↓ downdownarrows~⇊\
|
||||
downharpoonleft~⇃ downharpoonright~⇂ drbkarow~⤐ drcorn~⌟ drcrop~⌌ dscr~𝒹 dscy~ѕ dsol~⧶ dstrok~đ dtdot~⋱ dtri~▿\
|
||||
dtrif~▾ duarr~⇵ duhar~⥯ dwangle~⦦ dzcy~џ dzigrarr~⟿ eDDot~⩷ eDot~≑ eacute~é easter~⩮ ecaron~ě ecir~≖ ecirc~ê\
|
||||
ecolon~≕ ecy~э edot~ė ee~ⅇ efDot~≒ efr~𝔢 eg~⪚ egrave~è egs~⪖ egsdot~⪘ el~⪙ elinters~⏧ ell~ℓ els~⪕ elsdot~⪗\
|
||||
emacr~ē empty~∅ emptyset~∅ emptyv~∅ emsp~ emsp13~ emsp14~ eng~ŋ ensp~ eogon~ę eopf~𝕖 epar~⋕ eparsl~⧣ eplus~⩱\
|
||||
epsi~ε epsilon~ε epsiv~ϵ eqcirc~≖ eqcolon~≕ eqsim~≂ eqslantgtr~⪖ eqslantless~⪕ equals~= equest~≟ equiv~≡ equivDD~⩸\
|
||||
eqvparsl~⧥ erDot~≓ erarr~⥱ escr~ℯ esdot~≐ esim~≂ eta~η eth~ð euml~ë euro~€ excl~! exist~∃ expectation~ℰ\
|
||||
exponentiale~ⅇ fallingdotseq~≒ fcy~ф female~♀ ffilig~ffi fflig~ff ffllig~ffl ffr~𝔣 filig~fi fjlig~fj flat~♭ fllig~fl\
|
||||
fltns~▱ fnof~ƒ fopf~𝕗 forall~∀ fork~⋔ forkv~⫙ fpartint~⨍ frac12~½ frac13~⅓ frac14~¼ frac15~⅕ frac16~⅙ frac18~⅛\
|
||||
frac23~⅔ frac25~⅖ frac34~¾ frac35~⅗ frac38~⅜ frac45~⅘ frac56~⅚ frac58~⅝ frac78~⅞ frasl~⁄ frown~⌢ fscr~𝒻 gE~≧\
|
||||
gEl~⪌ gacute~ǵ gamma~γ gammad~ϝ gap~⪆ gbreve~ğ gcirc~ĝ gcy~г gdot~ġ ge~≥ gel~⋛ geq~≥ geqq~≧ geqslant~⩾ ges~⩾\
|
||||
gescc~⪩ gesdot~⪀ gesdoto~⪂ gesdotol~⪄ gesl~⋛︀ gesles~⪔ gfr~𝔤 gg~≫ ggg~⋙ gimel~ℷ gjcy~ѓ gl~≷ glE~⪒ gla~⪥ glj~⪤\
|
||||
gnE~≩ gnap~⪊ gnapprox~⪊ gne~⪈ gneq~⪈ gneqq~≩ gnsim~⋧ gopf~𝕘 grave~` gscr~ℊ gsim~≳ gsime~⪎ gsiml~⪐ gt~> gtcc~⪧\
|
||||
gtcir~⩺ gtdot~⋗ gtlPar~⦕ gtquest~⩼ gtrapprox~⪆ gtrarr~⥸ gtrdot~⋗ gtreqless~⋛ gtreqqless~⪌ gtrless~≷ gtrsim~≳\
|
||||
gvertneqq~≩︀ gvnE~≩︀ hArr~⇔ hairsp~ half~½ hamilt~ℋ hardcy~ъ harr~↔ harrcir~⥈ harrw~↭ hbar~ℏ hcirc~ĥ hearts~♥\
|
||||
heartsuit~♥ hellip~… hercon~⊹ hfr~𝔥 hksearow~⤥ hkswarow~⤦ hoarr~⇿ homtht~∻ hookleftarrow~↩ hookrightarrow~↪\
|
||||
hopf~𝕙 horbar~― hscr~𝒽 hslash~ℏ hstrok~ħ hybull~⁃ hyphen~‐ iacute~í ic~ icirc~î icy~и iecy~е iexcl~¡ iff~⇔\
|
||||
ifr~𝔦 igrave~ì ii~ⅈ iiiint~⨌ iiint~∭ iinfin~⧜ iiota~℩ ijlig~ij imacr~ī image~ℑ imagline~ℐ imagpart~ℑ imath~ı\
|
||||
imof~⊷ imped~Ƶ in~∈ incare~℅ infin~∞ infintie~⧝ inodot~ı int~∫ intcal~⊺ integers~ℤ intercal~⊺ intlarhk~⨗ intprod~⨼\
|
||||
iocy~ё iogon~į iopf~𝕚 iota~ι iprod~⨼ iquest~¿ iscr~𝒾 isin~∈ isinE~⋹ isindot~⋵ isins~⋴ isinsv~⋳ isinv~∈ it~\
|
||||
itilde~ĩ iukcy~і iuml~ï jcirc~ĵ jcy~й jfr~𝔧 jmath~ȷ jopf~𝕛 jscr~𝒿 jsercy~ј jukcy~є kappa~κ kappav~ϰ kcedil~ķ\
|
||||
kcy~к kfr~𝔨 kgreen~ĸ khcy~х kjcy~ќ kopf~𝕜 kscr~𝓀 lAarr~⇚ lArr~⇐ lAtail~⤛ lBarr~⤎ lE~≦ lEg~⪋ lHar~⥢ lacute~ĺ\
|
||||
laemptyv~⦴ lagran~ℒ lambda~λ lang~⟨ langd~⦑ langle~⟨ lap~⪅ laquo~« larr~← larrb~⇤ larrbfs~⤟ larrfs~⤝ larrhk~↩\
|
||||
larrlp~↫ larrpl~⤹ larrsim~⥳ larrtl~↢ lat~⪫ latail~⤙ late~⪭ lates~⪭︀ lbarr~⤌ lbbrk~❲ lbrace~{ lbrack~[ lbrke~⦋\
|
||||
lbrksld~⦏ lbrkslu~⦍ lcaron~ľ lcedil~ļ lceil~⌈ lcub~{ lcy~л ldca~⤶ ldquo~“ ldquor~„ ldrdhar~⥧ ldrushar~⥋ ldsh~↲\
|
||||
le~≤ leftarrow~← leftarrowtail~↢ leftharpoondown~↽ leftharpoonup~↼ leftleftarrows~⇇ leftrightarrow~↔\
|
||||
leftrightarrows~⇆ leftrightharpoons~⇋ leftrightsquigarrow~↭ leftthreetimes~⋋ leg~⋚ leq~≤ leqq~≦ leqslant~⩽ les~⩽\
|
||||
lescc~⪨ lesdot~⩿ lesdoto~⪁ lesdotor~⪃ lesg~⋚︀ lesges~⪓ lessapprox~⪅ lessdot~⋖ lesseqgtr~⋚ lesseqqgtr~⪋ lessgtr~≶\
|
||||
lesssim~≲ lfisht~⥼ lfloor~⌊ lfr~𝔩 lg~≶ lgE~⪑ lhard~↽ lharu~↼ lharul~⥪ lhblk~▄ ljcy~љ ll~≪ llarr~⇇ llcorner~⌞\
|
||||
llhard~⥫ lltri~◺ lmidot~ŀ lmoust~⎰ lmoustache~⎰ lnE~≨ lnap~⪉ lnapprox~⪉ lne~⪇ lneq~⪇ lneqq~≨ lnsim~⋦ loang~⟬\
|
||||
loarr~⇽ lobrk~⟦ longleftarrow~⟵ longleftrightarrow~⟷ longmapsto~⟼ longrightarrow~⟶ looparrowleft~↫\
|
||||
looparrowright~↬ lopar~⦅ lopf~𝕝 loplus~⨭ lotimes~⨴ lowast~∗ lowbar~_ loz~◊ lozenge~◊ lozf~⧫ lpar~( lparlt~⦓\
|
||||
lrarr~⇆ lrcorner~⌟ lrhar~⇋ lrhard~⥭ lrm~ lrtri~⊿ lsaquo~‹ lscr~𝓁 lsh~↰ lsim~≲ lsime~⪍ lsimg~⪏ lsqb~[ lsquo~‘\
|
||||
lsquor~‚ lstrok~ł lt~< ltcc~⪦ ltcir~⩹ ltdot~⋖ lthree~⋋ ltimes~⋉ ltlarr~⥶ ltquest~⩻ ltrPar~⦖ ltri~◃ ltrie~⊴ ltrif~◂\
|
||||
lurdshar~⥊ luruhar~⥦ lvertneqq~≨︀ lvnE~≨︀ mDDot~∺ macr~¯ male~♂ malt~✠ maltese~✠ map~↦ mapsto~↦ mapstodown~↧\
|
||||
mapstoleft~↤ mapstoup~↥ marker~▮ mcomma~⨩ mcy~м mdash~— measuredangle~∡ mfr~𝔪 mho~℧ micro~µ mid~∣ midast~*\
|
||||
midcir~⫰ middot~· minus~− minusb~⊟ minusd~∸ minusdu~⨪ mlcp~⫛ mldr~… mnplus~∓ models~⊧ mopf~𝕞 mp~∓ mscr~𝓂\
|
||||
mstpos~∾ mu~μ multimap~⊸ mumap~⊸ nGg~⋙̸ nGt~≫⃒ nGtv~≫̸ nLeftarrow~⇍ nLeftrightarrow~⇎ nLl~⋘̸ nLt~≪⃒ nLtv~≪̸\
|
||||
nRightarrow~⇏ nVDash~⊯ nVdash~⊮ nabla~∇ nacute~ń nang~∠⃒ nap~≉ napE~⩰̸ napid~≋̸ napos~ʼn napprox~≉ natur~♮\
|
||||
natural~♮ naturals~ℕ nbsp~ nbump~≎̸ nbumpe~≏̸ ncap~⩃ ncaron~ň ncedil~ņ ncong~≇ ncongdot~⩭̸ ncup~⩂ ncy~н ndash~–\
|
||||
ne~≠ neArr~⇗ nearhk~⤤ nearr~↗ nearrow~↗ nedot~≐̸ nequiv~≢ nesear~⤨ nesim~≂̸ nexist~∄ nexists~∄ nfr~𝔫 ngE~≧̸ nge~≱\
|
||||
ngeq~≱ ngeqq~≧̸ ngeqslant~⩾̸ nges~⩾̸ ngsim~≵ ngt~≯ ngtr~≯ nhArr~⇎ nharr~↮ nhpar~⫲ ni~∋ nis~⋼ nisd~⋺ niv~∋ njcy~њ\
|
||||
nlArr~⇍ nlE~≦̸ nlarr~↚ nldr~‥ nle~≰ nleftarrow~↚ nleftrightarrow~↮ nleq~≰ nleqq~≦̸ nleqslant~⩽̸ nles~⩽̸ nless~≮\
|
||||
nlsim~≴ nlt~≮ nltri~⋪ nltrie~⋬ nmid~∤ nopf~𝕟 not~¬ notin~∉ notinE~⋹̸ notindot~⋵̸ notinva~∉ notinvb~⋷ notinvc~⋶\
|
||||
notni~∌ notniva~∌ notnivb~⋾ notnivc~⋽ npar~∦ nparallel~∦ nparsl~⫽⃥ npart~∂̸ npolint~⨔ npr~⊀ nprcue~⋠ npre~⪯̸\
|
||||
nprec~⊀ npreceq~⪯̸ nrArr~⇏ nrarr~↛ nrarrc~⤳̸ nrarrw~↝̸ nrightarrow~↛ nrtri~⋫ nrtrie~⋭ nsc~⊁ nsccue~⋡ nsce~⪰̸\
|
||||
nscr~𝓃 nshortmid~∤ nshortparallel~∦ nsim~≁ nsime~≄ nsimeq~≄ nsmid~∤ nspar~∦ nsqsube~⋢ nsqsupe~⋣ nsub~⊄ nsubE~⫅̸\
|
||||
nsube~⊈ nsubset~⊂⃒ nsubseteq~⊈ nsubseteqq~⫅̸ nsucc~⊁ nsucceq~⪰̸ nsup~⊅ nsupE~⫆̸ nsupe~⊉ nsupset~⊃⃒ nsupseteq~⊉\
|
||||
nsupseteqq~⫆̸ ntgl~≹ ntilde~ñ ntlg~≸ ntriangleleft~⋪ ntrianglelefteq~⋬ ntriangleright~⋫ ntrianglerighteq~⋭ nu~ν\
|
||||
num~# numero~№ numsp~ nvDash~⊭ nvHarr~⤄ nvap~≍⃒ nvdash~⊬ nvge~≥⃒ nvgt~>⃒ nvinfin~⧞ nvlArr~⤂ nvle~≤⃒ nvlt~<⃒\
|
||||
nvltrie~⊴⃒ nvrArr~⤃ nvrtrie~⊵⃒ nvsim~∼⃒ nwArr~⇖ nwarhk~⤣ nwarr~↖ nwarrow~↖ nwnear~⤧ oS~Ⓢ oacute~ó oast~⊛ ocir~⊚\
|
||||
ocirc~ô ocy~о odash~⊝ odblac~ő odiv~⨸ odot~⊙ odsold~⦼ oelig~œ ofcir~⦿ ofr~𝔬 ogon~˛ ograve~ò ogt~⧁ ohbar~⦵ ohm~Ω\
|
||||
oint~∮ olarr~↺ olcir~⦾ olcross~⦻ oline~‾ olt~⧀ omacr~ō omega~ω omicron~ο omid~⦶ ominus~⊖ oopf~𝕠 opar~⦷ operp~⦹\
|
||||
oplus~⊕ or~∨ orarr~↻ ord~⩝ order~ℴ orderof~ℴ ordf~ª ordm~º origof~⊶ oror~⩖ orslope~⩗ orv~⩛ oscr~ℴ oslash~ø osol~⊘\
|
||||
otilde~õ otimes~⊗ otimesas~⨶ ouml~ö ovbar~⌽ par~∥ para~¶ parallel~∥ parsim~⫳ parsl~⫽ part~∂ pcy~п percnt~%\
|
||||
period~. permil~‰ perp~⊥ pertenk~‱ pfr~𝔭 phi~φ phiv~ϕ phmmat~ℳ phone~☎ pi~π pitchfork~⋔ piv~ϖ planck~ℏ planckh~ℎ\
|
||||
plankv~ℏ plus~+ plusacir~⨣ plusb~⊞ pluscir~⨢ plusdo~∔ plusdu~⨥ pluse~⩲ plusmn~± plussim~⨦ plustwo~⨧ pm~±\
|
||||
pointint~⨕ popf~𝕡 pound~£ pr~≺ prE~⪳ prap~⪷ prcue~≼ pre~⪯ prec~≺ precapprox~⪷ preccurlyeq~≼ preceq~⪯\
|
||||
precnapprox~⪹ precneqq~⪵ precnsim~⋨ precsim~≾ prime~′ primes~ℙ prnE~⪵ prnap~⪹ prnsim~⋨ prod~∏ profalar~⌮\
|
||||
profline~⌒ profsurf~⌓ prop~∝ propto~∝ prsim~≾ prurel~⊰ pscr~𝓅 psi~ψ puncsp~ qfr~𝔮 qint~⨌ qopf~𝕢 qprime~⁗\
|
||||
qscr~𝓆 quaternions~ℍ quatint~⨖ quest~? questeq~≟ quot~" rAarr~⇛ rArr~⇒ rAtail~⤜ rBarr~⤏ rHar~⥤ race~∽̱ racute~ŕ\
|
||||
radic~√ raemptyv~⦳ rang~⟩ rangd~⦒ range~⦥ rangle~⟩ raquo~» rarr~→ rarrap~⥵ rarrb~⇥ rarrbfs~⤠ rarrc~⤳ rarrfs~⤞\
|
||||
rarrhk~↪ rarrlp~↬ rarrpl~⥅ rarrsim~⥴ rarrtl~↣ rarrw~↝ ratail~⤚ ratio~∶ rationals~ℚ rbarr~⤍ rbbrk~❳ rbrace~}\
|
||||
rbrack~] rbrke~⦌ rbrksld~⦎ rbrkslu~⦐ rcaron~ř rcedil~ŗ rceil~⌉ rcub~} rcy~р rdca~⤷ rdldhar~⥩ rdquo~” rdquor~”\
|
||||
rdsh~↳ real~ℜ realine~ℛ realpart~ℜ reals~ℝ rect~▭ reg~® rfisht~⥽ rfloor~⌋ rfr~𝔯 rhard~⇁ rharu~⇀ rharul~⥬ rho~ρ\
|
||||
rhov~ϱ rightarrow~→ rightarrowtail~↣ rightharpoondown~⇁ rightharpoonup~⇀ rightleftarrows~⇄ rightleftharpoons~⇌\
|
||||
rightrightarrows~⇉ rightsquigarrow~↝ rightthreetimes~⋌ ring~˚ risingdotseq~≓ rlarr~⇄ rlhar~⇌ rlm~ rmoust~⎱\
|
||||
rmoustache~⎱ rnmid~⫮ roang~⟭ roarr~⇾ robrk~⟧ ropar~⦆ ropf~𝕣 roplus~⨮ rotimes~⨵ rpar~) rpargt~⦔ rppolint~⨒ rrarr~⇉\
|
||||
rsaquo~› rscr~𝓇 rsh~↱ rsqb~] rsquo~’ rsquor~’ rthree~⋌ rtimes~⋊ rtri~▹ rtrie~⊵ rtrif~▸ rtriltri~⧎ ruluhar~⥨ rx~℞\
|
||||
sacute~ś sbquo~‚ sc~≻ scE~⪴ scap~⪸ scaron~š sccue~≽ sce~⪰ scedil~ş scirc~ŝ scnE~⪶ scnap~⪺ scnsim~⋩ scpolint~⨓\
|
||||
scsim~≿ scy~с sdot~⋅ sdotb~⊡ sdote~⩦ seArr~⇘ searhk~⤥ searr~↘ searrow~↘ sect~§ semi~; seswar~⤩ setminus~∖ setmn~∖\
|
||||
sext~✶ sfr~𝔰 sfrown~⌢ sharp~♯ shchcy~щ shcy~ш shortmid~∣ shortparallel~∥ shy~ sigma~σ sigmaf~ς sigmav~ς sim~∼\
|
||||
simdot~⩪ sime~≃ simeq~≃ simg~⪞ simgE~⪠ siml~⪝ simlE~⪟ simne~≆ simplus~⨤ simrarr~⥲ slarr~← smallsetminus~∖ smashp~⨳\
|
||||
smeparsl~⧤ smid~∣ smile~⌣ smt~⪪ smte~⪬ smtes~⪬︀ softcy~ь sol~/ solb~⧄ solbar~⌿ sopf~𝕤 spades~♠ spadesuit~♠ spar~∥\
|
||||
sqcap~⊓ sqcaps~⊓︀ sqcup~⊔ sqcups~⊔︀ sqsub~⊏ sqsube~⊑ sqsubset~⊏ sqsubseteq~⊑ sqsup~⊐ sqsupe~⊒ sqsupset~⊐\
|
||||
sqsupseteq~⊒ squ~□ square~□ squarf~▪ squf~▪ srarr~→ sscr~𝓈 ssetmn~∖ ssmile~⌣ sstarf~⋆ star~☆ starf~★\
|
||||
straightepsilon~ϵ straightphi~ϕ strns~¯ sub~⊂ subE~⫅ subdot~⪽ sube~⊆ subedot~⫃ submult~⫁ subnE~⫋ subne~⊊ subplus~⪿\
|
||||
subrarr~⥹ subset~⊂ subseteq~⊆ subseteqq~⫅ subsetneq~⊊ subsetneqq~⫋ subsim~⫇ subsub~⫕ subsup~⫓ succ~≻ succapprox~⪸\
|
||||
succcurlyeq~≽ succeq~⪰ succnapprox~⪺ succneqq~⪶ succnsim~⋩ succsim~≿ sum~∑ sung~♪ sup~⊃ sup1~¹ sup2~² sup3~³\
|
||||
supE~⫆ supdot~⪾ supdsub~⫘ supe~⊇ supedot~⫄ suphsol~⟉ suphsub~⫗ suplarr~⥻ supmult~⫂ supnE~⫌ supne~⊋ supplus~⫀\
|
||||
supset~⊃ supseteq~⊇ supseteqq~⫆ supsetneq~⊋ supsetneqq~⫌ supsim~⫈ supsub~⫔ supsup~⫖ swArr~⇙ swarhk~⤦ swarr~↙\
|
||||
swarrow~↙ swnwar~⤪ szlig~ß target~⌖ tau~τ tbrk~⎴ tcaron~ť tcedil~ţ tcy~т tdot~⃛ telrec~⌕ tfr~𝔱 there4~∴\
|
||||
therefore~∴ theta~θ thetasym~ϑ thetav~ϑ thickapprox~≈ thicksim~∼ thinsp~ thkap~≈ thksim~∼ thorn~þ tilde~˜ times~×\
|
||||
timesb~⊠ timesbar~⨱ timesd~⨰ tint~∭ toea~⤨ top~⊤ topbot~⌶ topcir~⫱ topf~𝕥 topfork~⫚ tosa~⤩ tprime~‴ trade~™\
|
||||
triangle~▵ triangledown~▿ triangleleft~◃ trianglelefteq~⊴ triangleq~≜ triangleright~▹ trianglerighteq~⊵ tridot~◬\
|
||||
trie~≜ triminus~⨺ triplus~⨹ trisb~⧍ tritime~⨻ trpezium~⏢ tscr~𝓉 tscy~ц tshcy~ћ tstrok~ŧ twixt~≬\
|
||||
twoheadleftarrow~↞ twoheadrightarrow~↠ uArr~⇑ uHar~⥣ uacute~ú uarr~↑ ubrcy~ў ubreve~ŭ ucirc~û ucy~у udarr~⇅\
|
||||
udblac~ű udhar~⥮ ufisht~⥾ ufr~𝔲 ugrave~ù uharl~↿ uharr~↾ uhblk~▀ ulcorn~⌜ ulcorner~⌜ ulcrop~⌏ ultri~◸ umacr~ū\
|
||||
uml~¨ uogon~ų uopf~𝕦 uparrow~↑ updownarrow~↕ upharpoonleft~↿ upharpoonright~↾ uplus~⊎ upsi~υ upsih~ϒ upsilon~υ\
|
||||
upuparrows~⇈ urcorn~⌝ urcorner~⌝ urcrop~⌎ uring~ů urtri~◹ uscr~𝓊 utdot~⋰ utilde~ũ utri~▵ utrif~▴ uuarr~⇈ uuml~ü\
|
||||
uwangle~⦧ vArr~⇕ vBar~⫨ vBarv~⫩ vDash~⊨ vangrt~⦜ varepsilon~ϵ varkappa~ϰ varnothing~∅ varphi~ϕ varpi~ϖ varpropto~∝\
|
||||
varr~↕ varrho~ϱ varsigma~ς varsubsetneq~⊊︀ varsubsetneqq~⫋︀ varsupsetneq~⊋︀ varsupsetneqq~⫌︀ vartheta~ϑ\
|
||||
vartriangleleft~⊲ vartriangleright~⊳ vcy~в vdash~⊢ vee~∨ veebar~⊻ veeeq~≚ vellip~⋮ verbar~| vert~| vfr~𝔳 vltri~⊲\
|
||||
vnsub~⊂⃒ vnsup~⊃⃒ vopf~𝕧 vprop~∝ vrtri~⊳ vscr~𝓋 vsubnE~⫋︀ vsubne~⊊︀ vsupnE~⫌︀ vsupne~⊋︀ vzigzag~⦚ wcirc~ŵ\
|
||||
wedbar~⩟ wedge~∧ wedgeq~≙ weierp~℘ wfr~𝔴 wopf~𝕨 wp~℘ wr~≀ wreath~≀ wscr~𝓌 xcap~⋂ xcirc~◯ xcup~⋃ xdtri~▽ xfr~𝔵\
|
||||
xhArr~⟺ xharr~⟷ xi~ξ xlArr~⟸ xlarr~⟵ xmap~⟼ xnis~⋻ xodot~⨀ xopf~𝕩 xoplus~⨁ xotime~⨂ xrArr~⟹ xrarr~⟶ xscr~𝓍\
|
||||
xsqcup~⨆ xuplus~⨄ xutri~△ xvee~⋁ xwedge~⋀ yacute~ý yacy~я ycirc~ŷ ycy~ы yen~¥ yfr~𝔶 yicy~ї yopf~𝕪 yscr~𝓎 yucy~ю\
|
||||
yuml~ÿ zacute~ź zcaron~ž zcy~з zdot~ż zeetrf~ℨ zeta~ζ zfr~𝔷 zhcy~ж zigrarr~⇝ zopf~𝕫 zscr~𝓏 zwj~ zwnj~'
|
||||
|
||||
const namedReferences = new Map(packedReferences.split(/ +/).map(namedReference))
|
||||
|
||||
export function holdsEntityReference(text: string): boolean {
|
||||
for (let index = text.indexOf('&'); index !== -1; index = text.indexOf('&', index + 1)) {
|
||||
if (readEntityReference(text, index) !== undefined) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// The reference standing at `index`, `undefined` where none does.
|
||||
export function readEntityReference(text: string, index: number): EntityReference | undefined {
|
||||
anchoredEntityReference.lastIndex = index
|
||||
const reference = anchoredEntityReference.exec(text)?.[0]
|
||||
if (reference === undefined) return undefined
|
||||
const decoded = decodeReference(reference)
|
||||
return decoded === undefined ? undefined : { length: reference.length, text: decoded }
|
||||
}
|
||||
|
||||
function decodeReference(reference: string): string | undefined {
|
||||
const decimal = decimalReference.exec(reference)?.[1]
|
||||
if (decimal !== undefined) return characterOf(Number.parseInt(decimal, 10))
|
||||
const hexadecimal = hexadecimalReference.exec(reference)?.[1]
|
||||
if (hexadecimal !== undefined) return characterOf(Number.parseInt(hexadecimal, 16))
|
||||
return namedReferences.get(reference.slice(1, -1))
|
||||
}
|
||||
|
||||
function characterOf(codePoint: number): string {
|
||||
if (codePoint === 0 || codePoint > largestCodePoint) return replacementCharacter
|
||||
if (codePoint >= surrogates.first && codePoint <= surrogates.last) return replacementCharacter
|
||||
return String.fromCodePoint(codePoint)
|
||||
}
|
||||
|
||||
function namedReference(entry: string): [string, string] {
|
||||
const separator = entry.indexOf('~')
|
||||
return [entry.slice(0, separator), entry.slice(separator + 1)]
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
import { readEntityReference, replacementCharacter } from './entity-references.ts'
|
||||
|
||||
export type LinePosition = 'first' | 'later'
|
||||
|
||||
// The suffix lengths of a text that spell a thematic break, and the marker each of them opens with.
|
||||
export type ThematicBreakTail = { longest: number; marker: string; shortest: number }
|
||||
|
||||
type OpenHtmlBlock = { closer: RegExp | undefined; construct: string }
|
||||
|
||||
type HtmlBlockCondition = { closer: RegExp | undefined; construct: string | undefined; interrupts: boolean; start: RegExp }
|
||||
|
||||
const htmlConstructNames = {
|
||||
cdata: 'a CDATA section',
|
||||
comment: 'an HTML comment',
|
||||
declaration: 'an HTML declaration',
|
||||
processingInstruction: 'an HTML processing instruction',
|
||||
}
|
||||
|
||||
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-]*'
|
||||
const htmlSpaceSource = '[ \\t\\n]'
|
||||
const attributeSource = `(?:${htmlSpaceSource}+[A-Za-z_:][A-Za-z0-9_.:-]*(?:${htmlSpaceSource}*=${htmlSpaceSource}*(?:[^ \\t\\n"'=<>\`]+|'[^']*'|"[^"]*"))?)`
|
||||
|
||||
const htmlTagSource = `(?:<${tagNameSource}${attributeSource}*${htmlSpaceSource}*/?>|</${tagNameSource}${htmlSpaceSource}*>)`
|
||||
|
||||
const autolink = new RegExp(`^(?:${autolinkSource})$`)
|
||||
const bracketedAutolink = new RegExp(`<(?:${autolinkSource})>`, 'y')
|
||||
const controlCharacter = new RegExp(`[${controlCharacterRange}]`)
|
||||
const htmlTag = new RegExp(htmlTagSource, 'y')
|
||||
const nullCharacter = new RegExp(nullCharacterSource)
|
||||
const nullCharacters = new RegExp(nullCharacterSource, 'g')
|
||||
const tagName = new RegExp(`^</?(${tagNameSource})[\\s\\S]*$`)
|
||||
// The opener's own match ends with the terminator where the construct is complete on its own (`<!-->`).
|
||||
const inlineHtmlConstructs = [
|
||||
{ name: htmlConstructNames.cdata, opener: /<!\[CDATA\[/y, terminator: ']]>' },
|
||||
{ name: htmlConstructNames.comment, opener: /<!(?:--->|-->|--)/y, terminator: '-->' },
|
||||
{ name: htmlConstructNames.declaration, opener: /<![A-Za-z]/y, terminator: '>' },
|
||||
{ name: htmlConstructNames.processingInstruction, opener: /<\?/y, terminator: '?>' },
|
||||
]
|
||||
// CommonMark 0.31.2, HTML blocks: the tag names start condition 6 lists.
|
||||
const blockTagNames =
|
||||
'address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul'
|
||||
const completeTag = new RegExp(`^${htmlTagSource}[ \\t]*$`)
|
||||
const htmlBlockConditions: HtmlBlockCondition[] = [
|
||||
{ closer: /<\/(?:pre|script|style|textarea)>/i, construct: undefined, interrupts: true, start: /^<(?:pre|script|style|textarea)(?:[ \t>]|$)/i },
|
||||
{ closer: /-->/, construct: htmlConstructNames.comment, interrupts: true, start: /^<!--/ },
|
||||
{ closer: /\?>/, construct: htmlConstructNames.processingInstruction, interrupts: true, start: /^<\?/ },
|
||||
{ closer: />/, construct: htmlConstructNames.declaration, interrupts: true, start: /^<![A-Za-z]/ },
|
||||
{ closer: /\]\]>/, construct: htmlConstructNames.cdata, interrupts: true, start: /^<!\[CDATA\[/ },
|
||||
{ closer: undefined, construct: undefined, interrupts: true, start: new RegExp(`^</?(?:${blockTagNames})(?:[ \\t>]|/>|$)`, 'i') },
|
||||
{ closer: undefined, construct: undefined, interrupts: false, start: completeTag },
|
||||
]
|
||||
const asciiPunctuation = /[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]/
|
||||
const atxHeadingOpener = /^(#{1,6})(?:[ \t]|$)/
|
||||
const blankLine = /^[ \t]*$/
|
||||
const breakMarkers = '*-_'
|
||||
const codeFenceOpener = /^(`{3,}|~{3,})/
|
||||
const pipeClaim = /^\|/
|
||||
const bulletListOpener = /^[*+-](?:[ \t]|$)/
|
||||
// A superset of what the parser claims: over-escaping a line is safe, under-escaping one breaks the round-trip.
|
||||
const firstCharacterOpeners = [atxHeadingOpener, /^>/, bulletListOpener, codeFenceOpener, pipeClaim]
|
||||
// CommonMark 0.31.2, Autolinks: the email production, whose label may not open or close with a hyphen.
|
||||
const emailNameSource = "[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+"
|
||||
const emailLabelSource = '[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?'
|
||||
const emailAutolink = new RegExp(`<${emailNameSource}@${emailLabelSource}(?:\\.${emailLabelSource})*>`, 'y')
|
||||
const orderedListOpener = /^(\d{1,9})(?:[.)])(?:[ \t]|$)/
|
||||
const setextUnderline = /^(=+|-+)[ \t]*$/
|
||||
const unicodeWhitespace = /[\t\n\f\r \p{Zs}]/u
|
||||
|
||||
export function atxHeading(line: string): { level: number; text: string } | undefined {
|
||||
const hashes = atxHeadingOpener.exec(line)?.[1]
|
||||
if (hashes === undefined) return undefined
|
||||
const text = trimSpace(line.slice(hashes.length))
|
||||
return { level: hashes.length, text: trimSpace(text.replace(/(?:^|(?<=[ \t]))#+$/, '')) }
|
||||
}
|
||||
|
||||
// The character a backslash escapes at `index`, `undefined` where the backslash is literal text.
|
||||
export function backslashEscape(text: string, index: number): string | undefined {
|
||||
if (text.charAt(index) !== '\\') return undefined
|
||||
const escaped = text.charAt(index + 1)
|
||||
return isAsciiPunctuation(escaped) ? escaped : undefined
|
||||
}
|
||||
|
||||
export function claimsLine(line: string, position: LinePosition): boolean {
|
||||
return escapesLineClaim(line, 0, position) || orderedListOpener.test(line)
|
||||
}
|
||||
|
||||
export function claimsPipeLine(line: string): boolean {
|
||||
return pipeClaim.test(line)
|
||||
}
|
||||
|
||||
export function closingCodeFence(line: string, marker: string): boolean {
|
||||
const closing = codeFenceOpener.exec(line)?.[1]
|
||||
if (closing === undefined || closing.charAt(0) !== marker.charAt(0) || closing.length < marker.length) return false
|
||||
return /^[ \t]*$/.test(line.slice(closing.length))
|
||||
}
|
||||
|
||||
export function decodeTextEscapes(text: string): string {
|
||||
let decoded = ''
|
||||
let index = 0
|
||||
while (index < text.length) {
|
||||
const escaped = backslashEscape(text, index)
|
||||
if (escaped !== undefined) {
|
||||
decoded += escaped
|
||||
index += 2
|
||||
continue
|
||||
}
|
||||
const reference = readEntityReference(text, index)
|
||||
if (reference !== undefined) {
|
||||
decoded += reference.text
|
||||
index += reference.length
|
||||
continue
|
||||
}
|
||||
decoded += text.charAt(index)
|
||||
index += 1
|
||||
}
|
||||
return decoded
|
||||
}
|
||||
|
||||
export function escapesLineClaim(line: string, offset: number, position: LinePosition): boolean {
|
||||
if (offset === 0) {
|
||||
if (firstCharacterOpeners.some((opener) => opener.test(line)) || isThematicBreak(line)) return true
|
||||
if (openingHtmlBlock(line, position === 'later') !== undefined) return true
|
||||
return position === 'later' && setextUnderline.test(line)
|
||||
}
|
||||
const digits = orderedListOpener.exec(line)?.[1]
|
||||
return digits !== undefined && offset === digits.length
|
||||
}
|
||||
|
||||
export function holdsControlCharacter(text: string): boolean {
|
||||
return controlCharacter.test(text)
|
||||
}
|
||||
|
||||
export function holdsNullCharacter(text: string): boolean {
|
||||
return nullCharacter.test(text)
|
||||
}
|
||||
|
||||
export function replaceNullCharacters(text: string): string {
|
||||
return text.replaceAll(nullCharacters, replacementCharacter)
|
||||
}
|
||||
|
||||
function htmlTagName(text: string): string {
|
||||
return text.replace(tagName, '<$1>')
|
||||
}
|
||||
|
||||
export function inlineHtmlConstruct(text: string, index: number): 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
|
||||
}
|
||||
htmlTag.lastIndex = index
|
||||
const tag = htmlTag.exec(text)?.[0]
|
||||
return tag === undefined ? undefined : htmlTagName(tag)
|
||||
}
|
||||
|
||||
export function isAsciiPunctuation(character: string): boolean {
|
||||
return asciiPunctuation.test(character)
|
||||
}
|
||||
|
||||
export function isAutolink(text: string): boolean {
|
||||
return autolink.test(text)
|
||||
}
|
||||
|
||||
export function isBlankLine(line: string): boolean {
|
||||
return blankLine.test(line)
|
||||
}
|
||||
|
||||
export function isThematicBreak(line: string): boolean {
|
||||
return holdsThematicBreak(thematicBreakTail(line), line)
|
||||
}
|
||||
|
||||
export function thematicBreakTail(text: string): ThematicBreakTail | undefined {
|
||||
let marker: string | undefined
|
||||
let markers = 0
|
||||
let first = text.length
|
||||
let third = text.length
|
||||
let cursor = text.length
|
||||
while (cursor > 0) {
|
||||
const character = text.charAt(cursor - 1)
|
||||
if (marker === undefined ? breakMarkers.includes(character) : character === marker) {
|
||||
marker = character
|
||||
markers += 1
|
||||
first = cursor - 1
|
||||
if (markers === 3) third = cursor - 1
|
||||
} else if (!spaceOrTab(character)) break
|
||||
cursor -= 1
|
||||
}
|
||||
if (marker === undefined || markers < 3) return undefined
|
||||
return { longest: text.length - first, marker, shortest: text.length - third }
|
||||
}
|
||||
|
||||
// `text` is a suffix of what `tail` was read from, or opens with a space: a length alone names a
|
||||
// suffix, and the marker check is what refuses one starting mid-run — `--- ---` holds ` ---`.
|
||||
export function holdsThematicBreak(tail: ThematicBreakTail | undefined, text: string): boolean {
|
||||
if (tail === undefined) return false
|
||||
return text.length >= tail.shortest && text.length <= tail.longest && text.charAt(0) === tail.marker
|
||||
}
|
||||
|
||||
export function isUnicodeWhitespace(character: string): boolean {
|
||||
return unicodeWhitespace.test(character)
|
||||
}
|
||||
|
||||
// `start` is the list's first number, `undefined` for a bullet.
|
||||
export function listMarker(line: string): { start: number | undefined; width: number } | undefined {
|
||||
const ordered = orderedListOpener.exec(line)
|
||||
if (ordered !== null) {
|
||||
const digits = ordered[1] ?? ''
|
||||
return { start: Number(digits), width: digits.length + 1 }
|
||||
}
|
||||
return bulletListOpener.test(line) ? { start: undefined, width: 1 } : undefined
|
||||
}
|
||||
|
||||
export function markerInterruptsParagraph(start: number | undefined, empty: boolean): boolean {
|
||||
return !empty && (start === undefined || start === 1)
|
||||
}
|
||||
|
||||
export function openingCodeFence(line: string): { info: string; marker: string } | undefined {
|
||||
const marker = codeFenceOpener.exec(line)?.[1]
|
||||
if (marker === undefined) return undefined
|
||||
const info = trimSpace(line.slice(marker.length))
|
||||
return marker.startsWith('`') && info.includes('`') ? undefined : { info, marker }
|
||||
}
|
||||
|
||||
export function openingHtmlBlock(line: string, interrupting: boolean): OpenHtmlBlock | undefined {
|
||||
for (const condition of htmlBlockConditions) {
|
||||
if ((interrupting && !condition.interrupts) || !condition.start.test(line)) continue
|
||||
return { closer: condition.closer, construct: condition.construct ?? htmlTagName(line) }
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function opensBracketedAutolink(text: string, index: number): boolean {
|
||||
return readBracketedAutolink(text, index) !== undefined
|
||||
}
|
||||
|
||||
export function opensEmailAutolink(text: string, index: number): boolean {
|
||||
return readEmailAutolink(text, index) !== undefined
|
||||
}
|
||||
|
||||
export function readBracketedAutolink(text: string, index: number): number | undefined {
|
||||
bracketedAutolink.lastIndex = index
|
||||
return bracketedAutolink.exec(text)?.[0].length
|
||||
}
|
||||
|
||||
export function readEmailAutolink(text: string, index: number): number | undefined {
|
||||
emailAutolink.lastIndex = index
|
||||
return emailAutolink.exec(text)?.[0].length
|
||||
}
|
||||
|
||||
export function setextHeadingLevel(line: string): number | undefined {
|
||||
const underline = setextUnderline.exec(line)?.[1]
|
||||
if (underline === undefined) return undefined
|
||||
return underline.startsWith('=') ? 1 : 2
|
||||
}
|
||||
|
||||
function spaceOrTab(character: string): boolean {
|
||||
return character === ' ' || character === '\t'
|
||||
}
|
||||
|
||||
export function trimSpace(text: string): string {
|
||||
let start = 0
|
||||
while (start < text.length && spaceOrTab(text.charAt(start))) start += 1
|
||||
return trimTrailingSpace(text.slice(start))
|
||||
}
|
||||
|
||||
export function trimTrailingSpace(text: string): string {
|
||||
let end = text.length
|
||||
while (end > 0 && spaceOrTab(text.charAt(end - 1))) end -= 1
|
||||
return text.slice(0, end)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { LinkDefinition, LinkPart } from './link-syntax.ts'
|
||||
import { normalizeLabel, readDestination, readLabel, readTitle, skipLinkWhitespace } from './link-syntax.ts'
|
||||
|
||||
type ReadDefinition = { definition: LinkDefinition; label: string; length: number }
|
||||
|
||||
const restOfLine = /^[ \t]*(?:\n|$)/
|
||||
|
||||
export function opensLinkDefinition(text: string): boolean {
|
||||
return readDefinition(text) !== undefined
|
||||
}
|
||||
|
||||
export function readLinkDefinitions(definitions: Map<string, LinkDefinition>, text: string): string {
|
||||
let rest = text
|
||||
let read = readDefinition(rest)
|
||||
while (read !== undefined) {
|
||||
if (!definitions.has(read.label)) definitions.set(read.label, read.definition)
|
||||
rest = rest.slice(read.length)
|
||||
read = readDefinition(rest)
|
||||
}
|
||||
return rest
|
||||
}
|
||||
|
||||
function readDefinition(text: string): ReadDefinition | undefined {
|
||||
const label = readLabel(text, 0)
|
||||
if (label === undefined || text.charAt(label.length) !== ':') return undefined
|
||||
const name = normalizeLabel(label.value)
|
||||
if (name === '') return undefined
|
||||
const afterLabel = skipLinkWhitespace(text, label.length + 1)
|
||||
const destination = readDestination(text, afterLabel)
|
||||
if (destination === undefined) return undefined
|
||||
const afterDestination = afterLabel + destination.length
|
||||
const titled = readTitledEnd(text, afterDestination)
|
||||
if (titled !== undefined) return { definition: { destination: destination.value, title: titled.value }, label: name, length: titled.length }
|
||||
const plain = endOfLine(text, afterDestination)
|
||||
if (plain === undefined) return undefined
|
||||
return { definition: { destination: destination.value }, label: name, length: plain }
|
||||
}
|
||||
|
||||
function readTitledEnd(text: string, offset: number): LinkPart | undefined {
|
||||
const afterSpace = skipLinkWhitespace(text, offset)
|
||||
if (afterSpace === offset) return undefined
|
||||
const title = readTitle(text, afterSpace)
|
||||
if (title === undefined) return undefined
|
||||
const end = endOfLine(text, afterSpace + title.length)
|
||||
return end === undefined ? undefined : { length: end, value: title.value }
|
||||
}
|
||||
|
||||
function endOfLine(text: string, offset: number): number | undefined {
|
||||
const rest = restOfLine.exec(text.slice(offset))?.[0]
|
||||
return rest === undefined ? undefined : offset + rest.length
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { backslashEscape, decodeTextEscapes, holdsControlCharacter } from './grammar.ts'
|
||||
import { holdsEntityReference } from './entity-references.ts'
|
||||
|
||||
export type LinkDefinition = { destination: string; title?: string }
|
||||
|
||||
export type LinkPart = { length: number; value: string }
|
||||
|
||||
const bracketedDestination = /<((?:[^\n<>\\]|\\[^\n])*)>/y
|
||||
const linkLabel = /\[((?:[^[\]\\]|\\[\s\S]){0,999})\]/y
|
||||
const titleClosers: Readonly<Record<string, string>> = { '"': '"', "'": "'", '(': ')' }
|
||||
|
||||
// The label is matched by its normalized raw text, escapes and all.
|
||||
export function readLabel(text: string, offset: number): LinkPart | undefined {
|
||||
linkLabel.lastIndex = offset
|
||||
const value = linkLabel.exec(text)?.[1]
|
||||
return value === undefined ? undefined : { length: value.length + 2, value }
|
||||
}
|
||||
|
||||
export function normalizeLabel(raw: string): string {
|
||||
const collapsed = raw.replace(/[ \t\n]+/g, ' ')
|
||||
const start = collapsed.startsWith(' ') ? 1 : 0
|
||||
const end = collapsed.endsWith(' ') ? collapsed.length - 1 : collapsed.length
|
||||
return collapsed.slice(start, end).toLowerCase()
|
||||
}
|
||||
|
||||
export function readDestination(text: string, offset: number): LinkPart | undefined {
|
||||
bracketedDestination.lastIndex = offset
|
||||
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)) }
|
||||
}
|
||||
|
||||
// `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
|
||||
}
|
||||
const afterDestination = index
|
||||
index = skipLinkWhitespace(text, index)
|
||||
const title = index > afterDestination ? 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 }
|
||||
}
|
||||
|
||||
export function readTitle(text: string, offset: number): LinkPart | undefined {
|
||||
const opener = text.charAt(offset)
|
||||
const closer = titleClosers[opener]
|
||||
if (closer === undefined) return undefined
|
||||
let index = offset + 1
|
||||
while (index < text.length) {
|
||||
const character = text.charAt(index)
|
||||
if (backslashEscape(text, index) !== undefined) {
|
||||
index += 2
|
||||
continue
|
||||
}
|
||||
if (character === closer) return { length: index + 1 - offset, value: decodeTextEscapes(text.slice(offset + 1, index)) }
|
||||
if (character === opener) return undefined
|
||||
index += 1
|
||||
}
|
||||
return 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
|
||||
}
|
||||
|
||||
export function spellDestination(href: string): string | undefined {
|
||||
if (holdsControlCharacter(href) || href.includes('\\') || holdsEntityReference(href)) return undefined
|
||||
if (href.includes(' ')) return /[<>]/.test(href) ? undefined : `<${href}>`
|
||||
return href.startsWith('<') ? undefined : escapeUnbalanced(href, '(', ')')
|
||||
}
|
||||
|
||||
export function spellLinkTarget(href: string, title: string | undefined): string | undefined {
|
||||
const destination = spellDestination(href)
|
||||
if (destination === undefined || title === undefined) return destination
|
||||
const spelledTitle = spellTitle(title)
|
||||
return spelledTitle === undefined ? undefined : `${destination === '' ? '<>' : destination}${spelledTitle}`
|
||||
}
|
||||
|
||||
export function escapeUnbalanced(spelling: string, opener: string, closer: string): string {
|
||||
const open: number[] = []
|
||||
const unbalanced = new Set<number>()
|
||||
for (let index = 0; index < spelling.length; index += backslashEscape(spelling, index) === undefined ? 1 : 2) {
|
||||
const character = spelling.charAt(index)
|
||||
if (character === opener) open.push(index)
|
||||
if (character === closer && open.pop() === undefined) unbalanced.add(index)
|
||||
}
|
||||
for (const index of open) unbalanced.add(index)
|
||||
let spelled = ''
|
||||
for (let index = 0; index < spelling.length; index += 1) spelled += (unbalanced.has(index) ? '\\' : '') + spelling.charAt(index)
|
||||
return spelled
|
||||
}
|
||||
|
||||
function spellTitle(title: string): string | undefined {
|
||||
return /[\n\r\\]/.test(title) || holdsEntityReference(title) ? undefined : ` "${title.replaceAll('"', '\\"')}"`
|
||||
}
|
||||
Reference in New Issue
Block a user