diff --git a/.omo/notepads/docx-export-quality/learnings.md b/.omo/notepads/docx-export-quality/learnings.md index 9a55873..470c6ea 100644 --- a/.omo/notepads/docx-export-quality/learnings.md +++ b/.omo/notepads/docx-export-quality/learnings.md @@ -89,3 +89,13 @@ - 仓库根 `.prettierrc`(`singleQuote:false`、printWidth 80)**不适用于 src 手写风格(单引号、长行)**;`scripts/verify.sh` 的 prettier 检查只覆盖少数根文件。 - **对 `packages/mdpkg/src/*.ts`、`test/*.ts` 跑仓库根 `prettier --write` 会引发全文件重排(引号+换行),diff 噪声 ~3 倍**。恢复只能近似(`--single-quote --print-width 140` 仍有残余换行差异)。手写风格即规范,不要自动化格式化这些文件。 + +## Typography fix (2026-09-15, fix/docx-typography, #21) + +### 标题黑体 + 图片居中 + +- **样式表注入**:Heading1–6 的 `` 插入 ``(仅覆盖 eastAsia,不动 ascii/hAnsi)。docDefaults 保持 `eastAsia="宋体"` 不变。 +- **图片居中判定**:在 `blockToXml` paragraph case 中,当 `!extra?.style && !extra?.numPr && children.length===1 && children[0].type==='image'` 时注入 ``。排除面:表格单元格(`style:'Table'`)、列表项(`numPr`)、引用块(`style:'Quote'`)、行内图片(子节点>1)。 +- **关键陷阱**:`extra?.style` 在表格单元格序列化时被设为 `'Table'`(`tableToXml` 调用 `blockToXml(b, ctx, { style: 'Table' })`),这是判断"是否在表格内"的可靠信号。 +- **测试覆盖**:5 新用例(独立图片居中、行内图片不居中、表格图片不居中、标题黑体×6、正文宋体回归)。323 全绿。 +- **openspec**:ADDED requirement「docx 排版规范」,不冲突于 docx-export-quality 的 MODIFIED requirements(该 change 尚未 archive)。 diff --git a/packages/mdpkg/src/docx.ts b/packages/mdpkg/src/docx.ts index ea3de0f..4301a41 100644 --- a/packages/mdpkg/src/docx.ts +++ b/packages/mdpkg/src/docx.ts @@ -601,11 +601,17 @@ function blockToXml( const pPr = `${pStyle ? `` : ''}${extra?.numPr ?? ''}${calloutPPr(extra)}`; const prefix = extra?.prefix ? plainRun(extra.prefix, {}) : ''; const headerFmt = extra?.headerBold ? headerBoldFmt({}) : {}; - const hasBlockMath = (node.children as unknown[])?.some( + // 独立成段图片居中:段落唯一子节点为图片,且非表格单元格/列表项/引用块 + const children = node.children as unknown[]; + const isStandaloneImage = + !extra?.style && !extra?.numPr && children.length === 1 && (children[0] as { type?: string }).type === 'image'; + const centerJc = isStandaloneImage ? '' : ''; + const allPPr = `${pPr}${centerJc}`; + const hasBlockMath = children?.some( (c) => (c as { type?: string }).type === 'math' && (c as { display?: boolean }).display ); if (!hasBlockMath) { - return `${pPr ? `${pPr}` : ''}${prefix}${inlineChildren(node.children as never[], ctx, headerFmt)}`; + return `${allPPr ? `${allPPr}` : ''}${prefix}${inlineChildren(children as never[], ctx, headerFmt)}`; } return splitParagraphAtBlockMath(node as { children: unknown[] }, ctx, { ...extra, @@ -924,12 +930,12 @@ const STYLES_XML = ` - - - - - - + + + + + + diff --git a/packages/mdpkg/test/docx.test.ts b/packages/mdpkg/test/docx.test.ts index 57710aa..4d40c4b 100644 --- a/packages/mdpkg/test/docx.test.ts +++ b/packages/mdpkg/test/docx.test.ts @@ -1155,3 +1155,50 @@ test('回归#16-l 12列含长内容:总和 9026 且每列 ≥400', async () => assert.ok(w >= 400, `每列应 ≥400,实际 ${w}`); } }); + +// ============ Typography fix (issue #21) ============ + +test('排版 独立图片段落居中:w:jc w:val="center"', async () => { + const body = '![示例图片](assets/pic.png)\n'; + const out = await unpackDocx(toDocx(pkg(body, { 'assets/pic.png': PNG }))); + const doc = dec.decode(out.get('word/document.xml')!); + assert.ok(doc.includes(''), '独立图片段落应含居中对齐'); +}); + +test('排版 行内图片不居中:文字+图片段落无 w:jc', async () => { + const body = '文字 ![图](assets/pic.png) 文字\n'; + const out = await unpackDocx(toDocx(pkg(body, { 'assets/pic.png': PNG }))); + const doc = dec.decode(out.get('word/document.xml')!); + assert.ok(!doc.includes(''), '行内图片段落不应含居中对齐'); +}); + +test('排版 表格单元格图片不居中:单元格内图片无 w:jc', async () => { + const body = '| 列A |\n| --- |\n| ![图](assets/pic.png) |\n'; + const out = await unpackDocx(toDocx(pkg(body, { 'assets/pic.png': PNG }))); + const doc = dec.decode(out.get('word/document.xml')!); + assert.ok(!doc.includes(''), '表格单元格图片不应含居中对齐'); +}); + +test('排版 标题黑体:styles.xml Heading1-6 含 w:eastAsia="黑体"', async () => { + const body = '# 一\n## 二\n### 三\n#### 四\n##### 五\n###### 六\n'; + const out = await unpackDocx(toDocx(pkg(body))); + const styles = dec.decode(out.get('word/styles.xml')!); + for (let i = 1; i <= 6; i++) { + assert.ok( + styles.includes(`w:styleId="Heading${i}"`) && styles.includes('w:eastAsia="黑体"'), + `Heading${i} 应含 w:eastAsia="黑体"` + ); + } + // 精确计数:黑体出现 6 次(每个 heading 一次) + const count = (styles.match(/eastAsia="黑体"/g) || []).length; + assert.equal(count, 6, `黑体应出现 6 次,实际 ${count}`); +}); + +test('排版 正文宋体回归:docDefaults 仍为 eastAsia="宋体"', async () => { + const out = await unpackDocx(toDocx(pkg('# 标题\n\n正文\n'))); + const styles = dec.decode(out.get('word/styles.xml')!); + assert.ok( + styles.includes(''), + 'docDefaults 应保持宋体不变' + ); +}); diff --git a/packages/mdpkg/web/mdpkg-web.iife.js b/packages/mdpkg/web/mdpkg-web.iife.js index a53b5eb..44352d0 100644 --- a/packages/mdpkg/web/mdpkg-web.iife.js +++ b/packages/mdpkg/web/mdpkg-web.iife.js @@ -23934,11 +23934,15 @@ ${bodyHtml} const pPr = `${pStyle ? `` : ""}${extra?.numPr ?? ""}${calloutPPr(extra)}`; const prefix = extra?.prefix ? plainRun(extra.prefix, {}) : ""; const headerFmt = extra?.headerBold ? headerBoldFmt({}) : {}; - const hasBlockMath = node2.children?.some( + const children2 = node2.children; + const isStandaloneImage = !extra?.style && !extra?.numPr && children2.length === 1 && children2[0].type === "image"; + const centerJc = isStandaloneImage ? '' : ""; + const allPPr = `${pPr}${centerJc}`; + const hasBlockMath = children2?.some( (c) => c.type === "math" && c.display ); if (!hasBlockMath) { - return `${pPr ? `${pPr}` : ""}${prefix}${inlineChildren(node2.children, ctx, headerFmt)}`; + return `${allPPr ? `${allPPr}` : ""}${prefix}${inlineChildren(children2, ctx, headerFmt)}`; } return splitParagraphAtBlockMath(node2, ctx, { ...extra, @@ -24192,12 +24196,12 @@ ${items} - - - - - - + + + + + + diff --git a/packages/mdpkg/web/mdpkg-web.js b/packages/mdpkg/web/mdpkg-web.js index fccd50e..4242021 100644 --- a/packages/mdpkg/web/mdpkg-web.js +++ b/packages/mdpkg/web/mdpkg-web.js @@ -23914,11 +23914,15 @@ function blockToXml(node2, ctx, extra) { const pPr = `${pStyle ? `` : ""}${extra?.numPr ?? ""}${calloutPPr(extra)}`; const prefix = extra?.prefix ? plainRun(extra.prefix, {}) : ""; const headerFmt = extra?.headerBold ? headerBoldFmt({}) : {}; - const hasBlockMath = node2.children?.some( + const children2 = node2.children; + const isStandaloneImage = !extra?.style && !extra?.numPr && children2.length === 1 && children2[0].type === "image"; + const centerJc = isStandaloneImage ? '' : ""; + const allPPr = `${pPr}${centerJc}`; + const hasBlockMath = children2?.some( (c) => c.type === "math" && c.display ); if (!hasBlockMath) { - return `${pPr ? `${pPr}` : ""}${prefix}${inlineChildren(node2.children, ctx, headerFmt)}`; + return `${allPPr ? `${allPPr}` : ""}${prefix}${inlineChildren(children2, ctx, headerFmt)}`; } return splitParagraphAtBlockMath(node2, ctx, { ...extra, @@ -24172,12 +24176,12 @@ var STYLES_XML = ` - - - - - - + + + + + +