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
- 📝 Support for all heading levels (H1-H5)
- 📋 Bullet points and numbered lists
- 📊 Tables with headers and data
- 🔤 Bold and italic text formatting
- 💬 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
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 1
- Bullet point 2
**Bold text in list**
1. Numbered item 1
2. Numbered item 2
> This is a blockquote
| Header 1 | Header 2 |
|----------|----------|
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
# Multi-line Code Block
\`\`\`typescript
function hello(name: string): string {
return \`Hello, \${name}!\`;
}
const result = hello("World");
console.log(result);
\`\`\`
# Image Test
This is a test with an embedded image.

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:
- 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.