Chat replies, approvals, plans and agent transcripts now parse GitHub flavoured Markdown with unified (remark-parse, remark-gfm, remark-rehype) and render the HTML tree as Vue nodes. Raw HTML is dropped, links and images keep only safe URLs, and #N task references stay linked. The Astryx React prototype, its dependencies, scripts, theme file and agent notes are removed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
import type { Element, ElementContent, Root, RootContent } from "hast";
|
|
import remarkGfm from "remark-gfm";
|
|
import remarkParse from "remark-parse";
|
|
import remarkRehype from "remark-rehype";
|
|
import { unified } from "unified";
|
|
|
|
/**
|
|
* Model-written Markdown (GitHub flavoured) parsed into an HTML syntax tree
|
|
* that ChatMarkdown renders as Vue nodes. Raw HTML in the source is dropped,
|
|
* links and images keep only safe URLs, and a #N outside code and links
|
|
* becomes a `task-ref` element.
|
|
*/
|
|
const processor = unified().use(remarkParse).use(remarkGfm).use(remarkRehype);
|
|
|
|
// A #N the model wrote about a task. Not part of a word, entity or path.
|
|
const TASK_REF = /(?<![\w&#/])#(\d{1,9})(?!\w)/g;
|
|
const SAFE_HREF = /^(?:https?:\/\/|\/(?!\/)|#)/i;
|
|
const SAFE_SRC = /^(?:https?:\/\/|\/(?!\/)|data:image\/)/i;
|
|
|
|
export function parseMarkdown(text: string | null | undefined): Root {
|
|
const tree = processor.runSync(processor.parse(text ?? "")) as Root;
|
|
tree.children = clean(tree.children, false) as RootContent[];
|
|
return tree;
|
|
}
|
|
|
|
function clean(nodes: Array<RootContent | ElementContent>, literal: boolean): ElementContent[] {
|
|
const out: ElementContent[] = [];
|
|
for (const node of nodes) {
|
|
if (node.type === "text") {
|
|
out.push(...(literal ? [node] : taskRefs(node.value)));
|
|
} else if (node.type === "element") {
|
|
const kept = safeElement(node);
|
|
if (!kept) continue;
|
|
if (kept !== node) {
|
|
out.push(...clean(kept.children, literal));
|
|
continue;
|
|
}
|
|
const inner = literal || ["a", "code", "pre"].includes(node.tagName);
|
|
node.children = clean(node.children, inner);
|
|
out.push(node);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// safeElement returns the element, null to drop it, or a wrapper whose
|
|
// children replace a link with an unsafe URL.
|
|
function safeElement(node: Element): Element | null {
|
|
if (node.tagName === "img") {
|
|
return SAFE_SRC.test(String(node.properties.src ?? "")) ? node : null;
|
|
}
|
|
if (node.tagName === "a" && !SAFE_HREF.test(String(node.properties.href ?? ""))) {
|
|
return { ...node, tagName: "span", properties: {} };
|
|
}
|
|
return node;
|
|
}
|
|
|
|
function taskRefs(text: string): ElementContent[] {
|
|
const out: ElementContent[] = [];
|
|
let cursor = 0;
|
|
for (const match of text.matchAll(TASK_REF)) {
|
|
if (match.index > cursor) out.push({ type: "text", value: text.slice(cursor, match.index) });
|
|
out.push({
|
|
type: "element",
|
|
tagName: "task-ref",
|
|
properties: { id: match[1] },
|
|
children: [],
|
|
});
|
|
cursor = match.index + match[0].length;
|
|
}
|
|
if (cursor < text.length) out.push({ type: "text", value: text.slice(cursor) });
|
|
return out;
|
|
}
|