md-to-docx
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesemd-to-docx
md-to-docx
Use this skill to reliably produce output from Markdown.
.docx使用该工具可将Markdown可靠转换为格式输出。
.docxWorkflow
工作流程
- Decide the execution mode:
- CLI mode for file-to-file conversion.
- Programmatic mode for app code integration.
- Confirm input source (Markdown file or Markdown string).
- Confirm output target (file path or browser download).
.docx - Apply options only when requested (alignment, sizes, direction, font family, replacements, template, sections, page numbering).
- Run conversion and report resulting output path or filename.
- 确定执行模式:
- CLI模式:用于文件到文件的转换。
- 程序化模式:用于集成到应用代码中。
- 确认输入源(Markdown文件或Markdown字符串)。
- 确认输出目标(文件路径或浏览器下载)。
.docx - 仅在用户要求时应用配置选项(对齐方式、尺寸、方向、字体族、文本替换、模板、章节、页码设置)。
- 执行转换并报告最终输出路径或文件名。
CLI Mode
CLI模式
Use these commands:
bash
npx @mohtasham/md-to-docx input.md output.docx
md-to-docx input.md output.docx
md-to-docx input.md output.docx --options options.json
md-to-docx input.md output.docx -o options.json
md-to-docx --helpCLI contract:
- Required positional args:
<input.md> <output.docx> - Optional options file: or
--options <options.json>-o <options.json> - Options JSON can include the same shapes as the API: ,
style,template, andsectionstextReplacements - Help flags: or
-h--help - On success, expect:
DOCX created at: <absolute-path>
使用以下命令:
bash
npx @mohtasham/md-to-docx input.md output.docx
md-to-docx input.md output.docx
md-to-docx input.md output.docx --options options.json
md-to-docx input.md output.docx -o options.json
md-to-docx --helpCLI约定:
- 必填位置参数:
<input.md> <output.docx> - 可选配置文件:或
--options <options.json>-o <options.json> - 配置JSON可包含与API相同的结构:、
style、template和sectionstextReplacements - 帮助标识:或
-h--help - 成功时会输出:
DOCX created at: <absolute-path>
Programmatic Mode
程序化模式
typescript
import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx";
const markdown = "# Title\n\nHello **DOCX**.";
const blob = await convertMarkdownToDocx(markdown, {
documentType: "report",
style: {
fontFamily: "Trebuchet MS",
heading1Alignment: "CENTER",
paragraphAlignment: "JUSTIFIED",
codeBlockAlignment: "LEFT",
direction: "LTR"
}
});
downloadDocx(blob, "output.docx");Use to produce a DOCX .
Use only in browser environments.
convertMarkdownToDocx(markdown, options?)BlobdownloadDocx(blob, filename?)typescript
import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx";
const markdown = "# Title\n\nHello **DOCX**.";
const blob = await convertMarkdownToDocx(markdown, {
documentType: "report",
style: {
fontFamily: "Trebuchet MS",
heading1Alignment: "CENTER",
paragraphAlignment: "JUSTIFIED",
codeBlockAlignment: "LEFT",
direction: "LTR"
}
});
downloadDocx(blob, "output.docx");使用生成DOCX格式的。
仅在浏览器环境中使用。
convertMarkdownToDocx(markdown, options?)BlobdownloadDocx(blob, filename?)Multi-Section Documents
多章节文档
Use for shared defaults and for per-section markdown and overrides:
options.templateoptions.sectionstypescript
const blob = await convertMarkdownToDocx("", {
template: {
footers: {
default: { pageNumberDisplay: "currentAndTotal", alignment: "CENTER" }
}
},
sections: [
{
markdown: "# Cover Page\n\nIntroduction here.",
titlePage: true,
headers: { first: { text: "Confidential", alignment: "RIGHT" } },
pageNumbering: { start: 1, display: "none" }
},
{
markdown: "# Chapter 1\n\nBody content.",
style: { paragraphAlignment: "JUSTIFIED" },
pageNumbering: { start: 1, display: "currentAndTotal" }
}
]
});Each section can override: , , , , (margins/size/orientation), , and (break type).
styleheadersfooterspageNumberingpagetitlePagetypeMerge precedence:
- Global applies first
style - provides shared section defaults
template - Per-section options win last
Use for common footer numbering modes: , , , or .
pageNumbering.displaynonecurrentcurrentAndTotalcurrentAndSectionTotal使用设置全局默认配置,为各章节设置独立的Markdown内容和覆盖配置:
options.templateoptions.sectionstypescript
const blob = await convertMarkdownToDocx("", {
template: {
footers: {
default: { pageNumberDisplay: "currentAndTotal", alignment: "CENTER" }
}
},
sections: [
{
markdown: "# Cover Page\n\nIntroduction here.",
titlePage: true,
headers: { first: { text: "Confidential", alignment: "RIGHT" } },
pageNumbering: { start: 1, display: "none" }
},
{
markdown: "# Chapter 1\n\nBody content.",
style: { paragraphAlignment: "JUSTIFIED" },
pageNumbering: { start: 1, display: "currentAndTotal" }
}
]
});每个章节可覆盖以下配置:、、、、(边距/尺寸/方向)、和(分页类型)。
styleheadersfooterspageNumberingpagetitlePagetype配置优先级:
- 全局优先生效
style - 提供章节共享默认值
template - 章节独立配置优先级最高
使用设置常见页脚页码模式:、、或。
pageNumbering.displaynonecurrentcurrentAndTotalcurrentAndSectionTotalText Replacements
文本替换
Use to rewrite text before conversion:
textReplacementstypescript
const blob = await convertMarkdownToDocx("# Hello oldText", {
textReplacements: [
{ find: /oldText/g, replace: "newText" },
{ find: "Hello", replace: "Hi" }
]
});使用在转换前重写文本内容:
textReplacementstypescript
const blob = await convertMarkdownToDocx("# Hello oldText", {
textReplacements: [
{ find: /oldText/g, replace: "newText" },
{ find: "Hello", replace: "Hi" }
]
});Markdown Features to Expect
支持的Markdown特性
Support includes:
- Headings to
###### - Ordered/unordered lists
- Bold, italic, underline (), strikethrough (
++text++)~~text~~ - Custom font family via style option
fontFamily - Blockquotes
- Tables (with inline formatting: bold, italic, code, links, strikethrough in cells)
- Code blocks and inline code (with configurable )
codeBlockAlignment - Links and images
- Text replacements before rendering via
textReplacements COMMENT: ...- on its own line
[TOC] - on its own line
\pagebreak - Horizontal rules () are skipped during DOCX generation
---
支持特性包括:
- 标题 至
###### - 有序/无序列表
- 粗体、斜体、下划线()、删除线(
++text++)~~text~~ - 通过样式选项设置自定义字体族
fontFamily - 块引用
- 表格(单元格内支持粗体、斜体、代码、链接、删除线等格式)
- 代码块和行内代码(可配置)
codeBlockAlignment - 链接和图片
- 渲染前通过替换文本
textReplacements - 注释
COMMENT: ... - 单独一行的目录标记
[TOC] - 单独一行的分页符
\pagebreak - 水平线()在DOCX生成过程中会被忽略
---
Style Options Quick Reference
样式选项速查
| Option | Values | Default |
|---|---|---|
| | |
| same | |
| same | |
| same | |
| same | |
| any font name string | |
| | |
| | |
| number | library default |
| 选项 | 可选值 | 默认值 |
|---|---|---|
| | |
| 同上 | |
| 同上 | |
| 同上 | |
| 同上 | |
| 任意字体名称字符串 | |
| | |
| | |
| 数字 | 库默认值 |
Troubleshooting
故障排查
- If CLI fails with argument errors, re-check that exactly two positional paths are provided.
- If options parsing fails, validate JSON syntax and ensure the root is an object.
- If output is missing, verify destination directory permissions and path spelling.
- If in Node and you need a file, write the returned bytes to disk instead of using
Blob.downloadDocx - If sections produce unexpected numbering, ensure each section sets to reset counts.
pageNumbering.start - If first-page headers or footers do not appear, set on that section.
titlePage: true
- 若CLI因参数错误失败,请检查是否提供了恰好两个位置路径参数。
- 若配置解析失败,请验证JSON语法并确保根结构为对象。
- 若输出文件缺失,请检查目标目录权限和路径拼写。
- 若在Node环境中需要生成文件,请将返回的字节写入磁盘,而非使用
Blob。downloadDocx - 若章节页码显示异常,请确保每个章节设置来重置计数。
pageNumbering.start - 若首页页眉或页脚未显示,请为该章节设置。
titlePage: true