mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-05 03:47:58 +01:00
Models bold their links all the time — **[Chalco](/markets/chalco)** — and the bold span held plain text, so the reader saw the raw markup (Substrata's Ask, 2026-10-02). Bold and italic now carry their content parsed again; plain emphasis renders exactly as before. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
129 lines
4.8 KiB
JavaScript
129 lines
4.8 KiB
JavaScript
/**
|
|
* The markdown an LLM actually writes, parsed into blocks and inline spans —
|
|
* no dependency, no HTML string, nothing to sanitise because nothing is ever
|
|
* rendered as HTML.
|
|
*
|
|
* From loki's `MarkdownText`, the one renderer in the fleet that four repos
|
|
* did NOT have: those rendered model output with the literal `**` still in it.
|
|
* Supports what answers use — paragraphs, bullet and numbered lists, headings,
|
|
* **bold**, *italic*, `code`, fenced code, [links](url) and `[F1]` citation
|
|
* handles — and deliberately nothing else.
|
|
*/
|
|
export function parseBlocks(text) {
|
|
const blocks = [];
|
|
let list = null;
|
|
const lines = text.split(/\r?\n/);
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const raw = lines[i] ?? "";
|
|
const line = raw.trim();
|
|
// ``` fenced code — verbatim until the closing fence (or the end, while
|
|
// it is still streaming in).
|
|
if (line.startsWith("```")) {
|
|
list = null;
|
|
const lang = line.slice(3).trim();
|
|
const code = [];
|
|
i++;
|
|
while (i < lines.length && !(lines[i] ?? "").trim().startsWith("```")) {
|
|
code.push(lines[i] ?? "");
|
|
i++;
|
|
}
|
|
blocks.push({ kind: "code", lang, code: code.join("\n") });
|
|
continue;
|
|
}
|
|
if (!line) {
|
|
list = null;
|
|
continue;
|
|
}
|
|
const bullet = /^[-*•]\s+(.*)$/.exec(line);
|
|
const numbered = /^\d+[.)]\s+(.*)$/.exec(line);
|
|
if (bullet || numbered) {
|
|
const kind = bullet ? "ul" : "ol";
|
|
if (!list || list.kind !== kind) {
|
|
list = { kind, items: [] };
|
|
blocks.push(list);
|
|
}
|
|
list.items.push((bullet ?? numbered)[1] ?? "");
|
|
continue;
|
|
}
|
|
list = null;
|
|
if (/^#{1,6}\s/.test(line)) {
|
|
blocks.push({ kind: "h", text: line.replace(/^#+\s+/, "") });
|
|
}
|
|
else if (line.startsWith(">")) {
|
|
// A model's `>` leaked into prose was one of orangecat's fixed bugs.
|
|
blocks.push({ kind: "quote", text: line.replace(/^>\s?/, "") });
|
|
}
|
|
else {
|
|
const prev = blocks[blocks.length - 1];
|
|
// A soft line break inside a paragraph continues it.
|
|
if (prev && prev.kind === "p" && (lines[i - 1] ?? "").trim())
|
|
prev.text += `\n${line}`;
|
|
else
|
|
blocks.push({ kind: "p", text: line });
|
|
}
|
|
}
|
|
return blocks;
|
|
}
|
|
const INLINE_RE = /(`[^`]+`|\*\*[^*]+\*\*|\*[^*\s][^*]*\*|\[[^\]]+\]\([^)\s]+\)|\[[FD]\d+(?:,\s*[FD]\d+)*\]|https?:\/\/[^\s<>()]+[^\s<>().,;:!?'"])/g;
|
|
export function parseInline(text) {
|
|
const out = [];
|
|
for (const part of text.split(INLINE_RE)) {
|
|
if (!part)
|
|
continue;
|
|
if (/^\[[FD]\d+(?:,\s*[FD]\d+)*\]$/.test(part)) {
|
|
out.push({ kind: "cite", ids: part.slice(1, -1).split(/,\s*/) });
|
|
}
|
|
else if (part.startsWith("`") && part.endsWith("`") && part.length > 1) {
|
|
out.push({ kind: "code", text: part.slice(1, -1) });
|
|
}
|
|
else if (part.startsWith("**") && part.endsWith("**") && part.length > 4) {
|
|
const inner = part.slice(2, -2);
|
|
out.push({ kind: "strong", text: inner, children: parseInline(inner) });
|
|
}
|
|
else if (/^\*[^*\s][^*]*\*$/.test(part)) {
|
|
const inner = part.slice(1, -1);
|
|
out.push({ kind: "em", text: inner, children: parseInline(inner) });
|
|
}
|
|
else {
|
|
const link = /^\[([^\]]+)\]\(([^)\s]+)\)$/.exec(part);
|
|
const href = link
|
|
? safeHref(link[2] ?? "")
|
|
: /^https?:\/\//.test(part)
|
|
? safeHref(part)
|
|
: null;
|
|
if (link && href)
|
|
out.push({ kind: "link", text: link[1] ?? href, href });
|
|
else if (!link && href)
|
|
out.push({ kind: "link", text: part, href });
|
|
else
|
|
out.push({ kind: "text", text: link ? (link[1] ?? part) : part });
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
/**
|
|
* Only http(s), mailto and same-site paths become links. A `javascript:` URL in
|
|
* model output is text, never a link — the answer is written by a model that
|
|
* may have read a hostile page.
|
|
*/
|
|
export function safeHref(href) {
|
|
const h = href.trim();
|
|
if (h.startsWith("/") && !h.startsWith("//"))
|
|
return h;
|
|
if (h.startsWith("#"))
|
|
return h;
|
|
try {
|
|
const u = new URL(h);
|
|
return u.protocol === "https:" || u.protocol === "http:" || u.protocol === "mailto:"
|
|
? u.href
|
|
: null;
|
|
}
|
|
catch {
|
|
return null;
|
|
}
|
|
}
|
|
/** A same-site path navigates in place; anything else opens a new tab. */
|
|
export function isInternalHref(href) {
|
|
return href.startsWith("/") || href.startsWith("#");
|
|
}
|
|
//# sourceMappingURL=markdown.js.map
|