npm.io
1.6.4 • Published 18h ago

mdast2docx

Licence
MIT
Version
1.6.4
Deps
10
Size
17 kB
Vulns
0
Weekly
0
Stars
40

mdast2docx

CI codecov npm version npm downloads npm bundle size license

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:

Plugin Source Package Capability
tablePlugin @m2d/table Rich borders, repeating headers, cell fills, and column alignment
listPlugin @m2d/list Hierarchical decimal numbering (1.1.1) and multi-tier bullet glyphs
imagePlugin @m2d/image SVG and URL image resolution, rasterization, and dimensions
mermaidPlugin @m2d/mermaid Client-side Mermaid diagrams with auto-healing syntax
mathPlugin @m2d/math LaTeX math translated to native editable Word MathML equations
htmlPlugin @m2d/html Embedded HTML tag parsing and inline CSS styles
emojiPlugin @m2d/emoji GitHub emoji shortcodes to Unicode characters

Deep Dive Guides


License

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


with by Mayank Kumar Chaudhari

Keywords