fix: 修复预览与 PDF 渲染边界问题
This commit is contained in:
@@ -13,6 +13,15 @@ import sanitizeHtml from "sanitize-html";
|
||||
|
||||
export const RENDERER_VERSION = 1;
|
||||
|
||||
export class MarkdownDocumentParseError extends Error {
|
||||
readonly code = "INVALID_FRONT_MATTER";
|
||||
|
||||
constructor(message: string, options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
this.name = "MarkdownDocumentParseError";
|
||||
}
|
||||
}
|
||||
|
||||
export interface MarkdownDocumentMetadata {
|
||||
title: string;
|
||||
author: string;
|
||||
@@ -198,9 +207,10 @@ function parseFrontMatter(source: string) {
|
||||
return matter(source);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "未知错误";
|
||||
throw new Error(`无法解析 Markdown Front Matter:${message}`, {
|
||||
cause: error
|
||||
});
|
||||
throw new MarkdownDocumentParseError(
|
||||
`无法解析 Markdown Front Matter:${message}`,
|
||||
{ cause: error }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { renderMarkdown } from "../src/render-markdown.js";
|
||||
import {
|
||||
MarkdownDocumentParseError,
|
||||
renderMarkdown
|
||||
} from "../src/render-markdown.js";
|
||||
|
||||
describe("renderMarkdown", () => {
|
||||
it("渲染常用 Markdown 扩展并识别能力", () => {
|
||||
@@ -71,6 +74,17 @@ lang: zh-CN
|
||||
});
|
||||
});
|
||||
|
||||
it("使用稳定错误类型报告无效 Front Matter", () => {
|
||||
expect(() =>
|
||||
renderMarkdown(`---
|
||||
title: [未闭合
|
||||
---
|
||||
|
||||
# 文档
|
||||
`)
|
||||
).toThrow(MarkdownDocumentParseError);
|
||||
});
|
||||
|
||||
it("服务端渲染公式并保留 Mermaid 安全占位", () => {
|
||||
const result = renderMarkdown(`
|
||||
内联公式 $E=mc^2$。
|
||||
|
||||
Reference in New Issue
Block a user