@v1hz/md2docx 2.4.0 → 2.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
@@ -26,11 +26,16 @@
26
26
  | 图片尺寸限制 | 按自然尺寸等比缩小超过最大宽度或高度的图片 |
27
27
  | Mermaid 图表 | 使用 beautiful-mermaid 和 resvg-wasm 将 Mermaid 渲染为高 DPI PNG |
28
28
  | Word 样式 | 支持受控语义化配置和完整底层样式,也可从现有 DOCX 提取样式 |
29
+ | 配置预设 | 将处理配置和两层 Word 样式保存为可切换、可继承默认值的预设 |
29
30
  | Markdown 格式化 | 可只运行预处理流水线,输出格式化后的 Markdown |
30
31
  | 去除分隔符 | 默认移除 `---`、`***`、`___` 等分隔符行,减少无用页面间距 |
31
32
  | 集中缓存 | 中间文件统一存储到 `~/.md2docx/`,不会在当前目录创建 `tmp/` |
32
33
  | Node 与可执行版本 | 支持 npm CLI,也支持构建不依赖 Node.js/Bun 的 Windows 可执行文件 |
33
34
 
35
+ ## AI 辅助
36
+
37
+ 本仓库提供 [`SKILL.md`](SKILL.md),是供 AI 代码助手(如 Zed、Cursor、GitHub Copilot)使用的技能描述文件。在 AI 对话中引用此文件,AI 就能正确使用 `md2docx` CLI 完成转换、格式化、样式定制和导出等操作。
38
+
34
39
  ## 安装方式
35
40
 
36
41
  ### npm CLI
@@ -64,6 +69,10 @@ md2docx --file report.md --output output/report.docx
64
69
  # 使用自定义配置、底层样式和语义化样式配置
65
70
  md2docx -f report.md -c config.json --style-raw style-raw.json --style-config style-config.json
66
71
 
72
+ # 保存并使用预设
73
+ md2docx preset save --name academic --config config.json --style-config style-config.json
74
+ md2docx preset use academic
75
+
67
76
  # 只执行 Markdown 预处理
68
77
  md2docx format -f report.md
69
78
 
@@ -90,6 +99,9 @@ md2docx format -f <markdown> [选项]
90
99
  md2docx export config [选项]
91
100
  md2docx export style-raw [选项]
92
101
  md2docx export style-config [选项]
102
+ md2docx preset list
103
+ md2docx preset use <name>
104
+ md2docx preset save --name <name> [配置选项]
93
105
  md2docx clean
94
106
  ```
95
107
 
@@ -99,6 +111,7 @@ md2docx clean
99
111
  | ----------------------- | ---------------------------------------- |
100
112
  | `<markdown>` | 位置参数;仅在没有其他转换选项时允许使用 |
101
113
  | `-f, --file <path>` | Markdown 输入文件 |
114
+ | `--preset <name>` | 本次转换使用指定预设 |
102
115
  | `-c, --config <path>` | 自定义配置 JSON |
103
116
  | `--style-raw <path>` | 完整底层 Word 样式 JSON |
104
117
  | `--style-config <path>` | 受控语义化样式配置 JSON |
@@ -106,7 +119,7 @@ md2docx clean
106
119
  | `-h, --help` | 显示帮助 |
107
120
  | `-v, --version` | 显示版本号 |
108
121
 
109
- 位置参数不能和 `--file`、`--config`、`--style-raw`、`--style-config` 或 `--output` 混用。例如:
122
+ 位置参数不能和 `--file`、`--preset`、`--config`、`--style-raw`、`--style-config` 或 `--output` 混用。例如:
110
123
 
111
124
  ```bash
112
125
  md2docx report.md # 正确
@@ -121,6 +134,7 @@ md2docx -f report.md -o report.docx # 正确
121
134
  | 参数 | 说明 |
122
135
  | --------------------- | ------------------------------------------- |
123
136
  | `-f, --file <path>` | 必填,Markdown 输入文件 |
137
+ | `--preset <name>` | 使用预设中的 `config.json` |
124
138
  | `-c, --config <path>` | 自定义配置 JSON |
125
139
  | `-o, --output <path>` | 输出 Markdown,默认 `<文件名>_formatted.md` |
126
140
 
@@ -134,13 +148,27 @@ md2docx export style-config [-o style-config.json]
134
148
 
135
149
  `export config` 导出内置 Markdown 处理配置。`export style-raw` 不带 `--file` 时导出内置底层 Word 样式;指定 DOCX 时从该文档提取底层样式。`export style-config` 导出默认语义化样式配置。
136
150
 
151
+ ### preset
152
+
153
+ ```bash
154
+ md2docx preset list
155
+ md2docx preset use academic
156
+ md2docx preset use default
157
+ md2docx preset save --name academic \
158
+ --config config.json \
159
+ --style-raw style-raw.json \
160
+ --style-config style-config.json
161
+ ```
162
+
163
+ 用户预设位于 `~/.md2docx/presets/<name>/`,其中三个标准 JSON 文件都可省略;缺失项逐一继承系统内置 `default`。文件存在但内容无效时会报错。`preset save` 至少需要一个配置文件,同名保存会完整替换旧预设,未提供的类型改为继承默认值。`--preset` 只影响本次执行,`preset use` 会持久化默认选择。
164
+
137
165
  ### clean
138
166
 
139
167
  ```bash
140
168
  md2docx clean
141
169
  ```
142
170
 
143
- `clean` 只允许删除当前用户主目录下严格匹配的 `~/.md2docx/`。如果目标是符号链接,只删除链接本身。命令可重复执行;目录不存在时正常退出。
171
+ `clean` 只删除 `~/.md2docx/` 下可重建的预处理文件、物化资源和样式缓存,保留 `presets/` 与 `settings.json`。命令拒绝跟随符号链接,且可重复执行。
144
172
 
145
173
  npm 卸载不会可靠地清理用户数据。卸载前如需清理,请显式运行:
146
174
 
@@ -167,14 +195,21 @@ md2docx export style-config → ./style-config.json
167
195
 
168
196
  ```text
169
197
  ~/.md2docx/
198
+ ├── settings.json
199
+ ├── presets/
200
+ │ └── <预设名称>/
201
+ │ ├── config.json(可选)
202
+ │ ├── style-raw.json(可选)
203
+ │ └── style-config.json(可选)
170
204
  ├── preprocess/
171
205
  │ └── <输入文件名>-<绝对路径哈希>/
172
206
  │ ├── <输入文件名>_formatted.md
173
207
  │ └── mermaid_*.png
174
208
  ├── resources/
175
- │ ├── config.json
176
- │ ├── style-config.json
177
- │ ├── style-raw.json
209
+ │ ├── default/
210
+ ├── config.json
211
+ ├── style-config.json
212
+ │ │ └── style-raw.json
178
213
  │ ├── add-inline-code.lua
179
214
  │ └── limit-image-size.lua
180
215
  └── style/
@@ -189,7 +224,7 @@ md2docx export style-config → ./style-config.json
189
224
 
190
225
  ## 配置
191
226
 
192
- 内置配置来自 `config/config.json`,并由 `config/config.schema.json` 提供 JSON Schema。推荐先导出再编辑:
227
+ 内置配置来自 `config/default/config.json`,并由 `config/config.schema.json` 提供 JSON Schema。推荐先导出再编辑:
193
228
 
194
229
  ```bash
195
230
  md2docx export config
@@ -216,8 +251,8 @@ CLI 不支持覆盖单个配置项,所有配置都通过 JSON 文件管理。
216
251
  | `renderMermaid.theme` | beautiful-mermaid 主题 | `"tokyo-night-light"` |
217
252
  | `renderMermaid.density` | PNG 输出 DPI,最小值 72 | `200` |
218
253
  | `imageSize.enabled` | 等比缩小超过尺寸限制的图片 | `true` |
219
- | `imageSize.maxWidthCm` | DOCX 图片最大宽度(厘米) | `15.5` |
220
- | `imageSize.maxHeightCm` | DOCX 图片最大高度(厘米) | `22` |
254
+ | `imageSize.maxWidthCm` | DOCX 图片最大宽度(厘米) | `12` |
255
+ | `imageSize.maxHeightCm` | DOCX 图片最大高度(厘米) | `12` |
221
256
  | `removeThematicBreaks.enabled` | 移除 `---`、`***`、`___` 等分隔符行 | `true` |
222
257
 
223
258
  图片尺寸限制在 Pandoc 生成 DOCX 前通过 Lua filter 应用。程序读取图片像素尺寸和 DPI,仅缩小超限图片,不会放大小图;宽度和高度使用同一缩放比例。Markdown 中已经显式设置 `width` 或 `height` 的图片视为用户覆盖,不应用全局限制。单张图片无法读取尺寸时会输出警告并继续转换。
@@ -226,7 +261,7 @@ CLI 不支持覆盖单个配置项,所有配置都通过 JSON 文件管理。
226
261
 
227
262
  普通用户推荐使用受控的语义化样式配置,只修改程序明确开放的高频选项。其余颜色、尺寸、对齐方式、间距和 Word 样式继承关系继续由内置预设管理。
228
263
 
229
- 仓库中的 `config/style-config.json` 是可直接复制和修改的默认配置,`config/style-config.schema.json` 用于编辑器提示和校验。完整底层 Word 样式位于 `config/style-raw.json`。
264
+ 仓库中的 `config/default/style-config.json` 是可直接复制和修改的默认配置,`config/style-config.schema.json` 用于编辑器提示和校验。完整底层 Word 样式位于 `config/default/style-raw.json`。
230
265
 
231
266
  ```json
232
267
  {
@@ -234,11 +269,32 @@ CLI 不支持覆盖单个配置项,所有配置都通过 JSON 文件管理。
234
269
  "schemaVersion": 1,
235
270
  "options": {
236
271
  "body": {
237
- "firstLineIndent": false
272
+ "firstLineIndent": false,
273
+ "lineSpacing": "onePointFive"
238
274
  },
239
275
  "headings": {
240
276
  "1": {
241
- "startOnNewPage": false
277
+ "startOnNewPage": false,
278
+ "alignment": "left",
279
+ "bold": true
280
+ },
281
+ "2": {
282
+ "bold": true
283
+ },
284
+ "3": {
285
+ "bold": true
286
+ },
287
+ "4": {
288
+ "bold": true,
289
+ "italic": false
290
+ },
291
+ "5": {
292
+ "bold": true,
293
+ "italic": false
294
+ },
295
+ "6": {
296
+ "bold": true,
297
+ "italic": false
242
298
  }
243
299
  },
244
300
  "inlineCode": {
@@ -257,15 +313,15 @@ CLI 不支持覆盖单个配置项,所有配置都通过 JSON 文件管理。
257
313
  md2docx -f report.md --style-config style-config.json
258
314
  ```
259
315
 
260
- 字段缺失表示继承底层样式;`true` 表示显式启用完整效果;`false` 表示显式关闭。当前只开放正文首行缩进、一级标题另起一页、行内代码背景和代码块外框。完整设计见 [`docs/style-config-design.md`](docs/style-config-design.md)。
316
+ 字段缺失表示继承底层样式;`true` 表示显式启用完整效果;`false` 表示显式关闭。正文行距为浮点数倍数,例如 `1.5` 表示 1.5 倍行距;一级标题对齐只接受 `left`、`center`。当前还开放一至六级标题粗体,以及四至六级标题斜体。完整设计见 [`docs/style-config-design.md`](docs/style-config-design.md)。
261
317
 
262
318
  转换时按以下规则选择输入:
263
319
 
264
320
  | 参数 | 行为 |
265
321
  | ------------------- | ----------------------------------- |
266
- | 都不指定 | 默认 raw + 默认 config |
322
+ | 都不指定 | 当前预设 raw + 当前预设 config |
267
323
  | 仅 `--style-raw` | 直接使用用户 raw,不应用默认 config |
268
- | 仅 `--style-config` | 用户 config 应用到默认 raw |
324
+ | 仅 `--style-config` | 用户 config 应用到当前预设 raw |
269
325
  | 两者都指定 | 用户 config 应用到用户 raw |
270
326
 
271
327
  最终有效的底层样式用于生成 reference DOCX,并通过 Pandoc 的 `--reference-doc` 应用。`--style-raw` 和 `--style-config` 类型固定,不能互换。
@@ -131,13 +131,13 @@
131
131
  "description": "图片最大宽度(厘米)",
132
132
  "type": "number",
133
133
  "exclusiveMinimum": 0,
134
- "default": 15.5
134
+ "default": 12
135
135
  },
136
136
  "maxHeightCm": {
137
137
  "description": "图片最大高度(厘米)",
138
138
  "type": "number",
139
139
  "exclusiveMinimum": 0,
140
- "default": 22
140
+ "default": 12
141
141
  }
142
142
  },
143
143
  "required": ["enabled", "maxWidthCm", "maxHeightCm"]
@@ -0,0 +1,41 @@
1
+ {
2
+ "$schema": "https://raw.githubusercontent.com/WXY-V1hZ/md2docx/main/config/style-config.schema.json",
3
+ "schemaVersion": 1,
4
+ "options": {
5
+ "body": {
6
+ "firstLineIndent": true,
7
+ "lineSpacing": 1.5
8
+ },
9
+ "headings": {
10
+ "1": {
11
+ "startOnNewPage": false,
12
+ "alignment": "left",
13
+ "bold": true
14
+ },
15
+ "2": {
16
+ "bold": true
17
+ },
18
+ "3": {
19
+ "bold": true
20
+ },
21
+ "4": {
22
+ "bold": true,
23
+ "italic": false
24
+ },
25
+ "5": {
26
+ "bold": true,
27
+ "italic": false
28
+ },
29
+ "6": {
30
+ "bold": true,
31
+ "italic": false
32
+ }
33
+ },
34
+ "inlineCode": {
35
+ "background": true
36
+ },
37
+ "codeBlock": {
38
+ "border": true
39
+ }
40
+ }
41
+ }
@@ -23,6 +23,11 @@
23
23
  "firstLineIndent": {
24
24
  "type": "boolean",
25
25
  "description": "正文是否使用预设的首行缩进"
26
+ },
27
+ "lineSpacing": {
28
+ "type": "number",
29
+ "exclusiveMinimum": 0,
30
+ "description": "正文行距倍数,如 1.5 表示 1.5 倍行距"
26
31
  }
27
32
  }
28
33
  },
@@ -37,8 +42,32 @@
37
42
  "startOnNewPage": {
38
43
  "type": "boolean",
39
44
  "description": "一级标题是否另起一页"
45
+ },
46
+ "alignment": {
47
+ "type": "string",
48
+ "enum": ["left", "center"],
49
+ "description": "一级标题左对齐或居中"
50
+ },
51
+ "bold": {
52
+ "type": "boolean",
53
+ "description": "一级标题是否加粗"
40
54
  }
41
55
  }
56
+ },
57
+ "2": {
58
+ "$ref": "#/$defs/headingBold"
59
+ },
60
+ "3": {
61
+ "$ref": "#/$defs/headingBold"
62
+ },
63
+ "4": {
64
+ "$ref": "#/$defs/headingBoldItalic"
65
+ },
66
+ "5": {
67
+ "$ref": "#/$defs/headingBoldItalic"
68
+ },
69
+ "6": {
70
+ "$ref": "#/$defs/headingBoldItalic"
42
71
  }
43
72
  }
44
73
  },
@@ -64,5 +93,31 @@
64
93
  }
65
94
  }
66
95
  }
96
+ },
97
+ "$defs": {
98
+ "headingBold": {
99
+ "type": "object",
100
+ "additionalProperties": false,
101
+ "properties": {
102
+ "bold": {
103
+ "type": "boolean",
104
+ "description": "标题是否加粗"
105
+ }
106
+ }
107
+ },
108
+ "headingBoldItalic": {
109
+ "type": "object",
110
+ "additionalProperties": false,
111
+ "properties": {
112
+ "bold": {
113
+ "type": "boolean",
114
+ "description": "标题是否加粗"
115
+ },
116
+ "italic": {
117
+ "type": "boolean",
118
+ "description": "标题是否斜体"
119
+ }
120
+ }
121
+ }
67
122
  }
68
123
  }