import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { describe, expect, it } from "vitest"; import { Markdown, parseMarkdown, safeHref } from "./markdown.tsx"; const html = (text: string) => renderToStaticMarkup(createElement(Markdown, { text })); describe("Markdown 安全子集", () => { it("块级:标题、列表、表格、代码块、引用、分隔线", () => { const src = [ "## 结论", "", "**当前订阅 2 个**,来源 `inventory.query`:", "", "| # | 标题 | 年份 |", "|---|:---|---:|", "| 1 | 沙丘2 | 2024 |", "| 2 | 沙丘:预言 | 2024 |", "", "- 缺 1080p", "- 缺 4K", "", "1. 先搜", "2. 再提交", "", "> 仅供参考", "", "---", "```json", '{"a":1}', "```", ].join("\n"); const blocks = parseMarkdown(src); expect(blocks.map((b) => b.type)).toEqual(["heading", "paragraph", "table", "list", "list", "quote", "hr", "code"]); const out = html(src); expect(out).toContain("", () => { const out = html( " [点我](javascript:alert(1)) [文档](https://example.com/a)", ); expect(out).not.toContain(" { const out = html("用 `a*b*c` 查询\n第二行 *强调*"); expect(out).toContain(""); expect(out).toContain("强调"); }); it("空文本不渲染", () => { expect(html(" \n\n")).toBe(""); }); });