Files
MorphDoc/packages/docx-engine/tests/token-style-map.test.ts
T
SkyJourney 2c5c1bd317 release: 发布 v0.6.1 DOCX 视觉一致性修复
新增通用 CSS 到 OOXML 翻译修复,统一字体、字距、精确行距、段落、列表、表格、引用、代码块与行内代码连续性,不引入按主题 ID 分支。

新增 MdTP Mono 并统一 Serif、Sans、Mono 三字体包的 Chromium 与 DOCX 使用链;字体声明、嵌入部件和 Word/WPS 实际采用均进入硬门禁。

重建封面整页及正文语义块视觉差分,14 套主题、纵横两个方向、五组页边距共 140 个真实场景全部通过,阻断失败和诊断失败均为零。

源码服务、Docker Web API 与实际安装 Desktop 的 red-briefing 导出均包含 5 个字体部件;Word/WPS 原生渲染和逐页复核通过。修复 Docker 构建上下文与运行层复用软链接,并完善 v0.6.1 版本、发行说明和发布归集。

验证:npm test(116 个文件、616 项测试)、npm run typecheck、npm run build、git diff --check 全部通过。Desktop 安装器与 ZIP、Docker v0.6.1 镜像已生成;Windows 产物仍为未签名内部发行。
2026-08-04 10:30:44 +08:00

104 lines
2.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { DocxThemeTokenSet } from "@md-to-pdf/docx-theme-engine";
import {
DOCX_SLOT_WORD_STYLE_BINDINGS,
collectDocxTokenFonts,
createDocxTokenSlotMap,
resolvePandocSyntaxStyleSlot,
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"
]);
expect(
resolveTokenFonts(
{ fontCandidates: ["MdTP Mono", "monospace"] },
{ latin: "Arial", eastAsia: "MdTP Sans SC" },
{ preserveFallbackEastAsia: true }
)
).toEqual({
latin: "MdTP Mono",
eastAsia: "MdTP Sans SC",
complexScript: "MdTP Mono"
});
});
it("将 Pandoc 语法样式映射到引擎级代码语义槽位", () => {
expect(resolvePandocSyntaxStyleSlot("DataTypeTok", '"key"'))
.toBe("code-token-attribute");
expect(resolvePandocSyntaxStyleSlot("StringTok", '"value"'))
.toBe("code-token-string");
expect(resolvePandocSyntaxStyleSlot("FunctionTok", ":"))
.toBe("code-token-punctuation");
expect(resolvePandocSyntaxStyleSlot("FunctionTok", "render"))
.toBe("code-token-title");
expect(resolvePandocSyntaxStyleSlot("NormalTok", " "))
.toBe("code-block-text");
expect(resolvePandocSyntaxStyleSlot("UnknownTok", "x"))
.toBeUndefined();
});
});