mdast2docx 1.4.2 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -148
- package/dist/chunk-CWUY2JLF.mjs +4 -0
- package/dist/plugins/index.d.ts +3 -3
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/node/index.js +8 -8
- package/dist/plugins/node/index.mjs +1 -1
- package/dist/plugins/node/node-image.d.ts +3 -2
- package/dist/plugins/node/node-image.js +8 -8
- package/dist/plugins/node/node-image.mjs +1 -1
- package/package.json +16 -64
- package/dist/chunk-2EAXVYAR.mjs +0 -4
package/README.md
CHANGED
|
@@ -1,58 +1,47 @@
|
|
|
1
1
|
# **MDAST (Markdown Abstract Syntax Tree) to DOCX** <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 40px"/>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**🚀 A powerful, modular, and extensible Markdown to DOCX converter.**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="./banner.svg" alt="logo" style="height: 360px; margin:auto" />
|
|
7
|
+
</p>
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## **✨ Features**
|
|
10
|
-
|
|
11
|
-
✅ **MDAST to DOCX conversion** — Supports standard Markdown elements
|
|
12
|
-
✅ **Footnotes handling** — Converts Markdown footnotes to DOCX format
|
|
13
|
-
✅ **Tables support** — Converts Markdown tables into DOCX tables
|
|
14
|
-
✅ **Hyperlink support** — External and internal hyperlinks are now fully functional
|
|
15
|
-
✅ **New Plugin Architecture** — Extend and customize DOCX output with plugins
|
|
16
|
-
✅ **Customizable image handling** — Images are now supported via `@m2d/image` plugin
|
|
9
|
+
MD2DOCX is a robust library for converting Markdown Abstract Syntax Trees (MDAST) into DOCX documents. Built on the powerful `unified.js` and `docx.js` ecosystems, it offers a flexible, plugin-driven architecture that works seamlessly on both the client and server.
|
|
17
10
|
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
[](https://github.com/md2docx/mdast2docx/actions/workflows/test.yml)
|
|
12
|
+
[](https://codeclimate.com/github/md2docx/mdast2docx/maintainability)
|
|
13
|
+
[](https://codecov.io/gh/md2docx/mdast2docx)
|
|
14
|
+
[](https://www.npmjs.com/package/mdast2docx)
|
|
15
|
+
[](https://www.npmjs.com/package/mdast2docx)
|
|
16
|
+

|
|
20
17
|
|
|
21
18
|
---
|
|
22
19
|
|
|
23
|
-
##
|
|
24
|
-
|
|
25
|
-
<details open>
|
|
26
|
-
<summary>
|
|
27
|
-
<i>Converts markdown content/artifacts generated by Generative AI to docx for further processing/record-keeping/sharing.</i>
|
|
28
|
-
</summary>
|
|
29
|
-
|
|
30
|
-
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.
|
|
31
|
-
|
|
32
|
-
### **How It Works**
|
|
20
|
+
## ✨ Key Features
|
|
33
21
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
22
|
+
- **Modular Architecture**: Keep your bundles small. The core is lightweight, and you can add features like tables, images, or math support via plugins.
|
|
23
|
+
- **Comprehensive Markdown Support**: Handles all standard Markdown syntax, including headings, lists, links, code blocks, and more.
|
|
24
|
+
- **Client & Server Ready**: Isomorphic design works flawlessly in both Node.js and browser environments.
|
|
25
|
+
- **Highly Customizable**: Leverage the full power of `docx.js` to style your document, including headers, footers, margins, and paragraph styles.
|
|
26
|
+
- **Scoped Packages**: Install only what you need using `@m2d/*` packages, or grab the all-in-one `mdast2docx` package.
|
|
37
27
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
- **AI-Generated Reports** – Convert AI-generated Markdown reports into polished DOCX files.
|
|
41
|
-
- **AI-Powered Content Export** – Transform AI-generated blogs, research papers, and structured content into a DOCX format for easy sharing and editing.
|
|
42
|
-
- **Client & Server Flexibility** – Works both in **browser-based AI tools** and **high-performance server environments**.
|
|
28
|
+
---
|
|
43
29
|
|
|
44
|
-
|
|
30
|
+
## 🤖 The Generative AI Power-Up
|
|
45
31
|
|
|
46
|
-
|
|
47
|
-
- **Optimized & Extensible** – Use **only the plugins you need**, keeping your AI workflows lightweight.
|
|
32
|
+
Modern **Generative AI** tools often produce structured content in **Markdown**. This library acts as the perfect bridge to convert AI-generated artifacts into professionally formatted DOCX documents for editing, sharing, or record-keeping.
|
|
48
33
|
|
|
49
|
-
|
|
34
|
+
**Workflow:** `AI Output (Markdown)` → `unified/remark (MDAST)` → `mdast2docx` → `Professional DOCX Document`
|
|
50
35
|
|
|
51
|
-
|
|
36
|
+
- **AI-Generated Reports**: Convert AI-generated summaries and reports into polished DOCX files.
|
|
37
|
+
- **Content Export**: Transform AI-generated blogs, articles, and research into an editable DOCX format.
|
|
38
|
+
- **Flexible Processing**: Offload document generation to the client for scalability or run it on the server for high-performance batch processing.
|
|
52
39
|
|
|
53
40
|
---
|
|
54
41
|
|
|
55
|
-
##
|
|
42
|
+
## 📦 Installation
|
|
43
|
+
|
|
44
|
+
You can install the all-in-one package or select only the scoped packages you need.
|
|
56
45
|
|
|
57
46
|
### **Install Everything at Once**
|
|
58
47
|
|
|
@@ -60,182 +49,173 @@ By integrating **MDAST to DOCX**, AI applications can **seamlessly export Markdo
|
|
|
60
49
|
pnpm add mdast2docx
|
|
61
50
|
```
|
|
62
51
|
|
|
63
|
-
### **Or Install Only What You Need
|
|
52
|
+
### **Or Install Only What You Need**
|
|
64
53
|
|
|
65
54
|
```bash
|
|
66
|
-
pnpm add @m2d/core @m2d/
|
|
55
|
+
pnpm add @m2d/core @m2d/image @m2d/table # and other plugins
|
|
67
56
|
```
|
|
68
57
|
|
|
69
58
|
---
|
|
70
59
|
|
|
71
|
-
##
|
|
60
|
+
## 🚀 Usage
|
|
61
|
+
|
|
62
|
+
You can use the library at a high level with `@m2d/remark-docx` for seamless integration with unified plugin system or at a lower level with `@m2d/core` + plugins ro `mdast2docx` for full control.
|
|
72
63
|
|
|
73
64
|
### **Basic Example**
|
|
74
65
|
|
|
66
|
+
This example uses the core `toDocx` function to convert an MDAST object into a DOCX file and trigger a download in the browser.
|
|
67
|
+
|
|
75
68
|
```typescript
|
|
76
69
|
import { toDocx } from "mdast2docx";
|
|
77
70
|
import { unified } from "unified";
|
|
78
71
|
import remarkParse from "remark-parse";
|
|
79
|
-
import remarkMmd from "remark-mmd"; // Assumed MMD plugin
|
|
80
72
|
|
|
73
|
+
// 1. Your Markdown content
|
|
81
74
|
const markdown = `
|
|
82
|
-
# Sample Document
|
|
83
|
-
This is a **bold**
|
|
75
|
+
# Sample Document
|
|
76
|
+
This is a paragraph with **bold** and _italic_ text.
|
|
84
77
|
|
|
85
|
-
> A blockquote
|
|
78
|
+
> A blockquote.
|
|
86
79
|
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
* List Item 1
|
|
81
|
+
* List Item 2
|
|
89
82
|
|
|
90
|
-
[
|
|
91
|
-
|
|
92
|
-
This is a footnote reference[^1].
|
|
93
|
-
|
|
94
|
-
[^1]: This is the footnote content.
|
|
83
|
+
[Visit our website](https://example.com)
|
|
95
84
|
`;
|
|
96
85
|
|
|
97
|
-
|
|
86
|
+
// 2. Parse Markdown to MDAST
|
|
87
|
+
const mdast = unified().use(remarkParse).parse(markdown);
|
|
98
88
|
|
|
99
|
-
|
|
100
|
-
|
|
89
|
+
// 3. Convert MDAST to DOCX and download
|
|
90
|
+
async function generateDocx() {
|
|
91
|
+
const docxBlob = await toDocx(mdast);
|
|
92
|
+
|
|
93
|
+
// Create a download link
|
|
101
94
|
const url = URL.createObjectURL(docxBlob as Blob);
|
|
102
95
|
const link = document.createElement("a");
|
|
103
96
|
link.href = url;
|
|
104
97
|
link.download = "document.docx";
|
|
98
|
+
document.body.appendChild(link);
|
|
105
99
|
link.click();
|
|
100
|
+
document.body.removeChild(link);
|
|
106
101
|
URL.revokeObjectURL(url);
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
---
|
|
111
|
-
|
|
112
|
-
## **📜 API Reference**
|
|
113
|
-
|
|
114
|
-
### **`toDocx(astInputs, docxProps, defaultSectionProps, outputType?)`**
|
|
115
|
-
|
|
116
|
-
| Parameter | Type | Description |
|
|
117
|
-
| ------------------------- | -------------------------------------------------- | ---------------------------------------------------------------- |
|
|
118
|
-
| `astInputs` | `Root` \| `{ ast: Root; props?: ISectionProps }[]` | Single or multiple MDAST trees with optional section properties. |
|
|
119
|
-
| `docxProps` | `IDocxProps` | General document properties (title, styles, metadata). |
|
|
120
|
-
| `defaultSectionProps` | `ISectionProps` | Default properties for document sections. |
|
|
121
|
-
| `outputType` _(optional)_ | `"blob"` (default) \| `"buffer"` \| `"base64"` | Format of the generated DOCX document. |
|
|
102
|
+
}
|
|
122
103
|
|
|
123
|
-
|
|
104
|
+
generateDocx();
|
|
105
|
+
```
|
|
124
106
|
|
|
125
107
|
---
|
|
126
108
|
|
|
127
|
-
##
|
|
128
|
-
|
|
129
|
-
| Markdown Syntax | Supported in DOCX |
|
|
130
|
-
| ---------------------------------- | ------------------------------------- |
|
|
131
|
-
| Headings `# H1` to `###### H6` | ✅ |
|
|
132
|
-
| Paragraphs | ✅ |
|
|
133
|
-
| Bold `**text**` & Italics `_text_` | ✅ |
|
|
134
|
-
| Blockquotes `> quote` | ✅ |
|
|
135
|
-
| Lists (ordered & unordered) | ✅ _(ordered lists via `listPlugin`)_ |
|
|
136
|
-
| Links `[text](url)` | ✅ |
|
|
137
|
-
| Images `` | ✅ _(via `imagePlugin`)_ |
|
|
138
|
-
| Code Blocks `` `code` `` | ✅ |
|
|
139
|
-
| Footnotes `[^1]` | ✅ |
|
|
140
|
-
| Tables | ✅ _(via `tablePlugin`)_ |
|
|
141
|
-
| Hyperlinks (external & internal) | ✅ |
|
|
142
|
-
| Latex Math | ✅ _(via `mathPlugin`)_ |
|
|
143
|
-
| HTML Tags | ✅ _(via `htmlPlugin`)_ |
|
|
144
|
-
|
|
145
|
-
---
|
|
109
|
+
## 🔌 Plugins
|
|
146
110
|
|
|
147
|
-
|
|
111
|
+
The core library is intentionally lean. Use plugins to add support for features like images, tables, math equations, and more.
|
|
148
112
|
|
|
149
|
-
|
|
113
|
+
### **Example: Using Plugins**
|
|
150
114
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
const docxProps = {
|
|
155
|
-
title: "Styled Document",
|
|
156
|
-
author: "John Doe",
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
const sectionProps = {
|
|
160
|
-
page: { margin: { top: 1000, bottom: 1000, left: 1200, right: 1200 } },
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const docxBlob = await toDocx(mdast, docxProps, sectionProps);
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### **Use Plugins**
|
|
167
|
-
|
|
168
|
-
Plugins allow extending functionality like adding image or table support.
|
|
115
|
+
To use plugins, install them and pass them into the configuration. You can apply plugins globally to all sections using defaultSectionProps or specify them for each section individually.
|
|
169
116
|
|
|
170
117
|
```typescript
|
|
171
118
|
import { toDocx } from "@m2d/core";
|
|
172
119
|
import { imagePlugin } from "@m2d/image";
|
|
173
120
|
import { tablePlugin } from "@m2d/table";
|
|
174
|
-
import { listPlugin } from "@m2d/list";
|
|
175
121
|
import { mathPlugin } from "@m2d/math";
|
|
122
|
+
import { unified } from "unified";
|
|
123
|
+
import remarkParse from "remark-parse";
|
|
124
|
+
import remarkGfm from "remark-gfm"; // For table support
|
|
125
|
+
import remarkMath from "remark-math"; // For math support
|
|
126
|
+
|
|
127
|
+
const markdownWithFeatures = `
|
|
128
|
+
An image: 
|
|
129
|
+
|
|
130
|
+
| Header 1 | Header 2 |
|
|
131
|
+
|----------|----------|
|
|
132
|
+
| Cell 1 | Cell 2 |
|
|
133
|
+
|
|
134
|
+
A math equation: $E = mc^2$
|
|
135
|
+
`;
|
|
136
|
+
|
|
137
|
+
const mdast = unified()
|
|
138
|
+
.use(remarkParse)
|
|
139
|
+
.use(remarkGfm) // Enable GFM for tables
|
|
140
|
+
.use(remarkMath) // Enable math syntax
|
|
141
|
+
.parse(markdownWithFeatures);
|
|
176
142
|
|
|
177
|
-
|
|
178
|
-
toDocx(
|
|
143
|
+
(async () => {
|
|
144
|
+
const docxBlob = await toDocx(
|
|
179
145
|
mdast,
|
|
180
|
-
{},
|
|
181
|
-
{
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
link.click();
|
|
189
|
-
URL.revokeObjectURL(url);
|
|
190
|
-
});
|
|
191
|
-
};
|
|
146
|
+
{}, // docxProps
|
|
147
|
+
{
|
|
148
|
+
// Pass plugins in sectionProps
|
|
149
|
+
plugins: [imagePlugin(), tablePlugin(), mathPlugin()],
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
// ... download logic ...
|
|
153
|
+
})();
|
|
192
154
|
```
|
|
193
155
|
|
|
194
|
-
|
|
156
|
+
---
|
|
195
157
|
|
|
196
|
-
|
|
197
|
-
- [DOCX.js Section Options](https://docx.js.org/#/usage/sections)
|
|
158
|
+
## 📜 API Reference
|
|
198
159
|
|
|
199
|
-
|
|
160
|
+
### **`toDocx(astInputs, docxProps?, sectionProps?, outputType?)`**
|
|
200
161
|
|
|
201
|
-
|
|
162
|
+
Converts one or more MDAST trees into a DOCX document.
|
|
202
163
|
|
|
203
|
-
|
|
164
|
+
| Parameter | Type | Description |
|
|
165
|
+
| :------------- | :-------------- | :-------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
|
|
166
|
+
| `astInputs` | `Root` | `{ ast: Root; props?: ISectionProps }[]` | A single MDAST root node or an array of nodes, each with optional section properties. |
|
|
167
|
+
| `docxProps` | `IDocxProps` | Optional. Global document properties like title, author, and styles. See [docx.js docs](https://docx.js.org/#/usage/document). |
|
|
168
|
+
| `sectionProps` | `ISectionProps` | Optional. Default properties for document sections, like margins and plugins. See [docx.js docs](https://docx.js.org/#/usage/sections). |
|
|
169
|
+
| `outputType` | `"blob"` | `"buffer"` | `"base64"` | Optional. The output format. Defaults to `"blob"` in browsers and `"buffer"` in Node.js. |
|
|
204
170
|
|
|
205
|
-
|
|
206
|
-
- Enables **offloading document generation** to users' browsers or running high-performance server renderers
|
|
207
|
-
- Combine it with `unified` and `remark` to parse Markdown into MDAST, and then convert to DOCX seamlessly
|
|
171
|
+
**Returns:** `Promise<Blob | Buffer | string>` — A promise that resolves with the generated DOCX document in the specified format.
|
|
208
172
|
|
|
209
173
|
---
|
|
210
174
|
|
|
211
|
-
##
|
|
175
|
+
## ✅ Supported Markdown Elements
|
|
176
|
+
|
|
177
|
+
| Feature | Supported | Plugin Required |
|
|
178
|
+
| :--------------------- | :-------: | :-------------- |
|
|
179
|
+
| Headings (`# H1`) | ✅ | **No** |
|
|
180
|
+
| Paragraphs | ✅ | **No** |
|
|
181
|
+
| Bold & Italic | ✅ | **No** |
|
|
182
|
+
| Blockquotes | ✅ | **No** |
|
|
183
|
+
| Links | ✅ | **No** |
|
|
184
|
+
| Code Blocks | ✅ | **No** |
|
|
185
|
+
| Footnotes | ✅ | **No** |
|
|
186
|
+
| Unordered Lists | ✅ | **No** |
|
|
187
|
+
| Images `` | ✅ | `@m2d/image` |
|
|
188
|
+
| Tables (GFM) | ✅ | `@m2d/table` |
|
|
189
|
+
| Ordered Lists | ✅ | `@m2d/list` |
|
|
190
|
+
| Math Equations (LaTeX) | ✅ | `@m2d/math` |
|
|
191
|
+
| Raw HTML | ✅ | `@m2d/html` |
|
|
212
192
|
|
|
213
|
-
|
|
193
|
+
---
|
|
214
194
|
|
|
215
|
-
|
|
216
|
-
2. **Create a feature branch** (`git checkout -b feature-new`)
|
|
217
|
-
3. **Commit your changes** (`git commit -m "Add new feature"`)
|
|
218
|
-
4. **Push to GitHub** (`git push origin feature-new`)
|
|
219
|
-
5. **Open a Pull Request** 🚀
|
|
195
|
+
## 🤝 Contributing
|
|
220
196
|
|
|
221
|
-
|
|
197
|
+
We welcome contributions\! Please feel free to fork the repository, create a feature branch, and open a pull request.
|
|
222
198
|
|
|
223
|
-
|
|
199
|
+
1. **Fork** the repository.
|
|
200
|
+
2. **Create a feature branch** (`git checkout -b feature/your-new-feature`).
|
|
201
|
+
3. **Commit your changes** (`git commit -m "Add a new feature"`).
|
|
202
|
+
4. **Push to the branch** (`git push origin feature/your-new-feature`).
|
|
203
|
+
5. **Open a Pull Request**. 🚀
|
|
224
204
|
|
|
225
205
|
---
|
|
226
206
|
|
|
227
|
-
##
|
|
207
|
+
## 📄 License
|
|
228
208
|
|
|
229
|
-
This project is
|
|
209
|
+
This project is licensed under the **MPL-2.0**. See the [LICENSE](./LICENSE) file for details.
|
|
230
210
|
|
|
231
211
|
---
|
|
232
212
|
|
|
233
|
-
##
|
|
213
|
+
## 🙌 Acknowledgments
|
|
234
214
|
|
|
235
|
-
|
|
215
|
+
A huge thank you to the creators and maintainers of the **docx.js** and **unified.js** ecosystems, which make this library possible.
|
|
236
216
|
|
|
237
217
|
> ⭐ **Star this repository** if you find it useful!
|
|
238
|
-
> ❤️ Support our work by [sponsoring](https://github.com/sponsors/mayank1513).
|
|
218
|
+
> ❤️ Support our work by [sponsoring on GitHub](https://github.com/sponsors/mayank1513).
|
|
239
219
|
|
|
240
220
|
---
|
|
241
221
|
|