Files
chatkit/dist/markdown.js
T
CatoandClaude Opus 5.5 ed759a8516 feat: chatkit v0.1 — one chat for every product (#1)
* feat: chatkit v0.1 — one chat for every product

The fleet had 13 chats in 10 repos, each written fresh, each missing what
another had fixed. This extracts loki /loki (composer, thread, messages,
markdown, attachments) and adds heidi's microphone and orangecat's keyboard fix:

- Composer: auto-growing 16px textarea, Enter/Shift+Enter/IME, Send and Stop
  in one slot, attachments with paste, mic always present, every label
  translatable, app-specific controls in slots.
- useDictation: browser recogniser first; missing or SILENT (4 s watchdog,
  bounded permission wait) -> record and transcribe on the server; a dead
  recogniser remembered 30 days; every failure shown in words.
- ChatThread/ChatMessage: stick-to-bottom + jump button, speaker names once two
  agents speak, Copy on answers, Retry on the last one, failed turns visible.
- Markdown with no HTML path; javascript: links stay text.
- styles.css: --ck-* variables defaulting to @bitbaum/design-tokens.
- verify = format, lint, types, build, 18 unit/render tests and 21 browser
  checks (390px + desktop, light + dark, a sentence through a fake mic past a
  silent recogniser). Browser checks mutation-tested (14px input, 60 s
  watchdog) — both fail as they should.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(styles): --ck-* defaults at zero specificity on :root, so an app's own values win

Declared on the chat's elements they overrode any app setting --ck-* at the
root — the opposite of what the README promised. Found preparing loki, whose
colour variables are full OKLCH colours under the design-token names and so
must map them. New browser check: a :root --ck-accent reaches the chat.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 23:14:53 -07:00

127 lines
4.7 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) {
out.push({ kind: "strong", text: part.slice(2, -2) });
}
else if (/^\*[^*\s][^*]*\*$/.test(part)) {
out.push({ kind: "em", text: part.slice(1, -1) });
}
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