diff --git a/desktop/ui/src/codeView.tsx b/desktop/ui/src/codeView.tsx index 772824dd..fd05e7b6 100644 --- a/desktop/ui/src/codeView.tsx +++ b/desktop/ui/src/codeView.tsx @@ -40,8 +40,24 @@ const EXT_LANG: Record = { sh: "bash", bash: "bash", zsh: "bash", yml: "yaml", yaml: "yaml", sql: "sql", ini: "ini", toml: "ini", conf: "ini", + // 以下是 markdown 围栏语言标记专用的别名(不是文件扩展名) + "c++": "cpp", shell: "bash", console: "bash", golang: "go", }; +/** markdown 围栏代码块的高亮:语言标记经别名表/注册表解析,未收录或 + * 高亮失败返回 null(调用方回落纯文本转义路径)。输出是 hljs 的 HTML + * (文本已转义,只含 hljs 的 ),沿用与 CodeView 相同的安全前提。 */ +export function highlightFence(code: string, lang?: string): string | null { + if (!lang) return null; + const resolved = EXT_LANG[lang] ?? lang; + if (!hljs.getLanguage(resolved)) return null; + try { + return hljs.highlight(code, { language: resolved }).value; + } catch { + return null; + } +} + /** 高亮 HTML 按行拆分:跨行的 (块注释/模板串)在行尾闭合、次行重开, * 使每行成为独立合法片段——行号采用逐行 flex 行(与 DiffPanel 同构), * pre-wrap 折行时行号才能与内容对齐(整体 gutter 会错位)。 */ diff --git a/desktop/ui/src/markdown.tsx b/desktop/ui/src/markdown.tsx index 018c9a99..fa962ae5 100644 --- a/desktop/ui/src/markdown.tsx +++ b/desktop/ui/src/markdown.tsx @@ -8,18 +8,27 @@ import { type CSSProperties, type MouseEvent as ReactMouseEvent, } from "react"; +import { highlightFence } from "./codeView"; import { openExternal } from "./host"; import { resolveMarkdownResource } from "./markdownPaths"; marked.setOptions({ gfm: true, breaks: true }); // 代码块包一层容器并附复制按钮;按钮点击走 .md 容器的事件代理 -// (innerHTML 注入的 DOM 挂不了 React handler) +// (innerHTML 注入的 DOM 挂不了 React handler)。围栏带语言标记时走 +// hljs 高亮(codeView 同一注册表与配色令牌,容器加 .hl 复用样式), +// 未标注/未收录语言回落 baseRenderer 的纯文本转义路径。 const baseRenderer = new marked.Renderer(); marked.use({ renderer: { code(token) { - return `
${baseRenderer.code(token)}
`; + const lang = (token.lang ?? "").trim().split(/\s+/)[0]?.toLowerCase(); + const highlighted = highlightFence(token.text, lang); + const body = + highlighted !== null + ? `
${highlighted}
` + : baseRenderer.code(token); + return `
${body}
`; }, table(token) { const table = baseRenderer.table.call(this, token); diff --git a/desktop/ui/src/markdownHighlight.test.ts b/desktop/ui/src/markdownHighlight.test.ts new file mode 100644 index 00000000..51104bed --- /dev/null +++ b/desktop/ui/src/markdownHighlight.test.ts @@ -0,0 +1,41 @@ +// 围栏代码块的 hljs 高亮:语言标记解析、别名、未收录语言的纯文本回落。 +import { marked } from "marked"; +import { describe, expect, it } from "vitest"; + +import "./components"; +import { highlightFence } from "./codeView"; + +describe("markdown 代码块高亮", () => { + it("带语言标记的围栏走 hljs,容器挂 .hl 复用代码预览配色", () => { + const html = marked.parse('```ts\nconst x: number = 1;\n```', { async: false }) as string; + expect(html).toContain('class="mdcode hl"'); + expect(html).toContain('class="hljs language-ts"'); + expect(html).toContain("hljs-keyword"); + // 文本仍被转义(hljs 输出只含它自己的 span) + expect(html).not.toContain(" { + for (const fence of ["```\nplain text\n```", "```brainfuck\n+++\n```"]) { + const html = marked.parse(fence, { async: false }) as string; + expect(html).toContain('class="mdcode"'); + expect(html).not.toContain('class="mdcode hl"'); + expect(html).not.toContain("hljs-"); + } + }); + + it("围栏别名与附加参数:首段小写解析,c++/shell 这类映射到注册语言", () => { + expect(highlightFence("echo hi", "shell")).toContain("hljs-built_in"); + expect(highlightFence("int a;", "c++")).not.toBeNull(); + const html = marked.parse('```JS title=demo\nconst a = 1\n```', { async: false }) as string; + expect(html).toContain("hljs-keyword"); + }); + + it("高亮输出除 span 外不引入任何标签(注入面不变)", () => { + const out = highlightFence('const s = "";', "ts") ?? ""; + const tags = out.match(/<[^>]+>/g) ?? []; + expect(tags.length).toBeGreaterThan(0); + expect(tags.every((t) => /^<\/?span[ >]/.test(t) || t === "
")).toBe(true); + expect(out).toContain("<img"); + }); +});