md-to-docx
Use this skill to reliably produce .docx output from Markdown.
Workflow
- 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 (
.docx file path or browser download).
- 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 Mode
Use these commands:
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 --help
CLI contract:
- Required positional args:
<input.md> <output.docx>
- Optional options file:
--options <options.json> or -o <options.json>
- Options JSON can include the same shapes as the API:
style, template, sections, and textReplacements
- Help flags:
-h or --help
- On success, expect:
DOCX created at: <absolute-path>
Programmatic Mode
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 convertMarkdownToDocx(markdown, options?) to produce a DOCX Blob.
Use downloadDocx(blob, filename?) only in browser environments.
Multi-Section Documents
Use options.template for shared defaults and options.sections for per-section markdown and overrides:
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: style, headers, footers, pageNumbering, page (margins/size/orientation), titlePage, and type (break type).
Merge precedence:
- Global
style applies first
template provides shared section defaults
- Per-section options win last
Use pageNumbering.display for common footer numbering modes: none, current, currentAndTotal, or currentAndSectionTotal.
Text Replacements
Use textReplacements to rewrite text before conversion:
const blob = await convertMarkdownToDocx("# Hello oldText", {
textReplacements: [
{ find: /oldText/g, replace: "newText" },
{ find: "Hello", replace: "Hi" }
]
});
Markdown Features to Expect
Support includes:
- Headings
# to #####
- Ordered/unordered lists
- Bold, italic, underline (
++text++), strikethrough (~~text~~)
- Custom font family via
fontFamily style option
- 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: ...
[TOC] on its own line
\pagebreak on its own line
- Horizontal rules (
---) are skipped during DOCX generation
Style Options Quick Reference
| Option |
Values |
Default |
paragraphAlignment |
LEFT, CENTER, RIGHT, JUSTIFIED |
LEFT |
headingAlignment |
same |
LEFT |
heading1Alignment–heading5Alignment |
same |
LEFT |
blockquoteAlignment |
same |
LEFT |
codeBlockAlignment |
same |
LEFT |
fontFamily |
any font name string |
Calibri |
direction |
LTR, RTL |
LTR |
tableLayout |
autofit, fixed |
autofit |
tocFontSize |
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
Blob bytes to disk instead of using downloadDocx.
- If sections produce unexpected numbering, ensure each section sets
pageNumbering.start to reset counts.
- If first-page headers or footers do not appear, set
titlePage: true on that section.
Source: MohtashamMurshid/md-to-docx — distributed by TomeVault.
1---2name: md-to-docx3description: Convert Markdown files and strings into DOCX documents using @mohtasham/md-to-docx. Use when a user needs Markdown to Word conversion, CLI-based file conversion, options-driven styling/alignment/font family, TOC/page break handling, underline/strikethrough formatting, multi-section documents with per-section headers/footers, or programmatic conversion in Node/browser code. Use when this capability is needed.4---56# md-to-docx78Use this skill to reliably produce `.docx` output from Markdown.910## Workflow11121. Decide the execution mode:13 - CLI mode for file-to-file conversion.14 - Programmatic mode for app code integration.152. Confirm input source (Markdown file or Markdown string).163. Confirm output target (`.docx` file path or browser download).174. Apply options only when requested (alignment, sizes, direction, font family, replacements, template, sections, page numbering).185. Run conversion and report resulting output path or filename.1920## CLI Mode2122Use these commands:2324```bash25npx @mohtasham/md-to-docx input.md output.docx26md-to-docx input.md output.docx27md-to-docx input.md output.docx --options options.json28md-to-docx input.md output.docx -o options.json29md-to-docx --help30```3132CLI contract:33- Required positional args: `<input.md> <output.docx>`34- Optional options file: `--options <options.json>` or `-o <options.json>`35- Options JSON can include the same shapes as the API: `style`, `template`, `sections`, and `textReplacements`36- Help flags: `-h` or `--help`37- On success, expect: `DOCX created at: <absolute-path>`3839## Programmatic Mode4041```typescript42import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx";4344const markdown = "# Title\n\nHello **DOCX**.";45const blob = await convertMarkdownToDocx(markdown, {46 documentType: "report",47 style: {48 fontFamily: "Trebuchet MS",49 heading1Alignment: "CENTER",50 paragraphAlignment: "JUSTIFIED",51 codeBlockAlignment: "LEFT",52 direction: "LTR"53 }54});5556downloadDocx(blob, "output.docx");57```5859Use `convertMarkdownToDocx(markdown, options?)` to produce a DOCX `Blob`.60Use `downloadDocx(blob, filename?)` only in browser environments.6162## Multi-Section Documents6364Use `options.template` for shared defaults and `options.sections` for per-section markdown and overrides:6566```typescript67const blob = await convertMarkdownToDocx("", {68 template: {69 footers: {70 default: { pageNumberDisplay: "currentAndTotal", alignment: "CENTER" }71 }72 },73 sections: [74 {75 markdown: "# Cover Page\n\nIntroduction here.",76 titlePage: true,77 headers: { first: { text: "Confidential", alignment: "RIGHT" } },78 pageNumbering: { start: 1, display: "none" }79 },80 {81 markdown: "# Chapter 1\n\nBody content.",82 style: { paragraphAlignment: "JUSTIFIED" },83 pageNumbering: { start: 1, display: "currentAndTotal" }84 }85 ]86});87```8889Each section can override: `style`, `headers`, `footers`, `pageNumbering`, `page` (margins/size/orientation), `titlePage`, and `type` (break type).9091Merge precedence:92- Global `style` applies first93- `template` provides shared section defaults94- Per-section options win last9596Use `pageNumbering.display` for common footer numbering modes: `none`, `current`, `currentAndTotal`, or `currentAndSectionTotal`.9798## Text Replacements99100Use `textReplacements` to rewrite text before conversion:101102```typescript103const blob = await convertMarkdownToDocx("# Hello oldText", {104 textReplacements: [105 { find: /oldText/g, replace: "newText" },106 { find: "Hello", replace: "Hi" }107 ]108});109```110111## Markdown Features to Expect112113Support includes:114- Headings `#` to `#####`115- Ordered/unordered lists116- Bold, italic, underline (`++text++`), strikethrough (`~~text~~`)117- Custom font family via `fontFamily` style option118- Blockquotes119- Tables (with inline formatting: bold, italic, code, links, strikethrough in cells)120- Code blocks and inline code (with configurable `codeBlockAlignment`)121- Links and images122- Text replacements before rendering via `textReplacements`123- `COMMENT: ...`124- `[TOC]` on its own line125- `\pagebreak` on its own line126- Horizontal rules (`---`) are skipped during DOCX generation127128## Style Options Quick Reference129130| Option | Values | Default |131|---|---|---|132| `paragraphAlignment` | `LEFT`, `CENTER`, `RIGHT`, `JUSTIFIED` | `LEFT` |133| `headingAlignment` | same | `LEFT` |134| `heading1Alignment`–`heading5Alignment` | same | `LEFT` |135| `blockquoteAlignment` | same | `LEFT` |136| `codeBlockAlignment` | same | `LEFT` |137| `fontFamily` | any font name string | `Calibri` |138| `direction` | `LTR`, `RTL` | `LTR` |139| `tableLayout` | `autofit`, `fixed` | `autofit` |140| `tocFontSize` | number | library default |141142## Troubleshooting143144- If CLI fails with argument errors, re-check that exactly two positional paths are provided.145- If options parsing fails, validate JSON syntax and ensure the root is an object.146- If output is missing, verify destination directory permissions and path spelling.147- If in Node and you need a file, write the returned `Blob` bytes to disk instead of using `downloadDocx`.148- If sections produce unexpected numbering, ensure each section sets `pageNumbering.start` to reset counts.149- If first-page headers or footers do not appear, set `titlePage: true` on that section.150151---152> Source: [MohtashamMurshid/md-to-docx](https://github.com/MohtashamMurshid/md-to-docx) — distributed by [TomeVault](https://tomevault.io).153<!-- tomevault:4.0:skill_md:2026-07-08 -->