feat: 将主题令牌接入 DOCX 动态模板

This commit is contained in:
SkyJourney
2026-07-31 08:43:26 +08:00
parent 459a079f3b
commit 6e40374200
33 changed files with 1478 additions and 56 deletions
@@ -0,0 +1,76 @@
import { describe, expect, it } from "vitest";
import type { DocxThemeTokenSet } from "@md-to-pdf/docx-theme-engine";
import {
DOCX_SLOT_WORD_STYLE_BINDINGS,
collectDocxTokenFonts,
createDocxTokenSlotMap,
resolveTokenFonts
} from "../src/index.js";
const tokens: DocxThemeTokenSet = {
schemaVersion: 1,
themeId: "test-theme",
themeFingerprint: "a".repeat(64),
mode: "auto",
basePreset: "general",
slots: [
{
slot: "paragraph",
source: "computed-css",
confidence: "exact",
style: {
fontCandidates: [
"Source Han Serif SC",
"Times New Roman",
"serif"
]
}
}
],
diagnostics: []
};
describe("DOCX 令牌样式映射", () => {
it("以稳定样式 ID 覆盖标准 Markdown 和结构槽位", () => {
expect(
DOCX_SLOT_WORD_STYLE_BINDINGS["heading-1"]
).toEqual([
{
styleId: "Heading1",
type: "paragraph",
basedOn: "Normal"
},
{
styleId: "Heading1Char",
type: "character",
basedOn: "DefaultParagraphFont"
}
]);
expect(
DOCX_SLOT_WORD_STYLE_BINDINGS["official-title"]?.[0]
?.styleId
).toBe("MdOfficialTitle");
expect(
DOCX_SLOT_WORD_STYLE_BINDINGS["tender-representative"]?.[0]
?.styleId
).toBe("MdTenderRepresentative");
});
it("按文字系统选择字体并过滤 CSS 通用族名", () => {
const slot = createDocxTokenSlotMap(tokens).get("paragraph");
expect(
resolveTokenFonts(slot?.style, {
latin: "Arial",
eastAsia: "SimSun"
})
).toEqual({
latin: "Times New Roman",
eastAsia: "Source Han Serif SC",
complexScript: "Times New Roman"
});
expect([...collectDocxTokenFonts(tokens)]).toEqual([
"Source Han Serif SC",
"Times New Roman"
]);
});
});