Files
chatkit/dist/react/Markdown.js
T
CatoandClaude Opus 5.5 f2bc87db05 fix(markdown): a link inside bold or italic is still a link (0.2.1) (#4)
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>
2026-10-02 06:22:27 -07:00

62 lines
3.5 KiB
JavaScript

import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { isInternalHref, parseBlocks, parseInline } from "../markdown.js";
/** A plain anchor: same-site paths in place, everything else in a new tab. */
export const defaultRenderLink = ({ href, internal, className, children }) => internal ? (_jsx("a", { href: href, className: className, children: children })) : (_jsx("a", { href: href, className: className, target: "_blank", rel: "noopener noreferrer", children: children }));
function Inline({ spans, citations, renderLink, }) {
return (_jsx(_Fragment, { children: spans.map((s, i) => {
switch (s.kind) {
case "code":
return (_jsx("code", { className: "ck-code", children: s.text }, i));
case "strong":
case "em": {
// Plain emphasis stays plain markup; only what holds a link, code
// or a citation is rendered as nested spans.
const plain = s.children.every((c) => c.kind === "text");
const inner = plain ? (s.text) : (_jsx(Inline, { spans: s.children, citations: citations, renderLink: renderLink }));
return s.kind === "strong" ? (_jsx("strong", { children: inner }, i)) : (_jsx("em", { children: inner }, i));
}
case "link":
return (_jsx("span", { children: renderLink({
href: s.href,
internal: isInternalHref(s.href),
className: "ck-link",
children: s.text,
}) }, i));
case "cite": {
const known = s.ids.filter((id) => citations?.[id]);
if (known.length === 0)
return null;
return (_jsx("sup", { className: "ck-cite", title: known
.map((id) => `${id} — ${citations[id].label}\n${citations[id].detail}`)
.join("\n\n"), children: known.join(" ") }, i));
}
default:
return _jsx("span", { children: s.text }, i);
}
}) }));
}
/**
* Model output as readable text: paragraphs, lists, headings, bold, italic,
* code, links and citations. Never HTML — nothing here can inject markup.
*/
export function Markdown({ text, citations, renderLink = defaultRenderLink, className, }) {
const inline = (t) => (_jsx(Inline, { spans: parseInline(t), citations: citations, renderLink: renderLink }));
return (_jsx("div", { className: className ? `ck-md ${className}` : "ck-md", children: parseBlocks(text).map((b, i) => {
switch (b.kind) {
case "code":
return (_jsx("pre", { className: "ck-pre", "data-lang": b.lang || undefined, children: _jsx("code", { children: b.code }) }, i));
case "ul":
case "ol": {
const List = b.kind;
return (_jsx(List, { children: b.items.map((item, j) => (_jsx("li", { children: inline(item) }, j))) }, i));
}
case "h":
return (_jsx("p", { className: "ck-md-h", children: inline(b.text) }, i));
case "quote":
return _jsx("blockquote", { children: inline(b.text) }, i);
default:
return _jsx("p", { children: inline(b.text) }, i);
}
}) }));
}
//# sourceMappingURL=Markdown.js.map