feat(ui/chat): render Markdown with remark and rehype; remove the React reference
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>
This commit is contained in:
21 files changed
+679
-3315
No files matched your search
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* @generated by `astryx theme` — do not edit manually.
|
||||
* @astryx-theme-record {"themes":{"neutral":"@astryxdesign/theme-neutral"},"defaultThemeSlug":"neutral"}
|
||||
*/
|
||||
|
||||
import {neutralTheme as _astryxTheme0} from "@astryxdesign/theme-neutral/built";
|
||||
import "@astryxdesign/theme-neutral/theme.css";
|
||||
import "@astryxdesign/theme-neutral/fonts.css";
|
||||
|
||||
export const themes = {
|
||||
"neutral": _astryxTheme0,
|
||||
} as const;
|
||||
export type ThemeSlug = keyof typeof themes;
|
||||
|
||||
export const defaultThemeSlug: keyof typeof themes = "neutral";
|
||||
@@ -1,37 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import ChatTaskRef from "./ChatTaskRef.vue";
|
||||
import type { ChatInline } from "./markdown";
|
||||
|
||||
/** One inline run: the spans of a paragraph, a heading or a table cell. */
|
||||
defineProps<{ inline: ChatInline[] }>();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<template v-for="(span, i) in inline" :key="i">
|
||||
<img
|
||||
v-if="span.kind === 'image'"
|
||||
:src="span.src"
|
||||
:alt="span.alt"
|
||||
loading="lazy"
|
||||
class="my-1 block max-h-96 max-w-full cursor-pointer rounded-md border border-border bg-card object-contain"
|
||||
/>
|
||||
<del v-else-if="span.kind === 'del'">{{ span.text }}</del>
|
||||
<strong v-else-if="span.kind === 'strong'">{{ span.text }}</strong>
|
||||
<code
|
||||
v-else-if="span.kind === 'code'"
|
||||
class="rounded bg-foreground/10 px-1 py-0.5 font-mono text-[0.9em]"
|
||||
>{{ span.text }}</code
|
||||
>
|
||||
<a
|
||||
v-else-if="span.kind === 'link'"
|
||||
:href="span.href"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
class="text-link underline-offset-4 hover:underline"
|
||||
>{{ span.text }}</a
|
||||
>
|
||||
<ChatTaskRef v-else-if="span.kind === 'taskref'" :id="span.id" />
|
||||
<em v-else-if="span.kind === 'em'">{{ span.text }}</em>
|
||||
<template v-else>{{ span.text }}</template>
|
||||
</template>
|
||||
</template>
|
||||
@@ -1,100 +1,78 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import type { Element, ElementContent } from "hast";
|
||||
import { computed, h, type VNodeChild } from "vue";
|
||||
|
||||
import CodeView from "@/base/CodeView.vue";
|
||||
import ChatInline from "./ChatInline.vue";
|
||||
import ChatTaskRef from "./ChatTaskRef.vue";
|
||||
import { parseMarkdown } from "./markdown";
|
||||
|
||||
/**
|
||||
* A reply rendered as semantic HTML: headings, lists, quotes, fenced code,
|
||||
* tables, inline emphasis. The parse is a computed, so a reply that grows a
|
||||
* token at a time re-renders from the text it now has rather than accumulating
|
||||
* markup.
|
||||
* A reply rendered as semantic HTML. The parse is a computed, so a reply that
|
||||
* grows a token at a time re-renders from the text it now has rather than
|
||||
* accumulating markup.
|
||||
*/
|
||||
const props = defineProps<{ text: string }>();
|
||||
|
||||
const blocks = computed(() => parseMarkdown(props.text));
|
||||
const heading = "mt-1 mb-2 leading-tight font-semibold tracking-[-0.02em]";
|
||||
const CLASSES: Record<string, string> = {
|
||||
h1: `${heading} text-xl`,
|
||||
h2: `${heading} text-lg`,
|
||||
h3: `${heading} text-base`,
|
||||
h4: `${heading} text-base`,
|
||||
h5: `${heading} text-base`,
|
||||
h6: `${heading} text-base`,
|
||||
p: "mb-2.5 last:mb-0",
|
||||
ul: "my-2.5 flex list-disc flex-col gap-1 pl-5",
|
||||
ol: "my-2.5 flex list-decimal flex-col gap-1 pl-5",
|
||||
blockquote: "my-2 border-l-[3px] border-primary pl-3 text-fg-muted",
|
||||
table: "my-3 w-full border-collapse text-[0.92em]",
|
||||
th: "border border-border bg-muted px-2 py-1.5 text-left font-semibold",
|
||||
td: "border border-border px-2 py-1.5 text-left",
|
||||
code: "rounded bg-foreground/10 px-1 py-0.5 font-mono text-[0.9em]",
|
||||
a: "text-link underline-offset-4 hover:underline",
|
||||
img: "my-1 block max-h-96 max-w-full rounded-md border border-border bg-card object-contain",
|
||||
hr: "my-3 border-border",
|
||||
};
|
||||
|
||||
function textOf(node: ElementContent): string {
|
||||
if (node.type === "text") return node.value;
|
||||
if (node.type === "element") return node.children.map(textOf).join("");
|
||||
return "";
|
||||
}
|
||||
|
||||
function codeBlock(pre: Element): VNodeChild {
|
||||
const code = pre.children.find(
|
||||
(child): child is Element => child.type === "element" && child.tagName === "code",
|
||||
);
|
||||
const classes = (code?.properties.className as string[] | undefined) ?? [];
|
||||
const lang = classes.find((name) => name.startsWith("language-"))?.slice(9) ?? "";
|
||||
return h(CodeView, {
|
||||
code: textOf(code ?? pre).replace(/\n$/, ""),
|
||||
lang,
|
||||
class: "my-2",
|
||||
});
|
||||
}
|
||||
|
||||
function render(node: ElementContent, key: number): VNodeChild {
|
||||
if (node.type === "text") return node.value;
|
||||
if (node.type !== "element") return null;
|
||||
if (node.tagName === "task-ref") return h(ChatTaskRef, { key, id: Number(node.properties.id) });
|
||||
if (node.tagName === "pre") return codeBlock(node);
|
||||
|
||||
const { className, ...attrs } = node.properties;
|
||||
const props: Record<string, unknown> = { key, ...attrs, class: CLASSES[node.tagName] ?? className };
|
||||
if (node.tagName === "a") Object.assign(props, { target: "_blank", rel: "noreferrer" });
|
||||
if (node.tagName === "img") props.loading = "lazy";
|
||||
return h(node.tagName, props, node.children.map(render));
|
||||
}
|
||||
|
||||
const content = computed(() =>
|
||||
parseMarkdown(props.text).children.map((node, i) => render(node as ElementContent, i)),
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="[overflow-wrap:anywhere]">
|
||||
<template v-for="(block, i) in blocks" :key="i">
|
||||
<CodeView v-if="block.kind === 'pre'" :code="block.code" :lang="block.lang" class="my-2" />
|
||||
|
||||
<table
|
||||
v-else-if="block.kind === 'table'"
|
||||
class="my-3 w-full border-collapse text-[0.92em]"
|
||||
>
|
||||
<thead>
|
||||
<tr>
|
||||
<th
|
||||
v-for="(cell, c) in block.head"
|
||||
:key="c"
|
||||
class="border border-border bg-muted px-2 py-1.5 text-left font-semibold"
|
||||
>
|
||||
<ChatInline :inline="cell" />
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="(row, r) in block.rows" :key="r">
|
||||
<td
|
||||
v-for="(cell, c) in row"
|
||||
:key="c"
|
||||
class="border border-border px-2 py-1.5 text-left"
|
||||
>
|
||||
<ChatInline :inline="cell" />
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<blockquote
|
||||
v-else-if="block.kind === 'blockquote'"
|
||||
class="my-2 border-l-[3px] border-primary pl-3 text-fg-muted"
|
||||
>
|
||||
<ChatInline :inline="block.inline" />
|
||||
</blockquote>
|
||||
|
||||
<h1
|
||||
v-else-if="block.kind === 'h1'"
|
||||
class="mt-1 mb-2 text-xl leading-tight font-semibold tracking-[-0.02em]"
|
||||
>
|
||||
<ChatInline :inline="block.inline" />
|
||||
</h1>
|
||||
<h2
|
||||
v-else-if="block.kind === 'h2'"
|
||||
class="mt-1 mb-2 text-lg leading-tight font-semibold tracking-[-0.02em]"
|
||||
>
|
||||
<ChatInline :inline="block.inline" />
|
||||
</h2>
|
||||
<h3
|
||||
v-else-if="block.kind === 'h3'"
|
||||
class="mt-1 mb-2 text-base leading-tight font-semibold tracking-[-0.02em]"
|
||||
>
|
||||
<ChatInline :inline="block.inline" />
|
||||
</h3>
|
||||
|
||||
<ul
|
||||
v-else-if="block.kind === 'ul'"
|
||||
class="my-2.5 flex list-disc flex-col gap-1 pl-5"
|
||||
>
|
||||
<li v-for="(item, n) in block.items" :key="n">
|
||||
<ChatInline :inline="item" />
|
||||
</li>
|
||||
</ul>
|
||||
<ol
|
||||
v-else-if="block.kind === 'ol'"
|
||||
class="my-2.5 flex list-decimal flex-col gap-1 pl-5"
|
||||
>
|
||||
<li v-for="(item, n) in block.items" :key="n">
|
||||
<ChatInline :inline="item" />
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<p v-else-if="block.kind === 'p'" class="mb-2.5 last:mb-0">
|
||||
<ChatInline :inline="block.inline" />
|
||||
</p>
|
||||
</template>
|
||||
<component :is="() => content" />
|
||||
</div>
|
||||
</template>
|
||||
+59
-191
@@ -1,205 +1,73 @@
|
||||
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";
|
||||
|
||||
/**
|
||||
* Chat markdown.
|
||||
*
|
||||
* Telegram-style rich blocks -- headings, lists, quotes, fenced code, tables,
|
||||
* inline emphasis/links/images -- parsed into a tree that ChatMarkdown renders
|
||||
* as semantic HTML. The parse is separate from the render so a streamed reply
|
||||
* can be re-rendered on every frame without re-deciding what the text means.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/** One span of an inline run: what a paragraph's text breaks down into. */
|
||||
export type ChatInline =
|
||||
| { kind: "text"; text: string }
|
||||
| { kind: "strong"; text: string }
|
||||
| { kind: "em"; text: string }
|
||||
| { kind: "del"; text: string }
|
||||
| { kind: "code"; text: string }
|
||||
| { kind: "link"; text: string; href: string }
|
||||
| { kind: "taskref"; id: number }
|
||||
| { kind: "image"; alt: string; src: string };
|
||||
|
||||
/** One block of the document. Lists carry their items as inline runs. */
|
||||
export type ChatBlock =
|
||||
| { kind: "p" | "h1" | "h2" | "h3" | "blockquote"; inline: ChatInline[] }
|
||||
| { kind: "ul" | "ol"; items: ChatInline[][] }
|
||||
| { kind: "pre"; code: string; lang: string }
|
||||
| { kind: "table"; head: ChatInline[][]; rows: ChatInline[][][] };
|
||||
|
||||
const INLINE =
|
||||
/(!\[[^\]]*\]\((?:https?:\/\/[^\s)]+|data:image\/[^\s)]+|\/[^\s)]+)\)|~~[^~]+~~|\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\[[^\]]+\]\((?:https?:\/\/[^\s)]+|\/[^\s)]+)\)|\*[^*]+\*|_[^_]+_)/g;
|
||||
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;
|
||||
|
||||
// plainText splits a run of plain text around its task references.
|
||||
function plainText(text: string): ChatInline[] {
|
||||
const out: ChatInline[] = [];
|
||||
let cursor = 0;
|
||||
for (const match of text.matchAll(TASK_REF)) {
|
||||
if (match.index > cursor)
|
||||
out.push({ kind: "text", text: text.slice(cursor, match.index) });
|
||||
out.push({ kind: "taskref", id: Number(match[1]) });
|
||||
cursor = match.index + match[0].length;
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (cursor < text.length) out.push({ kind: "text", text: text.slice(cursor) });
|
||||
return out;
|
||||
}
|
||||
|
||||
// inlineMarkdown returns the children of one line: plain text and the element
|
||||
// each matched span becomes, in order.
|
||||
function inlineMarkdown(text: string): ChatInline[] {
|
||||
const children: ChatInline[] = [];
|
||||
// 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 String(text).matchAll(INLINE)) {
|
||||
if (match.index > cursor)
|
||||
children.push(...plainText(text.slice(cursor, match.index)));
|
||||
const value = match[0];
|
||||
if (value.startsWith("![")) {
|
||||
const img = value.match(/^!\[([^\]]*)\]\((.+)\)$/);
|
||||
if (img)
|
||||
children.push({ kind: "image", alt: img[1] || "Image", src: img[2] });
|
||||
} else if (value.startsWith("~~")) {
|
||||
children.push({ kind: "del", text: value.slice(2, -2) });
|
||||
} else if (value.startsWith("**") || value.startsWith("__")) {
|
||||
children.push({ kind: "strong", text: value.slice(2, -2) });
|
||||
} else if (value.startsWith("`")) {
|
||||
children.push({ kind: "code", text: value.slice(1, -1) });
|
||||
} else if (value.startsWith("[")) {
|
||||
const link = value.match(
|
||||
/^\[([^\]]+)\]\((https?:\/\/[^\s)]+|\/[^\s)]+)\)$/,
|
||||
);
|
||||
if (link) children.push({ kind: "link", text: link[1], href: link[2] });
|
||||
} else {
|
||||
children.push({ kind: "em", text: value.slice(1, -1) });
|
||||
}
|
||||
cursor = match.index + value.length;
|
||||
}
|
||||
if (cursor < text.length)
|
||||
children.push(...plainText(text.slice(cursor)));
|
||||
return children;
|
||||
}
|
||||
|
||||
export function parseMarkdown(text: string | null | undefined): ChatBlock[] {
|
||||
const blocks: ChatBlock[] = [];
|
||||
const lines = String(text || "").split("\n");
|
||||
let paragraph: string[] = [];
|
||||
let list: { tag: "ul" | "ol"; items: string[] } | null = null;
|
||||
let code: string[] | null = null;
|
||||
let lang = "";
|
||||
|
||||
const flushParagraph = () => {
|
||||
if (paragraph.length) {
|
||||
blocks.push({ kind: "p", inline: inlineMarkdown(paragraph.join(" ")) });
|
||||
paragraph = [];
|
||||
}
|
||||
};
|
||||
const flushList = () => {
|
||||
if (!list) return;
|
||||
blocks.push({
|
||||
kind: list.tag,
|
||||
items: list.items.map((item) => inlineMarkdown(item)),
|
||||
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: [],
|
||||
});
|
||||
list = null;
|
||||
};
|
||||
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
const line = lines[index];
|
||||
if (line.startsWith("```")) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
if (code) {
|
||||
blocks.push({ kind: "pre", code: code.join("\n"), lang });
|
||||
code = null;
|
||||
} else {
|
||||
code = [];
|
||||
lang = line.slice(3).trim().split(/\s/)[0];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (code) {
|
||||
code.push(line);
|
||||
continue;
|
||||
}
|
||||
const headerCells = tableCells(line);
|
||||
const separatorCells =
|
||||
index + 1 < lines.length ? tableCells(lines[index + 1]) : null;
|
||||
if (
|
||||
headerCells &&
|
||||
separatorCells &&
|
||||
isTableSeparator(separatorCells) &&
|
||||
headerCells.length === separatorCells.length
|
||||
) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
const rows: string[][] = [];
|
||||
for (index += 2; index < lines.length; index++) {
|
||||
const cells = tableCells(lines[index]);
|
||||
if (!cells || cells.length !== headerCells.length) {
|
||||
index--;
|
||||
break;
|
||||
}
|
||||
rows.push(cells);
|
||||
}
|
||||
blocks.push({
|
||||
kind: "table",
|
||||
head: headerCells.map((cell) => inlineMarkdown(cell)),
|
||||
rows: rows.map((row) => row.map((cell) => inlineMarkdown(cell))),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (!line.trim()) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
continue;
|
||||
}
|
||||
const heading = line.match(/^(#{1,3})\s+(.+)$/);
|
||||
if (heading) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
blocks.push({
|
||||
kind: `h${heading[1].length}` as "h1" | "h2" | "h3",
|
||||
inline: inlineMarkdown(heading[2]),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const bullet = line.match(/^\s*[-*]\s+(.+)$/);
|
||||
const ordered = line.match(/^\s*\d+[.)]\s+(.+)$/);
|
||||
if (bullet || ordered) {
|
||||
flushParagraph();
|
||||
const tag = ordered ? "ol" : "ul";
|
||||
if (!list || list.tag !== tag) {
|
||||
flushList();
|
||||
list = { tag, items: [] };
|
||||
}
|
||||
list.items.push((bullet || ordered)![1]);
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith(">")) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
blocks.push({
|
||||
kind: "blockquote",
|
||||
inline: inlineMarkdown(line.replace(/^>\s?/, "")),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
paragraph.push(line);
|
||||
cursor = match.index + match[0].length;
|
||||
}
|
||||
flushParagraph();
|
||||
flushList();
|
||||
if (code) blocks.push({ kind: "pre", code: code.join("\n"), lang });
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
function tableCells(line: string): string[] | null {
|
||||
const value = String(line || "").trim();
|
||||
if (!value.includes("|")) return null;
|
||||
const unwrapped = value.replace(/^\|/, "").replace(/\|$/, "");
|
||||
const cells = unwrapped.split("|").map((cell) => cell.trim());
|
||||
return cells.length > 1 && cells.every((cell) => cell !== "") ? cells : null;
|
||||
}
|
||||
|
||||
function isTableSeparator(cells: string[]): boolean {
|
||||
return cells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
||||
if (cursor < text.length) out.push({ type: "text", value: text.slice(cursor) });
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user