Package Exports
- @mohtasham/md-to-docx
- @mohtasham/md-to-docx/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mohtasham/md-to-docx) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Markdown to DOCX Converter
A powerful TypeScript module that converts Markdown text to Microsoft Word (.docx) documents with support for various Markdown features. Perfect for both Node.js and browser environments.
Github Repo (Open Source)
[https://github.com/MohtashamMurshid/md-to-docx]
Features
- ๐ฏ Convert Markdown to DOCX format
- ๐ Table of Contents generation with clickable links (
[TOC]) - ๐ Page break support (
\pagebreak) - #๏ธโฃ Automatic page numbering (centered in footer)
- ๐ Support for all heading levels (H1-H5)
- ๐ Bullet points and numbered lists with rich formatting
- ๐ Tables with headers and data
- ๐ค Bold and italic text formatting (including in lists)
- ๐ฌ Blockquotes
- ๐ก Comments
- ๐จ Customizable styling
- ๐ Report and document modes
- ๐ Browser and Node.js support
- ๐ผ๏ธ Support for embedded images
- ๐ป Code blocks (inline and multi-line)
- ๐ Support for links
Strikethroughtext support- ๐ Custom font sizes for all elements
- โ๏ธ Text alignment control for all elements
- ๐งช Comprehensive test coverage
Installation
npm install @mohtasham/md-to-docxUsage
Basic Usage
import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx";
const markdown = `
# Title
## Subtitle
This is a paragraph with **bold** and *italic* text.
- Bullet point with **bold text** inside
- Another point with *italic* and \`code\`
**Bold text on next line**
1. Numbered item with **bold** formatting
2. Another item with mixed **bold** and *italic*
> This is a blockquote
| Header 1 | Header 2 |
|----------|----------|
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
\`\`\`typescript
function hello(name: string): string {
return \`Hello, \${name}!\`;
}
\`\`\`

COMMENT: This is a comment
`;
// Convert to DOCX
const blob = await convertMarkdownToDocx(markdown);
// Download in browser
downloadDocx(blob, "output.docx");With Custom Options
const options = {
documentType: "report", // or 'document'
style: {
titleSize: 32,
headingSpacing: 240,
paragraphSpacing: 240,
lineSpacing: 1.15,
heading1Size: 32,
heading2Size: 28,
heading3Size: 24,
heading4Size: 20,
heading5Size: 18,
paragraphSize: 24,
listItemSize: 24,
codeBlockSize: 20,
blockquoteSize: 24,
paragraphAlignment: "JUSTIFIED",
blockquoteAlignment: "CENTER",
},
};
const blob = await convertMarkdownToDocx(markdown, options);Text Alignment Example
const markdownWithAlignment = `
# Left-Aligned Heading 1
## Left-Aligned Heading 2
This is a justified paragraph that demonstrates how text can be spread evenly across the width of the page. This creates a clean, professional look with straight edges on both the left and right margins.
> This is a centered blockquote that stands out from the regular text.
This is a left-aligned paragraph (default alignment) that shows the standard text positioning.
`;
const alignmentOptions = {
documentType: "document",
style: {
paragraphAlignment: "JUSTIFIED",
blockquoteAlignment: "CENTER",
// All headings default to LEFT alignment
},
};
const blob = await convertMarkdownToDocx(
markdownWithAlignment,
alignmentOptions
);Custom Heading Alignments
You can customize the alignment for each heading level individually:
const customHeadingOptions = {
documentType: "document",
style: {
// Individual heading alignments
heading1Alignment: "CENTER", // H1 will be centered
heading2Alignment: "RIGHT", // H2 will be right-aligned
heading3Alignment: "JUSTIFIED", // H3 will be justified
heading4Alignment: "LEFT", // H4 will be left-aligned
heading5Alignment: "CENTER", // H5 will be centered
// Other style options
paragraphAlignment: "LEFT", // Paragraphs will be left-aligned
blockquoteAlignment: "LEFT", // Blockquotes will be left-aligned
},
};
const markdown = `
# This will be centered
## This will be right-aligned
### This will be justified
#### This will be left-aligned
##### This will be centered
`;
const blob = await convertMarkdownToDocx(markdown, customHeadingOptions);In React
import { useState } from "react";
import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx";
function MarkdownConverter() {
const [markdown, setMarkdown] = useState("");
const handleConvert = async () => {
try {
const blob = await convertMarkdownToDocx(markdown);
downloadDocx(blob, "converted.docx");
} catch (error) {
console.error("Conversion failed:", error);
}
};
return (
<div>
<textarea
value={markdown}
onChange={(e) => setMarkdown(e.target.value)}
/>
<button onClick={handleConvert}>Convert to DOCX</button>
</div>
);
}API
convertMarkdownToDocx(markdown: string, options?: Options): Promise<Blob>
Converts Markdown text to a DOCX document.
Parameters
markdown(string): The Markdown text to convertoptions(object, optional): Configuration optionsdocumentType(string): Either 'document' or 'report'style(object): Styling options- Text Sizes:
titleSize(number): Font size for titlesheading1Sizethroughheading5Size(number): Font sizes for H1-H5paragraphSize(number): Font size for paragraphslistItemSize(number): Font size for list itemscodeBlockSize(number): Font size for code blocksblockquoteSize(number): Font size for blockquotes
- Spacing:
headingSpacing(number): Spacing before/after headingsparagraphSpacing(number): Spacing before/after paragraphslineSpacing(number): Line spacing multiplier
- Alignment:
paragraphAlignment(string): "LEFT" | "RIGHT" | "CENTER" | "JUSTIFIED"headingAlignment(string): "LEFT" | "RIGHT" | "CENTER" | "JUSTIFIED" (fallback for all headings)heading1Alignmentthroughheading5Alignment(string): Individual heading level alignmentsblockquoteAlignment(string): "LEFT" | "RIGHT" | "CENTER" | "JUSTIFIED"
- Text Sizes:
Returns
Promise that resolves to a Blob containing the DOCX file.
downloadDocx(blob: Blob, filename?: string): void
Downloads a DOCX file in the browser environment.
Parameters
blob(Blob): The Blob containing the DOCX file datafilename(string, optional): The name to save the file as (defaults to "document.docx")
Throws
- Error if called outside browser environment
- Error if invalid blob or filename is provided
- Error if file save fails
Markdown Support
The module supports the following Markdown features:
- Table of Contents:
[TOC](place on its own line where TOC should appear) - Page Breaks:
\pagebreak(place on its own line to force a page break) - Headings:
#,##,###,####,##### - Lists:
-,*,1.,2., etc. - Bold:
**text** - Italic:
*text* - Strikethrough:
~~text~~ - Blockquotes:
> text - Tables:
| Header | Header | - Comments:
COMMENT: text - Images:
 - Code blocks: ```code```
- Inline code: `code`
- Links:
[text](url)
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
List Formatting Examples
The module supports rich text formatting within list items:
const markdown = `
- Regular list item
- List item with **bold text** inside
- Item with *italic* and \`code\`
- Mixed **bold** and *italic* formatting
- List item
**Bold text on next line**
1. Numbered list with **bold text**
2. Numbered item with \`code\` and *italic*
3. Mixed **bold** and *italic* text
`;
const blob = await convertMarkdownToDocx(markdown);Basic Usage with TOC and Page Breaks
import { convertMarkdownToDocx, downloadDocx } from "@mohtasham/md-to-docx";
const markdown = `
[TOC]
# Section 1
This is the first section.
## Subsection 1.1
Content for subsection 1.1.
\pagebreak
# Section 2
This is the second section, appearing after a page break.
- Item A
- Item B
`;
// Convert to DOCX
const blob = await convertMarkdownToDocx(markdown);
// Download in browser
downloadDocx(blob, "output_with_toc.docx");