Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248// CommonMark 0.31.2 inline parser (a port of commonmark.js's delimiter-stack and bracket-stack algorithm),// extended with GFM strikethrough and extended autolinks, and OpenCode's `$…$`, `$$…$$`, and `\(…\)` math.import { decodeEntity, ENTITY_PATTERN, ESCAPABLE, normalizeURI, unescapeString } from "./entities"import type { InlineNode, MathDelimiter } from "./types"
export { healStreamingInline } from "./heal"
export interface ReferenceDef { href: string title?: string /** Source text of the definition (container prefixes stripped), for renderers that re-parse a block on its own */ raw?: string /** 1-based start line of the paragraph that defined this reference */ line?: number}
export type RefMap = Map<string, ReferenceDef>
export interface InlineParseOptions { gfm?: boolean math?: boolean defs?: ReadonlyMap<string, ReferenceDef> unresolvedRefs?: Set<string> usedRefs?: Set<string>}
const C_NEWLINE = 10const C_ASTERISK = 42const C_UNDERSCORE = 95const C_BACKTICK = 96const C_OPEN_BRACKET = 91const C_CLOSE_BRACKET = 93const C_LESSTHAN = 60const C_BANG = 33const C_BACKSLASH = 92const C_AMPERSAND = 38const C_OPEN_PAREN = 40const C_CLOSE_PAREN = 41const C_COLON = 58const C_TILDE = 126const C_DOLLAR = 36
const ESCAPED_CHAR = "\\\\" + ESCAPABLEconst TAGNAME = "[A-Za-z][A-Za-z0-9-]*"const ATTRIBUTENAME = "[a-zA-Z_:][a-zA-Z0-9:._-]*"const UNQUOTEDVALUE = "[^\"'=<>`\\x00-\\x20]+"const SINGLEQUOTEDVALUE = "'[^']*'"const DOUBLEQUOTEDVALUE = '"[^"]*"'const ATTRIBUTEVALUE = "(?:" + UNQUOTEDVALUE + "|" + SINGLEQUOTEDVALUE + "|" + DOUBLEQUOTEDVALUE + ")"const ATTRIBUTEVALUESPEC = "(?:\\s*=\\s*" + ATTRIBUTEVALUE + ")"const ATTRIBUTE = "(?:\\s+" + ATTRIBUTENAME + ATTRIBUTEVALUESPEC + "?)"export const OPENTAG = "<" + TAGNAME + ATTRIBUTE + "*\\s*/?>"export const CLOSETAG = "</" + TAGNAME + "\\s*[>]"const HTMLCOMMENT = "<!-->|<!--->|<!--[\\s\\S]*?-->"const PROCESSINGINSTRUCTION = "[<][?][\\s\\S]*?[?][>]"const DECLARATION = "<![A-Za-z]+[^>]*>"const CDATA = "<!\\[CDATA\\[[\\s\\S]*?\\]\\]>"const HTMLTAG = "(?:" + OPENTAG + "|" + CLOSETAG + "|" + HTMLCOMMENT + "|" + PROCESSINGINSTRUCTION + "|" + DECLARATION + "|" + CDATA + ")"const reHtmlTag = new RegExp(HTMLTAG, "y")
const rePunctuation = /^[!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~\p{P}\p{S}]/uconst reLinkTitle = new RegExp( '(?:"(' + ESCAPED_CHAR + '|\\\\[^\\\\]|[^\\\\"\\x00])*"' + "|'(" + ESCAPED_CHAR + "|\\\\[^\\\\]|[^\\\\'\\x00])*'" + "|\\((" + ESCAPED_CHAR + "|\\\\[^\\\\]|[^\\\\()\\x00])*\\))", "y",)const reLinkDestinationBraces = /(?:<(?:[^<>\n\\\x00]|\\.)*>)/yconst reEscapable = new RegExp("^" + ESCAPABLE)const reEntityHere = new RegExp(ENTITY_PATTERN, "iy")const reTicks = /`+/gconst reTicksHere = /`+/yconst reEmailAutolink = /<([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/yconst reAutolink = /<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*>/iyconst reSpnl = / *(?:\n *)?/yconst reWhitespaceChar = /^[ \t\n\x0b\x0c\x0d]/const reUnicodeWhitespaceChar = /^\s/const reFinalSpace = / *$/const reInitialSpace = / */yconst reSpaceAtEndOfLine = / *(?:\n|$)/yconst reLinkLabel = /\[(?:[^\\[\]]|\\.){0,1000}\]/syconst reMainCore = /[^\n`[\]\\!<&*_'"]+/yconst reMainGfm = /[^\n`[\]\\!<&*_'"~]+/yconst reMainMath = /[^\n`[\]\\!<&*_'"$]+/yconst reMainGfmMath = /[^\n`[\]\\!<&*_'"~$]+/y
// OpenCode's marked math extension (packages/ui/src/context/marked-parser.tsx), verbatimconst reInlineDollarMath = /\$\$(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n$]))\$\$|\$(?=[^\s$])((?:\\.|[^\\\n$])*?(?:\\\S|[^\s\\$]))\$(?!\w)/yconst reInlineParenMath = /\\\(((?:\\.|[^\\\n])*?)\\\)/y
type NodeType = | "text" | "softbreak" | "linebreak" | "code" | "emph" | "strong" | "del" | "link" | "image" | "html_inline" | "math" | "root"
class INode { type: NodeType literal = "" escaped = false destination = "" title = "" delimiter: MathDelimiter = "dollar" display = false parent: INode | null = null first: INode | null = null last: INode | null = null next: INode | null = null prev: INode | null = null
constructor(type: NodeType, literal = "") { this.type = type this.literal = literal }
appendChild(child: INode) { child.unlink() child.parent = this if (this.last) { this.last.next = child child.prev = this.last this.last = child } else { this.first = child this.last = child } }
insertAfter(sibling: INode) { sibling.unlink() sibling.next = this.next if (sibling.next) sibling.next.prev = sibling sibling.prev = this this.next = sibling sibling.parent = this.parent if (sibling.parent && !sibling.next) sibling.parent.last = sibling }
insertBefore(sibling: INode) { sibling.unlink() sibling.prev = this.prev if (sibling.prev) sibling.prev.next = sibling sibling.next = this this.prev = sibling sibling.parent = this.parent if (sibling.parent && !sibling.prev) sibling.parent.first = sibling }
unlink() { if (this.prev) this.prev.next = this.next else if (this.parent) this.parent.first = this.next if (this.next) this.next.prev = this.prev else if (this.parent) this.parent.last = this.prev this.parent = null this.next = null this.prev = null }}
interface Delimiter { cc: number numdelims: number origdelims: number node: INode previous: Delimiter | null next: Delimiter | null canOpen: boolean canClose: boolean}
interface Bracket { node: INode previous: Bracket | null previousDelimiter: Delimiter | null index: number image: boolean active: boolean bracketAfter: boolean}
const text = (s: string) => new INode("text", s)
/** Normalize a link label: strip brackets, trim, collapse whitespace, Unicode case fold */export function normalizeReference(label: string): string { return label .slice(1, label.length - 1) .trim() .replace(/[ \t\r\n]+/g, " ") .toLowerCase() .toUpperCase()}
function isSpecialFastPath(src: string, gfm: boolean, math: boolean): boolean { for (let i = 0; i < src.length; i++) { const c = src.charCodeAt(i) if ( c === C_NEWLINE || c === C_BACKSLASH || c === C_BACKTICK || c === C_ASTERISK || c === C_UNDERSCORE || c === C_OPEN_BRACKET || c === C_CLOSE_BRACKET || c === C_BANG || c === C_LESSTHAN || c === C_AMPERSAND || (gfm && (c === C_TILDE || c === C_COLON || c === 0x40 /* @ */ || c === 0x2e /* . */)) || (math && c === C_DOLLAR) ) { return true } } return false}
class InlineParser { subject = "" pos = 0 delimiters: Delimiter | null = null brackets: Bracket | null = null refmap: ReadonlyMap<string, ReferenceDef> = new Map() gfm = false math = false reMain = reMainCore tickRuns: Map<number, number[]> | null = null unresolvedRefs?: Set<string> usedRefs?: Set<string>
/** `re` must be sticky (anchored at `pos`) or global (searching forward from `pos`) */ match(re: RegExp): string | null { re.lastIndex = this.pos const m = re.exec(this.subject) if (m === null) return null this.pos = re.lastIndex return m[0] }
peek(): number { return this.pos < this.subject.length ? this.subject.charCodeAt(this.pos) : -1 }
spnl(): true { this.match(reSpnl) return true }
parseBackticks(block: INode): boolean { const ticks = this.match(reTicksHere) if (ticks === null) return false const afterOpenTicks = this.pos const len = ticks.length if (this.tickRuns === null) { const first = this.match(reTicks) if (first === null) { this.pos = afterOpenTicks block.appendChild(text(ticks)) return true } if (first.length === len) { const node = new INode("code") const contents = this.subject.slice(afterOpenTicks, this.pos - len).replace(/\n/gm, " ") if (contents.length > 0 && /[^ ]/.test(contents) && contents[0] === " " && contents[contents.length - 1] === " ") { node.literal = contents.slice(1, contents.length - 1) } else { node.literal = contents } block.appendChild(node) return true } const map = new Map<number, number[]>() map.set(first.length, [this.pos]) let m: string | null while ((m = this.match(reTicks)) !== null) { let list = map.get(m.length) if (!list) map.set(m.length, (list = [])) list.push(this.pos) } this.tickRuns = map } const candidates = this.tickRuns.get(len) if (candidates) { for (let i = 0; i < candidates.length; i++) { const endPos = candidates[i]! if (endPos - len >= afterOpenTicks) { this.pos = endPos const node = new INode("code") const contents = this.subject.slice(afterOpenTicks, endPos - len).replace(/\n/gm, " ") if (contents.length > 0 && /[^ ]/.test(contents) && contents[0] === " " && contents[contents.length - 1] === " ") { node.literal = contents.slice(1, contents.length - 1) } else { node.literal = contents } block.appendChild(node) return true } } } this.pos = afterOpenTicks block.appendChild(text(ticks)) return true }
parseBackslash(block: INode): boolean { const subj = this.subject if (this.math) { reInlineParenMath.lastIndex = this.pos const m = reInlineParenMath.exec(subj) if (m) { const node = new INode("math", m[1]!.trim()) node.delimiter = "paren" node.display = false block.appendChild(node) this.pos += m[0].length return true } } this.pos += 1 if (this.peek() === C_NEWLINE) { this.pos += 1 block.appendChild(new INode("linebreak")) } else if (reEscapable.test(subj.charAt(this.pos))) { const node = text(subj.charAt(this.pos)) node.escaped = true block.appendChild(node) this.pos += 1 } else { block.appendChild(text("\\")) } return true }
parseDollar(block: INode): boolean { reInlineDollarMath.lastIndex = this.pos const m = reInlineDollarMath.exec(this.subject) if (!m) return false const display = m[1] !== undefined const node = new INode("math", (m[1] ?? m[2])!.trim()) node.delimiter = display ? "double-dollar" : "dollar" node.display = display block.appendChild(node) this.pos += m[0].length return true }
parseAutolink(block: INode): boolean { let m: string | null if ((m = this.match(reEmailAutolink))) { const dest = m.slice(1, m.length - 1) const node = new INode("link") node.destination = normalizeURI("mailto:" + dest) node.appendChild(text(dest)) block.appendChild(node) return true } if ((m = this.match(reAutolink))) { const dest = m.slice(1, m.length - 1) const node = new INode("link") node.destination = normalizeURI(dest) node.appendChild(text(dest)) block.appendChild(node) return true } return false }
parseHtmlTag(block: INode): boolean { const m = this.match(reHtmlTag) if (m === null) return false block.appendChild(new INode("html_inline", m)) return true }
scanDelims(cc: number): { numdelims: number; canOpen: boolean; canClose: boolean } | null { let numdelims = 0 const startpos = this.pos while (this.peek() === cc) { numdelims++ this.pos++ } if (numdelims === 0) return null
const charBefore = startpos === 0 ? "\n" : this.subject.charAt(startpos - 1) const ccAfter = this.peek() const charAfter = ccAfter === -1 ? "\n" : this.subject.charAt(this.pos)
const afterIsWhitespace = reUnicodeWhitespaceChar.test(charAfter) const afterIsPunctuation = rePunctuation.test(charAfter) const beforeIsWhitespace = reUnicodeWhitespaceChar.test(charBefore) const beforeIsPunctuation = rePunctuation.test(charBefore)
const leftFlanking = (!afterIsWhitespace && (!afterIsPunctuation || beforeIsWhitespace || beforeIsPunctuation)) || (this.gfm && cc !== C_TILDE && charAfter === "~") const rightFlanking = (!beforeIsWhitespace && (!beforeIsPunctuation || afterIsWhitespace || afterIsPunctuation)) || (this.gfm && cc !== C_TILDE && charBefore === "~") let canOpen: boolean let canClose: boolean if (cc === C_UNDERSCORE) { canOpen = leftFlanking && (!rightFlanking || beforeIsPunctuation) canClose = rightFlanking && (!leftFlanking || afterIsPunctuation) } else { canOpen = leftFlanking canClose = rightFlanking } if (cc === C_TILDE && numdelims > 2) { canOpen = false canClose = false } this.pos = startpos return { numdelims, canOpen, canClose } }
handleDelim(cc: number, block: INode): boolean { const res = this.scanDelims(cc) if (!res) return false const numdelims = res.numdelims const startpos = this.pos this.pos += numdelims const node = text(this.subject.slice(startpos, this.pos)) block.appendChild(node)
if (res.canOpen || res.canClose) { this.delimiters = { cc, numdelims, origdelims: numdelims, node, previous: this.delimiters, next: null, canOpen: res.canOpen, canClose: res.canClose, } if (this.delimiters.previous !== null) this.delimiters.previous.next = this.delimiters } return true }
removeDelimiter(delim: Delimiter) { if (delim.previous !== null) delim.previous.next = delim.next if (delim.next === null) this.delimiters = delim.previous else delim.next.previous = delim.previous }
processEmphasis(stackBottom: Delimiter | null) { if (this.delimiters === stackBottom) return const openersBottom: (Delimiter | null)[] = [] for (let i = 0; i < 16; i++) openersBottom[i] = stackBottom
let closer = this.delimiters while (closer !== null && closer.previous !== stackBottom) closer = closer.previous
while (closer !== null) { const closercc = closer.cc if (!closer.canClose) { closer = closer.next continue } let opener = closer.previous let openerFound = false const openersBottomIndex = closercc === C_UNDERSCORE ? 2 + (closer.canOpen ? 3 : 0) + (closer.origdelims % 3) : closercc === C_ASTERISK ? 8 + (closer.canOpen ? 3 : 0) + (closer.origdelims % 3) : 14 + (closer.origdelims === 2 ? 1 : 0) while (opener !== null && opener !== stackBottom && opener !== openersBottom[openersBottomIndex]) { if (closercc === C_TILDE) { if (opener.cc === C_TILDE && opener.canOpen && opener.origdelims === closer.origdelims) { openerFound = true break } } else { const oddMatch = (closer.canOpen || opener.canClose) && closer.origdelims % 3 !== 0 && (opener.origdelims + closer.origdelims) % 3 === 0 if (opener.cc === closer.cc && opener.canOpen && !oddMatch) { openerFound = true break } } opener = opener.previous } const oldCloser = closer
if (!openerFound) { closer = closer.next } else { const op = opener! const useDelims = closercc === C_TILDE ? closer.numdelims : closer.numdelims >= 2 && op.numdelims >= 2 ? 2 : 1 const openerInl = op.node const closerInl = closer.node op.numdelims -= useDelims closer.numdelims -= useDelims openerInl.literal = openerInl.literal.slice(0, openerInl.literal.length - useDelims) closerInl.literal = closerInl.literal.slice(0, closerInl.literal.length - useDelims)
const emph = new INode(closercc === C_TILDE ? "del" : useDelims === 1 ? "emph" : "strong") let tmp = openerInl.next while (tmp && tmp !== closerInl) { const next = tmp.next tmp.unlink() emph.appendChild(tmp) tmp = next } openerInl.insertAfter(emph)
// remove delimiters between opener and closer if (op.next !== closer) { op.next = closer closer.previous = op }
if (op.numdelims === 0) { openerInl.unlink() this.removeDelimiter(op) } if (closer.numdelims === 0) { closerInl.unlink() const tempstack = closer.next this.removeDelimiter(closer) closer = tempstack } } if (!openerFound) { openersBottom[openersBottomIndex] = oldCloser.previous if (!oldCloser.canOpen) this.removeDelimiter(oldCloser) } }
while (this.delimiters !== null && this.delimiters !== stackBottom) this.removeDelimiter(this.delimiters) }
parseLinkTitle(): string | null { const title = this.match(reLinkTitle) if (title === null) return null return unescapeString(title.slice(1, -1)) }
parseLinkDestination(): string | null { let res = this.match(reLinkDestinationBraces) if (res !== null) return normalizeURI(unescapeString(res.slice(1, -1))) if (this.peek() === C_LESSTHAN) return null const savepos = this.pos let openparens = 0 let c: number while ((c = this.peek()) !== -1) { if (c === C_BACKSLASH && reEscapable.test(this.subject.charAt(this.pos + 1))) { this.pos += 1 if (this.peek() !== -1) this.pos += 1 } else if (c === C_OPEN_PAREN) { this.pos += 1 openparens += 1 } else if (c === C_CLOSE_PAREN) { if (openparens < 1) break this.pos += 1 openparens -= 1 } else if (reWhitespaceChar.test(String.fromCharCode(c))) { break } else if (c < 0x20 || c === 0x7f) { break } else { this.pos += 1 } } if (this.pos === savepos && c !== C_CLOSE_PAREN) return null if (openparens !== 0) return null res = this.subject.slice(savepos, this.pos) return normalizeURI(unescapeString(res)) }
parseLinkLabel(): number { const m = this.match(reLinkLabel) if (m === null || m.length > 1001) return 0 return m.length }
parseOpenBracket(block: INode): boolean { const startpos = this.pos this.pos += 1 const node = text("[") block.appendChild(node) this.addBracket(node, startpos, false) return true }
parseBang(block: INode): boolean { const startpos = this.pos this.pos += 1 if (this.peek() === C_OPEN_BRACKET) { this.pos += 1 const node = text("![") block.appendChild(node) this.addBracket(node, startpos + 1, true) } else { block.appendChild(text("!")) } return true }
lookupRef(label: string): ReferenceDef | undefined { const norm = normalizeReference(label) const def = this.refmap.get(norm) if (def) this.usedRefs?.add(norm) else if (norm) this.unresolvedRefs?.add(norm) return def }
parseCloseBracket(block: INode): boolean { let dest: string | null = null let title: string | null = null let matched = false
this.pos += 1 const startpos = this.pos let opener = this.brackets if (opener === null) { block.appendChild(text("]")) return true } if (!opener.active) { block.appendChild(text("]")) this.removeBracket() return true } const isImage = opener.image const savepos = this.pos
if (this.peek() === C_OPEN_PAREN) { this.pos++ if ( this.spnl() && (dest = this.parseLinkDestination()) !== null && this.spnl() && ((reWhitespaceChar.test(this.subject.charAt(this.pos - 1)) && (title = this.parseLinkTitle())) || true) && this.spnl() && this.peek() === C_CLOSE_PAREN ) { this.pos += 1 matched = true } else { this.pos = savepos } }
if (!matched) { const beforelabel = this.pos const n = this.parseLinkLabel() let reflabel: string | undefined if (n > 2) { reflabel = this.subject.slice(beforelabel, beforelabel + n) } else if (!opener.bracketAfter) { reflabel = this.subject.slice(opener.index, startpos) } if (n === 0) this.pos = savepos if (reflabel) { const link = this.lookupRef(reflabel) if (link) { dest = link.href title = link.title ?? null matched = true } } }
if (matched) { const node = new INode(isImage ? "image" : "link") node.destination = dest ?? "" node.title = title || "" let tmp = opener.node.next while (tmp) { const next = tmp.next tmp.unlink() node.appendChild(tmp) tmp = next } block.appendChild(node) this.processEmphasis(opener.previousDelimiter) this.removeBracket() opener.node.unlink() if (!isImage) { opener = this.brackets while (opener !== null) { if (!opener.image) { if (!opener.active) break opener.active = false } opener = opener.previous } } return true }
this.removeBracket() this.pos = startpos block.appendChild(text("]")) return true }
addBracket(node: INode, index: number, image: boolean) { if (this.brackets !== null) this.brackets.bracketAfter = true this.brackets = { node, previous: this.brackets, previousDelimiter: this.delimiters, index, image, active: true, bracketAfter: false, } }
removeBracket() { this.brackets = this.brackets!.previous }
parseEntity(block: INode): boolean { const m = this.match(reEntityHere) if (!m) return false const node = text(decodeEntity(m)) node.escaped = true block.appendChild(node) return true }
parseString(block: INode): boolean { const m = this.match(this.reMain) if (!m) return false block.appendChild(text(m)) return true }
parseNewline(block: INode): boolean { this.pos += 1 const lastc = block.last if (lastc && lastc.type === "text" && lastc.literal[lastc.literal.length - 1] === " ") { const hardbreak = lastc.literal[lastc.literal.length - 2] === " " lastc.literal = lastc.literal.replace(reFinalSpace, "") block.appendChild(new INode(hardbreak ? "linebreak" : "softbreak")) } else { block.appendChild(new INode("softbreak")) } this.match(reInitialSpace) return true }
/** Attempt to parse a link reference definition at the start of `s`, adding it to `refmap`. Returns chars consumed. */ parseReference(s: string, refmap: RefMap): number { this.subject = s this.pos = 0 const startpos = 0 let title: string | null = null
const matchChars = this.parseLinkLabel() if (matchChars === 0) return 0 const rawlabel = this.subject.slice(0, matchChars)
if (this.peek() === C_COLON) { this.pos++ } else { this.pos = startpos return 0 }
this.spnl() const dest = this.parseLinkDestination() if (dest === null) { this.pos = startpos return 0 }
const beforetitle = this.pos this.spnl() if (this.pos !== beforetitle) title = this.parseLinkTitle() if (title === null) this.pos = beforetitle
let atLineEnd = true if (this.match(reSpaceAtEndOfLine) === null) { if (title === null) { atLineEnd = false } else { title = null this.pos = beforetitle atLineEnd = this.match(reSpaceAtEndOfLine) !== null } } if (!atLineEnd) { this.pos = startpos return 0 }
const normlabel = normalizeReference(rawlabel) if (normlabel === "") { this.pos = startpos return 0 } if (!refmap.has(normlabel)) { refmap.set(normlabel, { href: dest, ...(title ? { title } : {}), raw: s.slice(0, this.pos).trim(), }) } return this.pos - startpos }
parseInline(block: INode): boolean { const c = this.peek() if (c === -1) return false let res = false switch (c) { case C_NEWLINE: res = this.parseNewline(block) break case C_BACKSLASH: res = this.parseBackslash(block) break case C_BACKTICK: res = this.parseBackticks(block) break case C_ASTERISK: case C_UNDERSCORE: res = this.handleDelim(c, block) break case C_TILDE: res = this.gfm && this.handleDelim(c, block) break case C_DOLLAR: res = this.math && this.parseDollar(block) break case C_OPEN_BRACKET: res = this.parseOpenBracket(block) break case C_BANG: res = this.parseBang(block) break case C_CLOSE_BRACKET: res = this.parseCloseBracket(block) break case C_LESSTHAN: res = this.parseAutolink(block) || this.parseHtmlTag(block) break case C_AMPERSAND: res = this.parseEntity(block) break default: res = this.parseString(block) break } if (!res) { this.pos += 1 block.appendChild(text(String.fromCharCode(c))) } return true }
parse(content: string): INode { const root = new INode("root") this.subject = content.trim() this.pos = 0 this.delimiters = null this.brackets = null this.tickRuns = null while (this.parseInline(root)) {} this.processEmphasis(null) return root }}
const sharedParser = new InlineParser()const EMPTY_REFMAP: ReadonlyMap<string, ReferenceDef> = new Map()
/** Parse a link reference definition at the start of `s` into `refmap`; returns the number of characters consumed */export function parseReference(s: string, refmap: RefMap): number { return sharedParser.parseReference(s, refmap)}
export function parseInlines(raw: string, options: InlineParseOptions = {}): InlineNode[] { const gfm = options.gfm !== false const math = options.math !== false const src = raw.trim() if (!src) return [] if (!isSpecialFastPath(src, gfm, math)) return [{ type: "text", text: src }]
const p = sharedParser p.refmap = options.defs ?? EMPTY_REFMAP p.gfm = gfm p.math = math p.reMain = gfm ? (math ? reMainGfmMath : reMainGfm) : math ? reMainMath : reMainCore p.unresolvedRefs = options.unresolvedRefs p.usedRefs = options.usedRefs const root = p.parse(src) return convertChildren(root, gfm, false)}
function convertChildren(parent: INode, gfm: boolean, inLink: boolean): InlineNode[] { const out: InlineNode[] = [] let pendingText = "" let prevChar = "" const pushText = (s: string) => { if (!s) return const prev = out[out.length - 1] if (prev && prev.type === "text") prev.text += s else out.push({ type: "text", text: s }) } const flush = () => { if (!pendingText) return if (gfm && !inLink) { for (const node of autolinkLiterals(pendingText, prevChar)) { if (node.type === "text") pushText(node.text) else out.push(node) } } else { pushText(pendingText) } prevChar = pendingText[pendingText.length - 1]! pendingText = "" } for (let n = parent.first; n; n = n.next) { switch (n.type) { case "text": if (gfm && !inLink && n.escaped) { flush() pushText(n.literal) prevChar = n.literal[n.literal.length - 1] ?? "" } else { pendingText += n.literal } break case "softbreak": pendingText += "\n" break case "linebreak": flush() out.push({ type: "br" }) prevChar = "\n" break case "code": flush() out.push({ type: "code", text: n.literal }) prevChar = "`" break case "emph": flush() out.push({ type: "em", children: convertChildren(n, gfm, inLink) }) prevChar = "*" break case "strong": flush() out.push({ type: "strong", children: convertChildren(n, gfm, inLink) }) prevChar = "*" break case "del": flush() out.push({ type: "del", children: convertChildren(n, gfm, inLink) }) prevChar = "~" break case "link": flush() out.push({ type: "link", href: n.destination, ...(n.title ? { title: n.title } : {}), children: convertChildren(n, gfm, true), }) prevChar = ")" break case "image": { flush() const children = convertChildren(n, gfm, true) out.push({ type: "image", src: n.destination, alt: inlinePlainText(children), ...(n.title ? { title: n.title } : {}), children, }) prevChar = ")" break } case "html_inline": flush() out.push({ type: "html_inline", raw: n.literal }) prevChar = ">" break case "math": flush() out.push({ type: "math", source: n.literal, delimiter: n.delimiter, display: n.display }) prevChar = "$" break } } flush() return out}
function inlinePlainText(nodes: ReadonlyArray<InlineNode>): string { let out = "" for (const node of nodes) { switch (node.type) { case "text": case "code": out += node.text break case "strong": case "em": case "del": case "link": out += inlinePlainText(node.children) break case "image": out += node.children ? inlinePlainText(node.children) : node.alt break case "math": out += node.source break case "br": out += "\n" break case "html_inline": out += node.raw break } } return out}
// GFM extended autolinks (www., http://, https://, ftp://, and bare emails), applied to merged text runs// outside links, following cmark-gfm's autolink extension.
function isAlnum(c: number): boolean { return (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a)}
function isAutolinkWhitespace(c: number): boolean { return c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0b || c === 0x0c || c === 0x0d}
function isValidPrecedingForWww(s: string, i: number, prevChar: string): boolean { if (i === 0 && !prevChar) return true const c = i === 0 ? prevChar.charCodeAt(0) : s.charCodeAt(i - 1) return ( isAutolinkWhitespace(c) || c === C_ASTERISK || c === C_UNDERSCORE || c === C_TILDE || c === C_OPEN_PAREN || c === C_OPEN_BRACKET || c === C_CLOSE_BRACKET )}
/** Length of a valid domain at `i`, or 0. Mirrors cmark-gfm `check_domain`. */function checkDomain(s: string, i: number, allowShort: boolean): number { let np = 0 let uscore1 = 0 let uscore2 = 0 let j = i for (; j < s.length; j++) { const c = s.charCodeAt(j) if (c === 0x5c /* \ */ && j + 1 < s.length) { // a backslash is never part of a domain break } if (c === C_UNDERSCORE) { uscore2++ } else if (c === 0x2e /* . */) { uscore1 = uscore2 uscore2 = 0 np++ } else if (!isAlnum(c) && c !== 0x2d /* - */ && c < 0x80) { break } } if ((uscore1 > 0 || uscore2 > 0) && np <= 10) return 0 if (allowShort || np > 0) return j - i return 0}
/** Trim trailing punctuation from a candidate link of length `len` starting at `start`. Mirrors cmark-gfm `autolink_delim`. */function autolinkDelim(s: string, start: number, len: number): number { for (let k = 0; k < len; k++) { if (s.charCodeAt(start + k) === C_LESSTHAN) { len = k break } } while (len > 0) { const cclose = s.charCodeAt(start + len - 1) if (cclose === C_CLOSE_PAREN) { let opening = 0 let closing = 0 for (let k = 0; k < len; k++) { const c = s.charCodeAt(start + k) if (c === C_OPEN_PAREN) opening++ else if (c === C_CLOSE_PAREN) closing++ } if (closing <= opening) break len-- } else if ( cclose === 0x3f /* ? */ || cclose === 0x21 /* ! */ || cclose === 0x2e /* . */ || cclose === 0x2c /* , */ || cclose === 0x3a /* : */ || cclose === C_ASTERISK || cclose === C_UNDERSCORE || cclose === C_TILDE || cclose === 0x27 /* ' */ || cclose === 0x22 /* " */ ) { len-- } else if (cclose === 0x3b /* ; */) { let newEnd = len - 2 while (newEnd > 0 && isAlnum(s.charCodeAt(start + newEnd))) newEnd-- if (newEnd < len - 2 && s.charCodeAt(start + newEnd) === C_AMPERSAND) len = newEnd else len-- } else { break } } return len}
function matchWww(s: string, i: number, prevChar: string): number { if (!isValidPrecedingForWww(s, i, prevChar)) return 0 if (!s.startsWith("www.", i)) return 0 const domainLen = checkDomain(s, i, false) if (domainLen === 0) return 0 let end = i + domainLen while (end < s.length && !isAutolinkWhitespace(s.charCodeAt(end))) end++ return autolinkDelim(s, i, end - i)}
function matchUrl(s: string, i: number, prevChar: string): { len: number; schemeLen: number } | null { // `i` points at ':' of "://" if (!s.startsWith("://", i)) return null let start = i while (start > 0 && /[A-Za-z]/.test(s[start - 1]!)) start-- if (start === 0 && prevChar && /[A-Za-z]/.test(prevChar)) return null const scheme = s.slice(start, i).toLowerCase() const matchedScheme = scheme === "https" || scheme === "http" || scheme === "ftp" ? scheme : "" if (!matchedScheme) return null const schemeStart = start const domainLen = checkDomain(s, i + 3, true) if (domainLen === 0) return null let end = i + 3 + domainLen while (end < s.length && !isAutolinkWhitespace(s.charCodeAt(end))) end++ const len = autolinkDelim(s, schemeStart, end - schemeStart) if (len <= i + 3 - schemeStart) return null return { len, schemeLen: i - schemeStart }}
function isEmailLocal(c: number): boolean { return isAlnum(c) || c === 0x2e || c === 0x2b || c === 0x2d || c === C_UNDERSCORE}
function matchEmail(s: string, at: number, minStart: number, prevChar: string): { start: number; end: number } | null { let start = at while (start > minStart && isEmailLocal(s.charCodeAt(start - 1))) start-- if (start === at) return null if (start > 0) { if (s.charCodeAt(start - 1) === 0x2f /* / */) return null } else if (prevChar) { const pc = prevChar.charCodeAt(0) if (pc === 0x2f /* / */ || isEmailLocal(pc)) return null } let np = 0 let end = at + 1 for (; end < s.length; end++) { const c = s.charCodeAt(end) if (isAlnum(c)) continue if (c === 0x40 /* @ */) return null if (c === 0x2e && end + 1 < s.length && isAlnum(s.charCodeAt(end + 1))) { np++ continue } if (c !== 0x2d && c !== C_UNDERSCORE) break } if (end - at < 2 || np === 0) return null const last = s.charCodeAt(end - 1) if (!((last >= 0x41 && last <= 0x5a) || (last >= 0x61 && last <= 0x7a))) return null return { start, end }}
const RE_AUTOLINK_CANDIDATE = /www\.|:\/\/|@/i
function autolinkLiterals(s: string, prevChar: string): InlineNode[] { if (!RE_AUTOLINK_CANDIDATE.test(s)) return [{ type: "text", text: s }] const out: InlineNode[] = [] let textStart = 0 let i = 0 const emit = (start: number, end: number, href: string) => { if (start > textStart) out.push({ type: "text", text: s.slice(textStart, start) }) const label = s.slice(start, end) out.push({ type: "link", href: normalizeURI(href), children: [{ type: "text", text: label }] }) textStart = end i = end } while (i < s.length) { const c = s.charCodeAt(i) if ((c === 0x77 || c === 0x57) && s.startsWith("www.", i)) { const len = matchWww(s, i, prevChar) if (len > 0) { emit(i, i + len, "http://" + s.slice(i, i + len)) continue } } else if (c === C_COLON) { const m = matchUrl(s, i, prevChar) if (m) { const start = i - m.schemeLen if (start >= textStart) { emit(start, start + m.len, s.slice(start, start + m.len)) continue } } } else if (c === 0x40 /* @ */) { const m = matchEmail(s, i, textStart, prevChar) if (m) { emit(m.start, m.end, "mailto:" + s.slice(m.start, m.end)) continue } } i++ } if (textStart === 0) return [{ type: "text", text: s }] if (textStart < s.length) out.push({ type: "text", text: s.slice(textStart) }) return out}