import {
afterEach,
describe,
expect,
it,
vi
} from "vitest";
import {
mkdtemp,
mkdir,
rm,
writeFile
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
ApplicationRequestError,
createApplicationService
} from "../src/index.js";
let temporaryDirectory: string | undefined;
afterEach(async () => {
if (temporaryDirectory) {
await rm(temporaryDirectory, {
recursive: true,
force: true
});
}
temporaryDirectory = undefined;
});
async function createThemeFixture() {
temporaryDirectory = await mkdtemp(
join(tmpdir(), "md-to-pdf-application-")
);
const bundledRoot = join(temporaryDirectory, "bundled");
const localRoot = join(temporaryDirectory, "local");
const themeRoot = join(bundledRoot, "test-theme");
const sharedFontRoot = join(
bundledRoot,
"_shared",
"official-fonts"
);
await mkdir(join(themeRoot, "fonts"), { recursive: true });
await mkdir(sharedFontRoot, { recursive: true });
await mkdir(localRoot, { recursive: true });
await writeFile(
join(themeRoot, "theme.json"),
JSON.stringify({
manifestVersion: 1,
id: "test-theme",
name: "测试主题",
version: "1.0.0",
description: "共享应用服务测试主题",
author: "test",
license: "MIT",
entry: "theme.css",
domPreset: "typora",
defaultFontSize: "16px",
supportedFeatures: ["code", "table"],
pageDefaults: {
margins: {
top: "37mm",
right: "26mm",
bottom: "35mm",
left: "28mm"
}
},
bundled: true
}),
"utf8"
);
await writeFile(
join(themeRoot, "theme.css"),
[
"@font-face {",
" font-family: Test;",
" src: url('./fonts/test.woff2') format('woff2');",
"}",
"@font-face {",
" font-family: SharedTest;",
" src: url('theme-shared:official-fonts/shared.woff2') format('woff2');",
"}",
"#write { font-family: Test; }"
].join("\n"),
"utf8"
);
await writeFile(
join(themeRoot, "fonts", "test.woff2"),
Buffer.from([0, 1, 2, 3])
);
await writeFile(
join(sharedFontRoot, "shared.woff2"),
Buffer.from([4, 5, 6, 7])
);
return { bundledRoot, localRoot };
}
describe("共享应用服务", () => {
it("渲染安全 Markdown 文档", async () => {
const roots = await createThemeFixture();
const service = createApplicationService(roots);
const document = await service.render({
markdown: "# 文档\n\n",
language: "zh-CN"
});
expect(document.articleHtml).toContain('id="write"');
expect(document.articleHtml).not.toContain("