
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
mdast-to-docx
Advanced tools
Convert Markdown Abstract Syntax Tree (MDAST) to DOCX seamlessly. Supports footnotes, images, links, and customizable document properties.
🚀 Effortlessly convert Markdown Abstract Syntax Trees (MDAST) to DOCX.
✅ MDAST to DOCX conversion — Supports standard Markdown elements
✅ Footnotes handling — Converts Markdown footnotes to DOCX format
✅ Tables support — Converts Markdown tables into DOCX tables
✅ Hyperlink support — External and internal hyperlinks are now fully functional
✅ New Plugin Architecture — Extend and customize DOCX output with plugins
✅ Customizable image handling — Images are now supported via @m2d/image
plugin
Note: Images are no longer supported by default. To enable image support, use the image plugin from
@m2d/image
ormdast2docx/plugins
explicitly.
This change helps us keep the library lean and extensible by community via plugins.
Modern Generative AI tools often produce structured content in Markdown (MD) format. However, converting this AI-generated Markdown into professional DOCX documents requires an additional transformation step. This is where MDAST to DOCX comes in.
By integrating MDAST to DOCX, AI applications can seamlessly export Markdown-based content into DOCX, making it more accessible, editable, and shareable. 🚀
pnpm add mdast2docx
pnpm add @m2d/core @m2d/html @m2d/image @m2d/math @m2d/table @m2d/list @m2d/mdast
import { toDocx } from "mdast2docx";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkMmd from "remark-mmd"; // Assumed MMD plugin
const markdown = `
# Sample Document
This is a **bold** text and _italic_ text.
> A blockquote example
- List Item 1
- List Item 2
[Click Here](https://example.com)
This is a footnote reference[^1].
[^1]: This is the footnote content.
`;
const mdast = unified().use(remarkParse).use(remarkMmd).parse(markdown);
(async () => {
const docxBlob = await toDocx(mdast, { title: "My Document" }, {});
const url = URL.createObjectURL(docxBlob as Blob);
const link = document.createElement("a");
link.href = url;
link.download = "document.docx";
link.click();
URL.revokeObjectURL(url);
})();
toDocx(astInputs, docxProps, defaultSectionProps, outputType?)
Parameter | Type | Description |
---|---|---|
astInputs | Root | { ast: Root; props?: ISectionProps }[] | Single or multiple MDAST trees with optional section properties. |
docxProps | IDocxProps | General document properties (title, styles, metadata). |
defaultSectionProps | ISectionProps | Default properties for document sections. |
outputType (optional) | "blob" (default) | "buffer" | "base64" | Format of the generated DOCX document. |
📌 Returns: A Promise
resolving to a DOCX document in the chosen format.
Markdown Syntax | Supported in DOCX |
---|---|
Headings # H1 to ###### H6 | ✅ |
Paragraphs | ✅ |
Bold **text** & Italics _text_ | ✅ |
Blockquotes > quote | ✅ |
Lists (ordered & unordered) | ✅ (ordered lists via listPlugin ) |
Links [text](url) | ✅ |
Images  | ✅ (via imagePlugin ) |
Code Blocks `code` | ✅ |
Footnotes [^1] | ✅ |
Tables | ✅ (via tablePlugin ) |
Hyperlinks (external & internal) | ✅ |
Latex Math | ✅ (via mathPlugin ) |
HTML Tags | ✅ (via htmlPlugin ) |
You can customize the DOCX output using ISectionProps
and IDocxProps
.
const docxProps = {
title: "Styled Document",
author: "John Doe",
};
const sectionProps = {
page: { margin: { top: 1000, bottom: 1000, left: 1200, right: 1200 } },
};
const docxBlob = await toDocx(mdast, docxProps, sectionProps);
Plugins allow extending functionality like adding image or table support.
import { toDocx } from "@m2d/core";
import { imagePlugin } from "@m2d/image";
import { tablePlugin } from "@m2d/table";
import { listPlugin } from "@m2d/list";
import { mathPlugin } from "@m2d/math";
const downloadDocx = () => {
toDocx(
mdast,
{},
{ plugins: [imagePlugin(), tablePlugin(), listPlugin(), mathPlugin()] },
"blob",
).then(blob => {
const url = URL.createObjectURL(blob as Blob);
const link = document.createElement("a");
link.href = url;
link.download = "my-document.docx";
link.click();
URL.revokeObjectURL(url);
});
};
📖 More details:
AI tools often generate Markdown as output (e.g., reports, documentation, summaries). This library makes it easy to convert those Markdown-based outputs into rich DOCX documents.
unified
and remark
to parse Markdown into MDAST, and then convert to DOCX seamlesslyWe welcome contributions! To get started:
git checkout -b feature-new
)git commit -m "Add new feature"
)git push origin feature-new
)The community can create plugins to extend the functionality of this library. For inspiration, check out the existing plugins in the lib/src/plugins
folder.
This project is licensed under MPL-2.0. See the LICENSE file for details.
Thanks to the docx.js and unified.js ecosystems for making this possible.
⭐ Star this repository if you find it useful!
❤️ Support our work by sponsoring.
Made with 💖 by Mayank Kumar Chaudhari
FAQs
Convert Markdown Abstract Syntax Tree (MDAST) to DOCX seamlessly. Supports footnotes, images, links, and customizable document properties.
The npm package mdast-to-docx receives a total of 3 weekly downloads. As such, mdast-to-docx popularity was classified as not popular.
We found that mdast-to-docx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.