// The inline parser: CommonMark's delimiter-stack algorithm, with GFM's strikethrough and bare autolinks. Nodes, // delimiters and brackets live in lists and point at each other by index, since Weft's values can't alias. import type { Inline } from "../ast" import { isAlnum, isAlpha, isAsciiPunct, isDigit, isSpaceOrTab, isUnicodePunct, isUnicodeSpace, codePointBefore, normalizeLabel, normalizeUrl, scanEntity, unescape, } from "./chars" /// A link reference definition. export type Ref = { url: string, title: string? } /// How deep inlines nest before the inner ones are flattened into their parent. let MAX_DEPTH = 64 type Kind = | "root" | "text" | "soft" | "hard" | "code" | "emph" | "strong" | "del" | "link" | "image" | "html" type Node = { kind: Kind, text: string, url: string, title: string?, parent: int, prev: int, next: int, first: int, last: int, } type Delim = { cc: int, num: int, orig: int, node: int, prev: int, next: int, canOpen: boolean, canClose: boolean, } type Bracket = { node: int, prev: int, prevDelim: int, index: int, image: boolean, active: boolean, bracketAfter: boolean, } type State = { subject: string, pos: int, gfm: boolean, nodes: Node[], delims: Delim[], delimTop: int, brackets: Bracket[], bracketTop: int, /// Backtick run lengths known to have no closer further on. noTicks: Set, /// Constructs known to have no terminator further on, so a long run of openers stays linear. noComment: boolean, noPi: boolean, noCdata: boolean, noDecl: boolean, } fn newState(subject: string, gfm: boolean): State { let root: Node = { kind: "root", text: "", url: "", title: null, parent: -1, prev: -1, next: -1, first: -1, last: -1, } return { subject, pos: 0, gfm, nodes: [root], delims: [], delimTop: -1, brackets: [], bracketTop: -1, noTicks: Set(), noComment: false, noPi: false, noCdata: false, noDecl: false, } } fn peek(s: State): int { return s.subject.codeUnitAt(s.pos) ?? -1 } fn isEscapeAt(s: State, i: int): boolean { return s.subject.codeUnitAt(i) == 92 && isAsciiPunct(s.subject.codeUnitAt(i + 1) ?? -1) } fn isTilde(s: State, c: int): boolean { return c == 126 && s.gfm } fn nextIsPunct(s: State): boolean { return isAsciiPunct(s.subject.codeUnitAt(s.pos + 1) ?? -1) } fn hasDelimAbove(s: State, bottom: int): boolean { return s.delimTop != -1 && s.delimTop != bottom } fn mayCloseTicks(s: State, ticks: int): boolean { return !s.noTicks.has(ticks) } // ---- the node list ---- fn add(mut s: State, kind: Kind, text: string): int { let id = s.nodes.length s.nodes.push( { kind, text, url: "", title: null, parent: -1, prev: -1, next: -1, first: -1, last: -1 }, ) return id } fn append(mut s: State, parent: int, child: int) { let last = s.nodes[parent].last s.nodes[child].parent = parent s.nodes[child].prev = last s.nodes[child].next = -1 if last != -1 { s.nodes[last].next = child } else { s.nodes[parent].first = child } s.nodes[parent].last = child } fn pushText(mut s: State, text: string): int { let id = add(&s, "text", text) append(&s, 0, id) return id } fn pushNode(mut s: State, kind: Kind) { let id = add(&s, kind, "") append(&s, 0, id) } fn unlink(mut s: State, id: int) { let prev = s.nodes[id].prev let next = s.nodes[id].next let parent = s.nodes[id].parent if prev != -1 { s.nodes[prev].next = next } else if parent != -1 { s.nodes[parent].first = next } if next != -1 { s.nodes[next].prev = prev } else if parent != -1 { s.nodes[parent].last = prev } s.nodes[id].parent = -1 s.nodes[id].prev = -1 s.nodes[id].next = -1 } fn insertAfter(mut s: State, at: int, id: int) { let next = s.nodes[at].next let parent = s.nodes[at].parent s.nodes[id].parent = parent s.nodes[id].prev = at s.nodes[id].next = next s.nodes[at].next = id if next != -1 { s.nodes[next].prev = id } else if parent != -1 { s.nodes[parent].last = id } } /// Moves the siblings after `from`, up to `to` (not included; `-1` for all of them), into `parent`. fn moveChildren(mut s: State, from: int, to: int, parent: int) { let mut at = s.nodes[from].next while at != -1 && at != to { let next = s.nodes[at].next unlink(&s, at) append(&s, parent, at) at = next } } // ---- brackets and delimiters ---- fn addBracket(mut s: State, node: int, index: int, image: boolean) { let top = s.bracketTop let prevDelim = s.delimTop if top != -1 { s.brackets[top].bracketAfter = true } s.brackets.push({ node, prev: top, prevDelim, index, image, active: true, bracketAfter: false }) s.bracketTop = s.brackets.length - 1 } fn removeBracket(mut s: State) { s.bracketTop = s.brackets[s.bracketTop].prev } fn removeDelim(mut s: State, d: int) { let prev = s.delims[d].prev let next = s.delims[d].next if prev != -1 { s.delims[prev].next = next } if next == -1 { s.delimTop = prev } else { s.delims[next].prev = prev } } fn handleDelim(mut s: State, cc: int) { let start = s.pos while peek(s) == cc { s.pos += 1 } let num = s.pos - start let text = s.subject.slice(start, s.pos) let node = pushText(&s, text) // GFM's strikethrough takes one or two tildes; a longer run is text. if cc == 126 && num > 2 { return } let before = codePointBefore(s.subject, start) let after = (s.subject.codePointAt(s.pos) ?? 10) let afterSpace = isUnicodeSpace(after) let afterPunct = isUnicodePunct(after) let beforeSpace = isUnicodeSpace(before) let beforePunct = isUnicodePunct(before) let left = !afterSpace && (!afterPunct || beforeSpace || beforePunct) let right = !beforeSpace && (!beforePunct || afterSpace || afterPunct) let canOpen = cc == 95 ? left && (!right || beforePunct) : left let canClose = cc == 95 ? right && (!left || afterPunct) : right if !canOpen && !canClose { return } let top = s.delimTop s.delims.push({ cc, num, orig: num, node, prev: top, next: -1, canOpen, canClose }) let id = s.delims.length - 1 if top != -1 { s.delims[top].next = id } s.delimTop = id } fn bottomIndex(d: Delim): int { let open = d.canOpen ? 3 : 0 if d.cc == 95 { return open + d.orig % 3 } if d.cc == 42 { return 6 + open + d.orig % 3 } return 12 + (d.num == 2 ? 1 : 0) } fn processEmphasis(mut s: State, bottom: int) { let mut bottoms: int[] = [ bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, bottom, ] let mut closer = s.delimTop while closer != -1 { let prev = s.delims[closer].prev if prev == bottom { break } closer = prev } while closer != -1 { if !s.delims[closer].canClose { closer = s.delims[closer].next continue } let c = s.delims[closer] let index = bottomIndex(c) let mut opener = c.prev let mut found = false while opener != -1 && opener != bottom && opener != bottoms[index] { let o = s.delims[opener] if o.cc == c.cc && o.canOpen { let odd = (c.canOpen || o.canClose) && c.orig % 3 != 0 && (o.orig + c.orig) % 3 == 0 if c.cc == 126 ? o.num == c.num : !odd { found = true break } } opener = o.prev } let oldCloser = closer if found { let tilde = c.cc == 126 let openNum = s.delims[opener].num let used = tilde ? c.num : c.num >= 2 && openNum >= 2 ? 2 : 1 let openNode = s.delims[opener].node let closeNode = c.node s.delims[opener].num -= used s.delims[closer].num -= used let openText = s.nodes[openNode].text let closeText = s.nodes[closeNode].text s.nodes[openNode].text = openText.slice(0, openText.length - used) s.nodes[closeNode].text = closeText.slice(0, closeText.length - used) let wrap = add(&s, tilde ? "del" : used == 1 ? "emph" : "strong", "") moveChildren(&s, openNode, closeNode, wrap) insertAfter(&s, openNode, wrap) // Delimiters between the two are inside the new node now. s.delims[opener].next = closer s.delims[closer].prev = opener if s.delims[opener].num == 0 { unlink(&s, openNode) removeDelim(&s, opener) } if s.delims[closer].num == 0 { unlink(&s, closeNode) let next = s.delims[closer].next removeDelim(&s, closer) closer = next } } else { closer = c.next bottoms[index] = c.prev if !c.canOpen { removeDelim(&s, oldCloser) } } } while hasDelimAbove(s, bottom) { let top = s.delimTop removeDelim(&s, top) } } // ---- code spans, escapes, breaks ---- /// Where the run of `c` starting at `from` ends. fn runEnd(text: string, from: int, c: int): int { let mut i = from while text.codeUnitAt(i) == c { i += 1 } return i } /// Where the plain text starting at `from` ends. fn textEnd(text: string, from: int, gfm: boolean): int { let n = text.length let mut i = from + 1 while i < n && !isSpecial(text.codeUnitAt(i) ?? 10, gfm) { i += 1 } return i } fn parseBackticks(mut s: State) { let start = s.pos while peek(s) == 96 { s.pos += 1 } let ticks = s.pos - start let after = s.pos if mayCloseTicks(s, ticks) { let mut i = after let n = s.subject.length while i < n { let at = s.subject.indexOf("`", i) if at < 0 { break } let j = runEnd(s.subject, at, 96) if j - at == ticks { let mut code = s.subject.slice(after, at).replaceAll("\n", " ") let strip = code.length > 1 && code.startsWith(" ") && code.endsWith(" ") && code.trim() != "" if strip { code = code.slice(1, code.length - 1) } let id = add(&s, "code", code) append(&s, 0, id) s.pos = j return } i = j } s.noTicks.add(ticks) } let run = s.subject.slice(start, after) pushText(&s, run) } fn parseBackslash(mut s: State) { s.pos += 1 let c = peek(s) if c == 10 { s.pos += 1 pushNode(&s, "hard") skipLineStart(&s) } else if isAsciiPunct(c) { pushText(&s, String.fromCharCode(c)) s.pos += 1 } else { pushText(&s, "\\") } } fn skipLineStart(mut s: State) { while isSpaceOrTab(peek(s)) { s.pos += 1 } } fn parseNewline(mut s: State) { s.pos += 1 let last = s.nodes[0].last let mut hard = false if last != -1 && (s.nodes[last].kind == "text") { let text = s.nodes[last].text if text.endsWith(" ") { hard = text.endsWith(" ") let mut end = text.length while end > 0 && text.codeUnitAt(end - 1) == 32 { end -= 1 } s.nodes[last].text = text.slice(0, end) } } pushNode(&s, hard ? "hard" : "soft") skipLineStart(&s) } // ---- autolinks and raw HTML ---- fn isEmailLocal(c: int): boolean { return isAlnum(c) || (c < 128 && c > 0 && ".!#$%&'*+/=?^_`{|}~-".includes(String.fromCharCode(c))) } /// `` or `` at `pos`: the address and the position after `>`. fn scanAutolink(s: string, pos: int): [string, boolean, int]? { let mut i = pos + 1 if isAlpha(s.codeUnitAt(i) ?? -1) { let mut j = i + 1 while j - i < 32 { let c = s.codeUnitAt(j) ?? -1 if !(isAlnum(c) || c == 43 || c == 46 || c == 45) { break } j += 1 } if s.codeUnitAt(j) == 58 && j - i >= 2 { let mut k = j + 1 while true { let c = s.codeUnitAt(k) ?? -1 if c == 62 { return [s.slice(i, k), false, k + 1] } if c == -1 || c <= 32 || c == 60 || c == 127 { break } k += 1 } } } while isEmailLocal(s.codeUnitAt(i) ?? -1) { i += 1 } if i == pos + 1 || s.codeUnitAt(i) != 64 { return null } i += 1 let mut labels = 0 while true { let start = i while isAlnum(s.codeUnitAt(i) ?? -1) || s.codeUnitAt(i) == 45 { i += 1 } let len = i - start if len == 0 || len > 63 || s.codeUnitAt(start) == 45 || s.codeUnitAt(i - 1) == 45 { return null } labels += 1 if s.codeUnitAt(i) == 46 { i += 1 } else { break } } if labels < 1 || s.codeUnitAt(i) != 62 { return null } return [s.slice(pos + 1, i), true, i + 1] } fn skipSpace(s: string, from: int): int { let mut i = from while true { let c = s.codeUnitAt(i) ?? -1 if c == 32 || c == 9 || c == 10 || c == 13 || c == 12 { i += 1 } else { return i } } return i } /// The end of the open tag at `pos`, or -1. export fn scanOpenTag(s: string, pos: int): int { let mut i = pos + 1 if !isAlpha(s.codeUnitAt(i) ?? -1) { return -1 } while isAlnum(s.codeUnitAt(i) ?? -1) || s.codeUnitAt(i) == 45 { i += 1 } while true { let space = skipSpace(s, i) let c = s.codeUnitAt(space) ?? -1 if c == 62 { return space + 1 } if c == 47 { return s.codeUnitAt(space + 1) == 62 ? space + 2 : -1 } if space == i || !(isAlpha(c) || c == 95 || c == 58) { return -1 } i = space + 1 while true { let a = s.codeUnitAt(i) ?? -1 if isAlnum(a) || a == 95 || a == 58 || a == 46 || a == 45 { i += 1 } else { break } } let eq = skipSpace(s, i) if s.codeUnitAt(eq) == 61 { let v = skipSpace(s, eq + 1) let q = s.codeUnitAt(v) ?? -1 if q == 34 || q == 39 { let close = s.indexOf(String.fromCharCode(q), v + 1) if close < 0 { return -1 } i = close + 1 } else { let mut k = v while true { let u = s.codeUnitAt(k) ?? -1 if u == -1 || u <= 32 || u == 34 || u == 39 || u == 61 || u == 60 || u == 62 || u == 96 { break } k += 1 } if k == v { return -1 } i = k } } } return -1 } /// The end of the closing tag at `pos`, or -1. export fn scanCloseTag(s: string, pos: int): int { let mut i = pos + 2 if !isAlpha(s.codeUnitAt(i) ?? -1) { return -1 } while isAlnum(s.codeUnitAt(i) ?? -1) || s.codeUnitAt(i) == 45 { i += 1 } i = skipSpace(s, i) return s.codeUnitAt(i) == 62 ? i + 1 : -1 } fn scanHtml(mut s: State): int { let t = s.subject let p = s.pos let c = t.codeUnitAt(p + 1) ?? -1 if c == 47 { return scanCloseTag(t, p) } if c == 63 { if s.noPi { return -1 } let end = t.indexOf("?>", p + 2) if end < 0 { s.noPi = true return -1 } return end + 2 } if c == 33 { if t.startsWith("", p) { return p + 5 } if t.startsWith("", p) { return p + 6 } if s.noComment { return -1 } let end = t.indexOf("-->", p + 4) if end < 0 { s.noComment = true return -1 } return end + 3 } if t.startsWith("", p + 9) if end < 0 { s.noCdata = true return -1 } return end + 3 } if isAlpha(t.codeUnitAt(p + 2) ?? -1) { if s.noDecl { return -1 } let end = t.indexOf(">", p + 2) if end < 0 { s.noDecl = true return -1 } return end + 1 } return -1 } return scanOpenTag(t, p) } fn parseLessThan(mut s: State) { let auto = scanAutolink(s.subject, s.pos) if auto != null { let id = add(&s, "link", "") s.nodes[id].url = normalizeUrl(auto[1] ? "mailto:" + auto[0] : auto[0]) append(&s, 0, id) let label = add(&s, "text", auto[0]) append(&s, id, label) s.pos = auto[2] return } let end = scanHtml(&s) if end >= 0 { let html = s.subject.slice(s.pos, end) let id = add(&s, "html", html) append(&s, 0, id) s.pos = end return } s.pos += 1 pushText(&s, "<") } // ---- links ---- fn skipLinkSpace(mut s: State) { let mut lines = 0 while true { let c = peek(s) if c == 10 { lines += 1 if lines > 1 { return } s.pos += 1 } else if isSpaceOrTab(c) { s.pos += 1 } else { return } } } fn parseDestination(mut s: State): string? { let start = s.pos if peek(s) == 60 { let mut i = s.pos + 1 while true { let c = (s.subject.codeUnitAt(i) ?? -1) if c == -1 || c == 10 || c == 60 { return null } if c == 62 { s.pos = i + 1 return normalizeUrl(unescape(s.subject.slice(start + 1, i))) } i += isEscapeAt(s, i) ? 2 : 1 } } let mut open = 0 while true { let c = peek(s) if c == 92 && nextIsPunct(s) { s.pos += 2 } else if c == 40 { // cmark's limit: a long run of unfinished links would otherwise scan to the end of the text from each of them. if open >= 32 { s.pos = start return null } open += 1 s.pos += 1 } else if c == 41 { if open < 1 { break } open -= 1 s.pos += 1 } else if c == -1 || c <= 32 || c == 127 { break } else { s.pos += 1 } } if open != 0 || ((s.pos == start) && peek(s) != 41) { s.pos = start return null } return normalizeUrl(unescape(s.subject.slice(start, s.pos))) } fn parseTitle(mut s: State): string? { let q = peek(s) if q != 34 && q != 39 && q != 40 { return null } let close = q == 40 ? 41 : q let mut i = s.pos + 1 while true { let c = (s.subject.codeUnitAt(i) ?? -1) if c == -1 || (q == 40 && c == 40) { return null } if c == close { let title = unescape(s.subject.slice(s.pos + 1, i)) s.pos = i + 1 return title } i += isEscapeAt(s, i) ? 2 : 1 } return null } /// The length of the link label at the position (brackets included), or 0. fn parseLabel(s: State): int { if peek(s) != 91 { return 0 } let mut i = s.pos + 1 while i - s.pos <= 1001 { let c = s.subject.codeUnitAt(i) ?? -1 if c == -1 || c == 91 { return 0 } if c == 93 { return i + 1 - s.pos } i += c == 92 ? 2 : 1 } return 0 } fn parseOpenBracket(mut s: State) { let start = s.pos s.pos += 1 let node = pushText(&s, "[") addBracket(&s, node, start, false) } fn parseBang(mut s: State) { let start = s.pos s.pos += 1 if peek(s) == 91 { s.pos += 1 let node = pushText(&s, "![") addBracket(&s, node, start + 1, true) } else { pushText(&s, "!") } } fn parseCloseBracket(mut s: State, refs: Map, mut budget: int[]) { s.pos += 1 let start = s.pos let top = s.bracketTop if top == -1 { pushText(&s, "]") return } if !s.brackets[top].active { pushText(&s, "]") removeBracket(&s) return } let image = s.brackets[top].image let mut url: string? = null let mut title: string? = null let mut matched = false let save = s.pos if peek(s) == 40 { s.pos += 1 skipLinkSpace(&s) let dest = parseDestination(&s) if dest != null { let beforeSpace = s.pos skipLinkSpace(&s) if !(s.pos == beforeSpace) { title = parseTitle(&s) skipLinkSpace(&s) } if peek(s) == 41 { s.pos += 1 url = dest matched = true } } if !matched { s.pos = save title = null } } if !matched { let before = s.pos let n = parseLabel(s) let mut label: string? = null if n > 2 { label = s.subject.slice(before, before + n) } else if !s.brackets[top].bracketAfter { label = s.subject.slice(s.brackets[top].index, start) } if n == 0 { s.pos = save } else { s.pos = before + n } if label != null { let found = refs.get(normalizeLabel(label.slice(1, label.length - 1))) if found != null { // A definition can be used many times: the total text they expand to is bounded, against a quadratic document. let cost = found.url.length + (found.title ?? "").length if budget[0] >= cost { budget[0] -= cost url = found.url title = found.title matched = true } } } if !matched { s.pos = start } } if !matched { removeBracket(&s) pushText(&s, "]") return } let opener = s.brackets[top].node let id = add(&s, image ? "image" : "link", "") s.nodes[id].url = url ?? "" s.nodes[id].title = title moveChildren(&s, opener, -1, id) append(&s, 0, id) let bottom = s.brackets[top].prevDelim processEmphasis(&s, bottom) removeBracket(&s) unlink(&s, opener) if !image { let mut b = s.bracketTop while b != -1 { if !s.brackets[b].image { s.brackets[b].active = false } b = s.brackets[b].prev } } } // ---- the main loop ---- fn parseEntity(mut s: State) { let found = scanEntity(s.subject, s.pos) if found == null { s.pos += 1 pushText(&s, "&") } else { pushText(&s, found[0]) s.pos = found[1] } } fn isSpecial(c: int, gfm: boolean): boolean { return c == 10 || c == 92 || c == 96 || c == 42 || c == 95 || c == 91 || c == 93 || c == 33 || c == 60 || c == 38 || (gfm && c == 126) } fn parseString(mut s: State) { let start = s.pos let end = textEnd(s.subject, start, s.gfm) let text = s.subject.slice(start, end) s.pos = end pushText(&s, text) } fn run(mut s: State, refs: Map, mut budget: int[]) { let n = s.subject.length while s.pos < n { let c = peek(s) if c == 10 { parseNewline(&s) } else if c == 92 { parseBackslash(&s) } else if c == 96 { parseBackticks(&s) } else if c == 42 || c == 95 || isTilde(s, c) { handleDelim(&s, c) } else if c == 91 { parseOpenBracket(&s) } else if c == 33 { parseBang(&s) } else if c == 93 { parseCloseBracket(&s, refs, &budget) } else if c == 60 { parseLessThan(&s) } else if c == 38 { parseEntity(&s) } else { parseString(&s) } } processEmphasis(&s, -1) } // ---- GFM's bare autolinks ---- fn isBoundary(c: int): boolean { return c == -1 || isUnicodeSpace(c) || c == 42 || c == 95 || c == 126 || c == 40 } /// Where a bare link's text ends: GFM's rules for trailing punctuation, closing parentheses and entities. fn trimAutolink(text: string, start: int, stop: int): int { let mut open = 0 let mut close = 0 for i of start..stop { let u = text.codeUnitAt(i) ?? 0 if u == 40 { open += 1 } else if u == 41 { close += 1 } } let mut end = stop while end > start { let c = text.codeUnitAt(end - 1) ?? 0 if c == 63 || c == 33 || c == 46 || c == 44 || c == 58 || c == 42 || c == 95 || c == 126 || c == 39 || c == 34 { end -= 1 } else if c == 41 { if close <= open { return end } close -= 1 end -= 1 } else if c == 59 { let mut i = end - 2 while i > start && isAlnum(text.codeUnitAt(i) ?? 0) { i -= 1 } if i < end - 2 && text.codeUnitAt(i) == 38 { end = i } else { return end } } else { return end } } return end } fn linkEnd(text: string, from: int): int { let mut i = from while i < text.length { let c = text.codeUnitAt(i) ?? 32 if isUnicodeSpace(c) || c == 60 { break } i += 1 } return i } /// The end of the valid domain at `from`, or -1. The last two segments can't hold an underscore. fn domainEnd(text: string, from: int): int { let mut i = from let mut dots = 0 let mut underscoreLast = false let mut underscorePrev = false while i < text.length { let c = text.codeUnitAt(i) ?? 32 if c == 46 { if !isAlnumOrDash(text.codeUnitAt(i + 1) ?? 32) { break } dots += 1 underscorePrev = underscoreLast underscoreLast = false } else if c == 95 { underscoreLast = true } else if !isAlnumOrDash(c) { break } i += 1 } if i == from || underscoreLast || underscorePrev { return -1 } return dots > 0 ? i : -2 - i } fn isAlnumOrDash(c: int): boolean { return isAlnum(c) || c == 45 || c == 95 || c >= 128 } fn matchAt(text: string, word: string, at: int): boolean { return text.slice(at, at + word.length).toLowerCase() == word } /// A bare link starting at `i`: the end of its text and the address, or `null`. fn bareLink(text: string, i: int): [int, string]? { if matchAt(text, "www.", i) { let d = domainEnd(text, i) if d < 0 { return null } let end = trimAutolink(text, i, linkEnd(text, d)) return [end, "http://" + text.slice(i, end)] } let schemes: string[] = ["https://", "http://", "ftp://"] for scheme of schemes { if matchAt(text, scheme, i) { let d = domainEnd(text, i + scheme.length) if d == -1 || d == i + scheme.length { return null } let stop = d < 0 ? -2 - d : d let end = trimAutolink(text, i, linkEnd(text, stop)) return [end, text.slice(i, end)] } } return null } fn isEmailChar(c: int): boolean { return isAlnum(c) || c == 46 || c == 45 || c == 95 || c == 43 } /// The email address around the `@` at `at`: its start, end and address, or `null`. fn bareEmail(text: string, at: int, floor: int): [int, int, string]? { let mut start = at while start > floor && isEmailChar(text.codeUnitAt(start - 1) ?? 32) { start -= 1 } if start == at { return null } // `mailto:` and `xmpp:` written out are part of the link. let mut scheme = "" let words: string[] = ["mailto:", "xmpp:"] for word of words { if start - word.length >= floor && matchAt(text, word, start - word.length) { scheme = word } } let mut i = at + 1 let mut dots = 0 while isAlnum(text.codeUnitAt(i) ?? 32) || text.codeUnitAt(i) == 45 || text.codeUnitAt(i) == 95 || text.codeUnitAt(i) == 46 { if text.codeUnitAt(i) == 46 { dots += 1 } i += 1 } while i > at + 1 && text.codeUnitAt(i - 1) == 46 { i -= 1 dots -= 1 } let last = text.codeUnitAt(i - 1) ?? 45 if dots < 1 || i == at + 1 || last == 45 || last == 95 { return null } if scheme == "xmpp:" && text.codeUnitAt(i) == 47 { let mut k = i + 1 while isAlnum(text.codeUnitAt(k) ?? 32) || text.codeUnitAt(k) == 46 || text.codeUnitAt(k) == 64 { k += 1 } while k > i + 1 && text.codeUnitAt(k - 1) == 46 { k -= 1 } i = k } let begin = start - scheme.length return [begin, i, scheme == "" ? "mailto:" + text.slice(start, i) : text.slice(begin, i)] } fn autolinks(text: string): Inline[] { if !text.includes(".") { return [Str { text }] } let mut out: Inline[] = [] let mut from = 0 let mut i = 0 while i < text.length { let c = text.codeUnitAt(i) ?? 0 if c == 64 { let email = bareEmail(text, i, from) if email != null { let [begin, end, address] = email if begin > from { out.push(Str { text: text.slice(from, begin) }) } out.push( Link { url: normalizeUrl(address), title: null, children: [Str { text: text.slice(begin, end) }], }, ) from = end i = end continue } } else if (c == 119 || c == 87 || c == 104 || c == 72 || c == 102 || c == 70) && isBoundary(i == 0 ? -1 : text.codeUnitAt(i - 1) ?? -1) { let link = bareLink(text, i) if link != null { let [end, address] = link if i > from { out.push(Str { text: text.slice(from, i) }) } out.push( Link { url: normalizeUrl(address), title: null, children: [Str { text: text.slice(i, end) }], }, ) from = end i = end continue } } i += 1 } if from < text.length { out.push(Str { text: text.slice(from) }) } return out } // ---- from the node list to the tree ---- fn children(s: State, parent: int, depth: int, inLink: boolean): Inline[] { // `convert` stops at MAX_DEPTH, so this recursion is bounded whatever the input. let mut out: Inline[] = [] let mut text: string[] = [] let mut at = s.nodes[parent].first while at != -1 { let n = s.nodes[at] if n.kind == "text" { text.push(n.text) } else { flush(&out, &text, s.gfm && !inLink) convert(s, at, depth, inLink, &out) } at = n.next } flush(&out, &text, s.gfm && !inLink) return out } fn flush(mut out: Inline[], mut text: string[], links: boolean) { if text.length == 0 { return } let joined = text.join("") text.clear() if joined == "" { return } if links { out.push(...autolinks(joined)) } else { out.push(Str { text: joined }) } } /// The text under a node, walked without recursion: what nesting past `MAX_DEPTH` becomes. fn flatText(s: State, root: int): string { let mut parts: string[] = [] let mut at = s.nodes[root].first while at != -1 { let n = s.nodes[at] if n.kind == "text" || n.kind == "code" || n.kind == "html" { parts.push(n.text) } else if n.kind == "soft" || n.kind == "hard" { parts.push(" ") } if n.first != -1 { at = n.first } else { while at != root && s.nodes[at].next == -1 { at = s.nodes[at].parent } at = at == root ? -1 : s.nodes[at].next } } return parts.join("") } fn convert(s: State, id: int, depth: int, inLink: boolean, mut out: Inline[]): void { let n = s.nodes[id] if depth >= MAX_DEPTH && n.first != -1 { out.push(Str { text: flatText(s, id) }) return } match n.kind { "soft" => out.push(SoftBreak), "hard" => out.push(HardBreak), "code" => out.push(CodeSpan { code: n.text }), "html" => out.push(HtmlInline { html: n.text }), "emph" | "strong" | "del" | "link" | "image" => { let linked = inLink || n.kind == "link" || n.kind == "image" let kids = children(s, id, depth + 1, linked) if n.kind == "emph" { out.push(Emphasis { children: kids }) } else if n.kind == "strong" { out.push(Strong { children: kids }) } else if n.kind == "del" { out.push(Strikethrough { children: kids }) } else if n.kind == "link" { out.push(Link { url: n.url, title: n.title, children: kids }) } else { out.push(Picture { url: n.url, title: n.title, alt: kids }) } }, _ => {}, } } /// Parses inline content. `budget[0]` is how much text reference links may still expand to. export fn parseInlines( text: string, refs: Map, gfm: boolean, mut budget: int[], ): Inline[] { let mut s = newState(text.trim(), gfm) run(&s, refs, &budget) return children(s, 0, 0, false) } /// Reads the link reference definition at `start` into `refs`: where it ends, or `start` when there is none. export fn parseReference(text: string, start: int, mut refs: Map): int { let mut s = newState(text, false) s.pos = start let n = parseLabel(s) if n == 0 || text.codeUnitAt(start + n) != 58 { return start } let raw = text.slice(start + 1, start + n - 1) s.pos = start + n + 1 skipLinkSpace(&s) if peek(s) == -1 { return start } let url = parseDestination(&s) if url == null { return start } let beforeTitle = s.pos skipLinkSpace(&s) let mut title: string? = null if !(s.pos == beforeTitle) { title = parseTitle(&s) } if title == null { s.pos = beforeTitle } let mut atEnd = lineEnds(&s) if !atEnd && title != null { title = null s.pos = beforeTitle atEnd = lineEnds(&s) } if !atEnd { return start } let label = normalizeLabel(raw) if label == "" { return start } if !refs.has(label) { refs.set(label, { url, title }) } return s.pos } fn lineEnds(mut s: State): boolean { let start = s.pos while isSpaceOrTab(peek(s)) { s.pos += 1 } let c = peek(s) if c == 10 { s.pos += 1 return true } if c == -1 { return true } s.pos = start return false }