@v1hz/md2docx 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -98,6 +98,16 @@ md2docx docs/example.md --figureCaption.enabled false
98
98
 
99
99
  `config/style.json` 定义了 docx 输出的全部样式,包括字体、字号、颜色、缩进、间距等。
100
100
 
101
+ ### 从模板 docx 提取样式
102
+
103
+ 如果你有一个排版精美的 docx 模板,可以提取其样式:
104
+
105
+ ```bash
106
+ bun run src/style/extract.ts
107
+ ```
108
+
109
+ 提取后修改 `config/style.json`,转换时会自动使用这些样式生成输出文档。
110
+
101
111
  ### 手动维护样式
102
112
 
103
113
  可直接编辑 `config/style.json`。支持以下样式配置:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@v1hz/md2docx",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "基于 pandoc 的 Markdown 转 Word 工具,自动格式化 Markdown,支持自定义样式,AI 友好,用户友好。",
5
5
  "keywords": [
6
6
  "converter",
@@ -8,7 +8,10 @@ import { STYLE_TEMPLATE_DOCX } from "../paths";
8
8
  *
9
9
  * @param styleJsonPath - 样式 JSON 文件路径(如 config/style.json)
10
10
  */
11
- export async function generateTemplateDocx(styleJsonPath: string): Promise<void> {
11
+ export async function generateTemplateDocx(
12
+ styleJsonPath: string,
13
+ outputPath: string = STYLE_TEMPLATE_DOCX,
14
+ ): Promise<void> {
12
15
  const raw = JSON.parse(readFileSync(styleJsonPath, "utf-8")) as Record<string, unknown>;
13
16
  const tableStylesXml = raw.tableStylesXml as string | undefined;
14
17
  // 移除 tableStylesXml,docx 包不识别它
@@ -19,7 +22,7 @@ export async function generateTemplateDocx(styleJsonPath: string): Promise<void>
19
22
  sections: [],
20
23
  });
21
24
  const buf = await Packer.toBuffer(doc);
22
- mkdirSync(dirname(STYLE_TEMPLATE_DOCX), { recursive: true });
25
+ mkdirSync(dirname(outputPath), { recursive: true });
23
26
 
24
27
  // 如果有表格样式 XML,注入到生成的 docx 中
25
28
  if (tableStylesXml) {
@@ -32,12 +35,12 @@ export async function generateTemplateDocx(styleJsonPath: string): Promise<void>
32
35
  const injectedXml = originalXml.replace("</w:styles>", tableStylesXml + "</w:styles>");
33
36
  zip.file("word/styles.xml", injectedXml);
34
37
  const modifiedBuf = zip.generate({ type: "nodebuffer" }) as Buffer;
35
- await Bun.write(STYLE_TEMPLATE_DOCX, modifiedBuf);
38
+ await Bun.write(outputPath, modifiedBuf);
36
39
  return;
37
40
  }
38
41
  }
39
42
 
40
- await Bun.write(STYLE_TEMPLATE_DOCX, buf);
43
+ await Bun.write(outputPath, buf);
41
44
  }
42
45
 
43
46
  let _generatePromise: Promise<void> | null = null;