Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291import type { InlineNode, ParseOptions } from "./types.js";
// ── inline parser ──────────────────────────────────────────────────────────// Hand-rolled because the rules for // italic are tricky (not in URLs, not// in word-internal slashes like "this/that").
export function parseInline(src: string, opts: ParseOptions = {}): InlineNode[] { return new InlineParser(src, opts).parse();}
class InlineParser { private pos = 0; private readonly src: string; private readonly opts: ParseOptions;
constructor(src: string, opts: ParseOptions) { this.src = src; this.opts = opts; }
parse(terminator?: string): InlineNode[] { const nodes: InlineNode[] = []; let text = "";
const flush = () => { if (text) { nodes.push({ type: "text", value: text }); text = ""; } };
while (this.pos < this.src.length) { const ch = this.src[this.pos];
// terminator check if (terminator && this.src.startsWith(terminator, this.pos)) { this.pos += terminator.length; break; }
// inline code `...` if (ch === "`") { const closeIdx = this.src.indexOf("`", this.pos + 1); if (closeIdx !== -1) { flush(); nodes.push({ type: "code", value: this.src.slice(this.pos + 1, closeIdx) }); this.pos = closeIdx + 1; continue; } }
// inline math $$ ... $$ if (this.src.startsWith("$$", this.pos) && !this.src.startsWith("$$$", this.pos)) { flush(); this.pos += 2; const start = this.pos; const end = this.src.indexOf("$$", this.pos); if (end !== -1) { nodes.push({ type: "math-inline", value: this.src.slice(start, end).trim() }); this.pos = end + 2; } else { text += "$$"; } continue; }
// bold **...** — only if a closing ** actually exists ahead, otherwise // this is a stray "**" that should stay literal text if (this.src.startsWith("**", this.pos) && this.src.indexOf("**", this.pos + 2) !== -1) { flush(); this.pos += 2; const children = this.parse("**"); nodes.push({ type: "bold", children }); continue; }
// italic //...// — only if not inside a URL, not word-internal, and a // closing // actually exists ahead if (this.src.startsWith("//", this.pos) && this.src.indexOf("//", this.pos + 2) !== -1) { // Check if we're inside a URL (preceded by http: or https: or ://) const before = text + nodes.map(n => n.type === "text" ? n.value : "X").join(""); const lastChunk = before.slice(-8); const inUrl = /https?:$/.test(lastChunk) || lastChunk.endsWith("://");
// Check if word-internal: letter immediately before AND after const charBefore = this.src[this.pos - 1] ?? ""; const charAfter = this.src[this.pos + 2]; const wordInternal = /\w/.test(charBefore) && charAfter !== undefined && /\w/.test(charAfter);
if (!inUrl && !wordInternal) { flush(); this.pos += 2; const children = this.parse("//"); nodes.push({ type: "italic", children }); continue; } }
// markdown-compat italic *...* (single star, ** already claimed by bold above) if (this.opts.markdownCompat && ch === "*") { flush(); this.pos += 1; const children = this.parse("*"); nodes.push({ type: "italic", children }); continue; }
// wikilink [[page|display]] or [[page]] if (this.src.startsWith("[[", this.pos)) { flush(); this.pos += 2; const end = this.src.indexOf("]]", this.pos); if (end !== -1) { const inner = this.src.slice(this.pos, end); this.pos = end + 2; const pipe = inner.indexOf("|"); if (pipe !== -1) { nodes.push({ type: "wikilink", page: inner.slice(0, pipe), display: inner.slice(pipe + 1) }); } else { nodes.push({ type: "wikilink", page: inner, display: inner }); } continue; } }
// inline image [![img ...]](...) or ![img ...] if (this.src.startsWith("[![", this.pos)) { flush(); this.pos += 3; const end = this.src.indexOf("]]", this.pos); if (end !== -1) { const inner = this.src.slice(this.pos, end); this.pos = end + 2; // optional () let href = ""; if (this.src[this.pos] === "(") { this.pos++; const hEnd = this.src.indexOf(")", this.pos); href = this.src.slice(this.pos, hEnd); this.pos = hEnd + 1; } const { src, attrs } = parseImageAttrs(inner); nodes.push({ type: "link", href, children: [{ type: "image-inline", src, attrs }] }); continue; } }
if (this.src.startsWith(" if (this.opts.markdownCompat) { const altEnd = this.src.indexOf("]", this.pos + 2); if (altEnd !== -1 && this.src[altEnd + 1] === "(") { const parenEnd = this.src.indexOf(")", altEnd + 2); if (parenEnd !== -1) { flush(); const alt = this.src.slice(this.pos + 2, altEnd); const srcUrl = this.src.slice(altEnd + 2, parenEnd); nodes.push({ type: "image-inline", src: srcUrl, attrs: alt ? { alt } : {} }); this.pos = parenEnd + 1; continue; } } }
flush(); this.pos += 2; const end = this.src.indexOf("]", this.pos); if (end !== -1) { const inner = this.src.slice(this.pos, end); this.pos = end + 1; const { src, attrs } = parseImageAttrs(inner); nodes.push({ type: "image-inline", src, attrs }); continue; } }
// [^ref] footnote reference if (this.src.startsWith("[^", this.pos)) { flush(); this.pos += 2; const end = this.src.indexOf("]", this.pos); if (end !== -1) { const id = this.src.slice(this.pos, end); this.pos = end + 1; nodes.push({ type: "footnote-ref", id }); continue; } }
// [>> inline sidenote] if (this.src.startsWith("[>> ", this.pos)) { flush(); this.pos += 4; const end = this.src.indexOf("]", this.pos); if (end !== -1) { const inner = this.src.slice(this.pos, end); this.pos = end + 1; nodes.push({ type: "sidenote-inline", children: parseInline(inner, this.opts) }); continue; } }
// [text](url) or [text](^ref) link if (ch === "[") { const linkResult = this.tryParseLink(); if (linkResult) { flush(); nodes.push(linkResult); continue; } }
// auto-link: http:// or https:// if (this.src.startsWith("http://", this.pos) || this.src.startsWith("https://", this.pos)) { flush(); const start = this.pos; // consume until whitespace or punctuation that ends URLs while (this.pos < this.src.length && !/[\s,;)\]>]/.test(this.src[this.pos]!)) { this.pos++; } const url = this.src.slice(start, this.pos); nodes.push({ type: "link", href: url, children: [{ type: "text", value: url }] }); continue; }
text += ch; this.pos++; }
if (text) nodes.push({ type: "text", value: text }); return nodes; }
private tryParseLink(): InlineNode | null { const saved = this.pos; this.pos++; // skip [
// Collect label text (not recursively parsing, just grabbing raw) let depth = 1; const labelStart = this.pos; while (this.pos < this.src.length && depth > 0) { if (this.src[this.pos] === "[") depth++; else if (this.src[this.pos] === "]") depth--; if (depth > 0) this.pos++; } if (depth !== 0) { this.pos = saved; return null; } const label = this.src.slice(labelStart, this.pos); this.pos++; // skip ]
if (this.src[this.pos] !== "(") { this.pos = saved; return null; } this.pos++; // skip ( const hrefStart = this.pos; while (this.pos < this.src.length && this.src[this.pos] !== ")") this.pos++; const hrefRaw = this.src.slice(hrefStart, this.pos); this.pos++; // skip )
let href = hrefRaw; // reference link (^id) — resolved at render time via data attr if (hrefRaw.startsWith("^")) { href = `^${hrefRaw.slice(1)}`; // keep the ^ marker, renderer resolves }
return { type: "link", href, children: parseInline(label, this.opts) }; }}
// ── apply typography (dashes) ──────────────────────────────────────────────// Called by the renderer on text nodes outside code spans.
export function applyTypography(text: string): string { return text .replace(/---/g, "—") // em dash .replace(/--/g, "–"); // en dash}
// ── image attr parser (shared with block.ts) ───────────────────────────────
export function parseImageAttrs(inner: string): { src: string; attrs: Record<string, string> } { const parts = inner.match(/(?:[^\s"]+|"[^"]*")+/g) ?? []; const src = parts[1] ?? ""; const attrs: Record<string, string> = {}; for (let i = 2; i < parts.length; i++) { const part = parts[i]!; const kv = part.match(/^([^=]+)=(.+)$/); if (kv) attrs[kv[1]!] = kv[2]!.replace(/^"|"$/g, ""); else attrs[part] = ""; } return { src, attrs };}