mdast2docx logo Md2Docx
Version 1.6.2

Overview

Updated 5 min ago

CIcodecovnpm versionnpm downloadsnpm bundle sizelicense

mdast2docx: The complete, all-in-one Markdown Abstract Syntax Tree (MDAST) to Microsoft Word DOCX compiler bundle.

✨ Features

  • 📦 All-in-One Distribution: Includes @m2d/core and the complete official plugin ecosystem (table, list, image, math, mermaid, html, emoji).
  • 🤖 Optimized for Generative AI: Perfect bridge for converting LLM-generated Markdown into professionally styled Word documents.
  • 📐 Native Word Structures: Renders genuine OpenXML tables, native editable equations (docx.Math), multi-tier outline numbering, and vector diagrams.
  • 🌐 Isomorphic Runtimes: Works identically on both the client (browser downloads via Blob) and server (high-throughput Node.js batch generation via Buffer).
  • ⚡ Streamlined Subpaths: Clean access via mdast2docx, mdast2docx/plugins, mdast2docx/section, and mdast2docx/utils.

📦 Installation

$ pnpm add mdast2docx docx

or

$ npm install mdast2docx docx

or

$ yarn add mdast2docx docx

🚀 Quick Start

import { toDocx } from "mdast2docx";
import {
  htmlPlugin,
  imagePlugin,
  listPlugin,
  mathPlugin,
  mermaidPlugin,
  tablePlugin,
} from "mdast2docx/plugins";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import { unified } from "unified";

const markdown = `
# Executive Summary

This document was generated automatically from **Markdown**.

## Key Metrics

| Metric | Target | Actual |
| :----- | :----: | -----: |
| Latency | < 50ms |   28ms |
| Uptime  | 99.9%  | 99.99% |

## System Flowchart

\`\`\`mermaid
graph LR
  Ingest --> Process --> Storage
\`\`\`

Inline formula: $E = mc^2$
`;

// 1. Parse markdown with unified
const mdast = unified()
  .use(remarkParse)
  .use(remarkGfm)
  .use(remarkMath)
  .parse(markdown);

// 2. Compile to Word DOCX with bundled plugins
const docxBlob = await toDocx(mdast, {}, {
  plugins: [
    htmlPlugin(),
    mermaidPlugin(),
    imagePlugin(),
    tablePlugin(),
    listPlugin(),
    mathPlugin(),
  ],
});

🔌 Bundled Plugins

The mdast2docx/plugins subpath provides instant access to all official plugins:

PluginSource PackageCapability
tablePlugin@m2d/tableRich borders, repeating headers, cell fills, and column alignment
listPlugin@m2d/listHierarchical decimal numbering (1.1.1) and multi-tier bullet glyphs
imagePlugin@m2d/imageSVG and URL image resolution, rasterization, and dimensions
mermaidPlugin@m2d/mermaidClient-side Mermaid diagrams with auto-healing syntax
mathPlugin@m2d/mathLaTeX math translated to native editable Word MathML equations
htmlPlugin@m2d/htmlEmbedded HTML tag parsing and inline CSS styles
emojiPlugin@m2d/emojiGitHub emoji shortcodes to Unicode characters

📚 Deep Dive Guides

License

This library is licensed under the MIT open-source license.


with 💖 by Mayank Kumar Chaudhari

On this page