@v1hz/md2docx 1.5.0 → 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 +23 -13
- package/package.json +1 -1
- package/src/style/generate.ts +7 -4
- package/src/web/app.css +775 -152
- package/src/web/app.js +803 -104
- package/src/web/index.html +96 -9
- package/src/web.ts +375 -11
package/README.md
CHANGED
|
@@ -19,17 +19,17 @@
|
|
|
19
19
|
|
|
20
20
|
将 Markdown 转换为格式精美的 Word 文档,自动处理:
|
|
21
21
|
|
|
22
|
-
| 功能 | 说明
|
|
23
|
-
| ------------------ |
|
|
24
|
-
| **文档标题** | 从 frontmatter、H1 或文件名自动提取
|
|
25
|
-
| **标题层级归一化** | 修正标题层级跳跃,确保从 H1 开始连续递增
|
|
26
|
-
| **标题编号** | 为标题添加多级编号(1、1.1、1.1.1 …),支持剥离已有编号
|
|
27
|
-
| **表格编号** | 自动为表格添加编号(表 1、表 2 …)
|
|
28
|
-
| **图片编号** | 为独立图片添加编号(图 1、图 2 …)
|
|
29
|
-
| **Mermaid 图表** | 将 Mermaid 代码块渲染为 PNG 图片
|
|
30
|
-
| **Word 导出** | 集成 pandoc,传入自定义样式模板,一键生成 .docx
|
|
31
|
-
| **样式定制** | 通过 `
|
|
32
|
-
| **web 配置编辑器** | 浏览器中可视化编辑配置
|
|
22
|
+
| 功能 | 说明 |
|
|
23
|
+
| ------------------ | ----------------------------------------------------------- |
|
|
24
|
+
| **文档标题** | 从 frontmatter、H1 或文件名自动提取 |
|
|
25
|
+
| **标题层级归一化** | 修正标题层级跳跃,确保从 H1 开始连续递增 |
|
|
26
|
+
| **标题编号** | 为标题添加多级编号(1、1.1、1.1.1 …),支持剥离已有编号 |
|
|
27
|
+
| **表格编号** | 自动为表格添加编号(表 1、表 2 …) |
|
|
28
|
+
| **图片编号** | 为独立图片添加编号(图 1、图 2 …) |
|
|
29
|
+
| **Mermaid 图表** | 将 Mermaid 代码块渲染为 PNG 图片 |
|
|
30
|
+
| **Word 导出** | 集成 pandoc,传入自定义样式模板,一键生成 .docx |
|
|
31
|
+
| **样式定制** | 通过 `config/style.json` 定义文档样式,支持从模板 docx 提取 |
|
|
32
|
+
| **web 配置编辑器** | 浏览器中可视化编辑配置 |
|
|
33
33
|
|
|
34
34
|
## 安装
|
|
35
35
|
|
|
@@ -98,9 +98,19 @@ 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`。支持以下样式配置:
|
|
104
114
|
|
|
105
115
|
- **文档默认样式**(`default.document`):全局字体、字号、段落间距
|
|
106
116
|
- **内置样式覆盖**(`default.heading1` … `default.heading6`、`default.title` 等)
|
|
@@ -132,7 +142,7 @@ numberPictures() ← 添加图片编号(图 1、图 2 …)
|
|
|
132
142
|
↓
|
|
133
143
|
序列化 Markdown
|
|
134
144
|
↓
|
|
135
|
-
生成样式模板 docx ← 根据
|
|
145
|
+
生成样式模板 docx ← 根据 config/style.json 生成 pandoc reference-doc
|
|
136
146
|
↓
|
|
137
147
|
pandoc → DOCX ← 以 --reference-doc 传入样式模板
|
|
138
148
|
```
|
package/package.json
CHANGED
package/src/style/generate.ts
CHANGED
|
@@ -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(
|
|
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(
|
|
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(
|
|
38
|
+
await Bun.write(outputPath, modifiedBuf);
|
|
36
39
|
return;
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
42
|
|
|
40
|
-
await Bun.write(
|
|
43
|
+
await Bun.write(outputPath, buf);
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
let _generatePromise: Promise<void> | null = null;
|