mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-23 04:09:15 +08:00
feat(desktop): 对话 markdown 代码块接入 hljs 高亮,复用代码预览注册表与配色
This commit is contained in:
1 parent
16dfce860b
commit
22222bbc97
3 files changed
+68
-2
No files matched your search
@@ -40,8 +40,24 @@ const EXT_LANG: Record<string, string> = {
|
||||
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 的 <span>),沿用与 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 按行拆分:跨行的 <span>(块注释/模板串)在行尾闭合、次行重开,
|
||||
* 使每行成为独立合法片段——行号采用逐行 flex 行(与 DiffPanel 同构),
|
||||
* pre-wrap 折行时行号才能与内容对齐(整体 gutter 会错位)。 */
|
||||
|
||||
@@ -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 `<div class="mdcode">${baseRenderer.code(token)}<button class="mdcopy" type="button">复制</button></div>`;
|
||||
const lang = (token.lang ?? "").trim().split(/\s+/)[0]?.toLowerCase();
|
||||
const highlighted = highlightFence(token.text, lang);
|
||||
const body =
|
||||
highlighted !== null
|
||||
? `<pre><code class="hljs language-${lang}">${highlighted}</code></pre>`
|
||||
: baseRenderer.code(token);
|
||||
return `<div class="mdcode${highlighted !== null ? " hl" : ""}">${body}<button class="mdcopy" type="button">复制</button></div>`;
|
||||
},
|
||||
table(token) {
|
||||
const table = baseRenderer.table.call(this, token);
|
||||
|
||||
@@ -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("<script");
|
||||
});
|
||||
|
||||
it("未标语言/未收录语言回落纯文本转义路径,不挂 .hl", () => {
|
||||
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 = "<img src=x onerror=alert(1)>";', "ts") ?? "";
|
||||
const tags = out.match(/<[^>]+>/g) ?? [];
|
||||
expect(tags.length).toBeGreaterThan(0);
|
||||
expect(tags.every((t) => /^<\/?span[ >]/.test(t) || t === "</span>")).toBe(true);
|
||||
expect(out).toContain("<img");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user