feat: 实现动态 DOCX 模板与样式映射

This commit is contained in:
SkyJourney
2026-07-30 14:10:43 +08:00
parent fe5d67fb6d
commit fa159d916f
41 changed files with 3610 additions and 7 deletions
@@ -0,0 +1,67 @@
import { describe, expect, it } from "vitest";
import type { ThemeManifest } from "@md-to-pdf/core";
import {
inferDocxStylePreset,
resolveDocxThemeStyle
} from "../src/index.js";
function manifest(
overrides: Partial<ThemeManifest> = {}
): ThemeManifest {
return {
manifestVersion: 1,
id: "test-theme",
name: "测试主题",
version: "1.0.0",
description: "测试",
author: "测试",
license: "内部许可",
entry: "theme.css",
domPreset: "generic",
defaultFontSize: "16px",
supportedFeatures: [],
category: "general",
compatibleProfiles: [],
bundled: true,
...overrides
};
}
describe("DOCX 样式预设", () => {
it("按主题分类提供兼容回退", () => {
expect(inferDocxStylePreset(manifest())).toBe("general");
expect(
inferDocxStylePreset(manifest({ category: "red-letter" }))
).toBe("official");
expect(
resolveDocxThemeStyle(
manifest({ category: "formal" })
).body.firstLineIndentChars
).toBe(2);
});
it("将主题覆盖合并到完整预设", () => {
const resolved = resolveDocxThemeStyle(
manifest({
docxStyle: {
preset: "tender",
body: {
sizePt: 13,
color: "#112233"
},
headings: {
color: "#334455"
}
}
})
);
expect(resolved.body).toMatchObject({
sizePt: 13,
color: "#112233",
lineSpacing: 1.5
});
expect(resolved.headings.color).toBe("#334455");
expect(resolved.code.fonts.latin).toBe("Consolas");
});
});