mdast2docx 0.0.0-beta.0
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 +170 -0
- package/dist/chunk-KRMGJ4HN.mjs +2 -0
- package/dist/chunk-O6BHSWEF.mjs +37 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +38 -0
- package/dist/index.mjs +1 -0
- package/dist/section.d.ts +30 -0
- package/dist/section.js +38 -0
- package/dist/section.mjs +1 -0
- package/dist/utils.d.ts +55 -0
- package/dist/utils.js +37 -0
- package/dist/utils.mjs +1 -0
- package/package.json +106 -0
package/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# **MDAST (Markdown Abstract Syntax Tree) to DOCX** <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 40px"/>
|
|
2
|
+
|
|
3
|
+
🚀 **Effortlessly convert Markdown Abstract Syntax Trees (MDAST) to DOCX.**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/tiny-md/mdast2docx/actions/workflows/test.yml) [](https://codeclimate.com/github/tiny-md/mdast2docx/maintainability) [](https://codecov.io/gh/tiny-md/mdast2docx) [](https://www.npmjs.com/package/mdast2docx) [](https://www.npmjs.com/package/mdast2docx) 
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## **✨ Features**
|
|
10
|
+
|
|
11
|
+
✅ **MDAST to DOCX conversion** — Supports standard Markdown elements
|
|
12
|
+
✅ **Footnotes handling** — Converts Markdown footnotes to DOCX format
|
|
13
|
+
✅ **Customizable image handling** — Fine-tune image rendering in DOCX
|
|
14
|
+
✅ **Hyperlink support** — Converts external links, internal links [WIP]
|
|
15
|
+
✅ **Configurable sections** — Customize document structure & styling
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## **📦 Installation**
|
|
20
|
+
|
|
21
|
+
Install the package using `pnpm`, `npm`, or `yarn`:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pnpm add mdast2docx
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**_or_**
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install mdast2docx
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**_or_**
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
yarn add mdast2docx
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## **🚀 Usage**
|
|
42
|
+
|
|
43
|
+
### **Basic Example**
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { toDocx } from "mdast2docx";
|
|
47
|
+
import { unified } from "unified";
|
|
48
|
+
import remarkParse from "remark-parse";
|
|
49
|
+
import remarkMmd from "remark-mmd"; // Assumed MMD plugin
|
|
50
|
+
|
|
51
|
+
const markdown = `
|
|
52
|
+
# Sample Document
|
|
53
|
+
This is a **bold** text and _italic_ text.
|
|
54
|
+
|
|
55
|
+
> A blockquote example
|
|
56
|
+
|
|
57
|
+
- List Item 1
|
|
58
|
+
- List Item 2
|
|
59
|
+
|
|
60
|
+
[Click Here](https://example.com)
|
|
61
|
+
|
|
62
|
+

|
|
63
|
+
|
|
64
|
+
This is a footnote reference[^1].
|
|
65
|
+
|
|
66
|
+
[^1]: This is the footnote content.
|
|
67
|
+
`;
|
|
68
|
+
|
|
69
|
+
const mdast = unified().use(remarkParse).use(remarkMmd).parse(markdown);
|
|
70
|
+
|
|
71
|
+
(async () => {
|
|
72
|
+
const docxBlob = await toDocx(mdast, { title: "My Document" }, {});
|
|
73
|
+
const url = URL.createObjectURL(docxBlob as Blob);
|
|
74
|
+
const link = document.createElement("a");
|
|
75
|
+
link.href = url;
|
|
76
|
+
link.download = "document.docx";
|
|
77
|
+
link.click();
|
|
78
|
+
URL.revokeObjectURL(url);
|
|
79
|
+
})();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## **🛠 API Reference**
|
|
85
|
+
|
|
86
|
+
### **`toDocx(astInputs, docxProps, defaultSectionProps, outputType?)`**
|
|
87
|
+
|
|
88
|
+
| Parameter | Type | Description |
|
|
89
|
+
| ------------------------- | -------------------------------------------------- | ---------------------------------------------------------------- |
|
|
90
|
+
| `astInputs` | `Root` \| `{ ast: Root; props?: ISectionProps }[]` | Single or multiple MDAST trees with optional section properties. |
|
|
91
|
+
| `docxProps` | `IDocxProps` | General document properties (title, styles, metadata). |
|
|
92
|
+
| `defaultSectionProps` | `ISectionProps` | Default properties for document sections. |
|
|
93
|
+
| `outputType` _(optional)_ | `"blob"` (default) \| `"buffer"` \| `"base64"` | Format of the generated DOCX document. |
|
|
94
|
+
|
|
95
|
+
📌 **Returns:** A `Promise` resolving to a DOCX document in the chosen format.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## **📜 Supported Markdown Elements**
|
|
100
|
+
|
|
101
|
+
| Markdown Syntax | Supported in DOCX |
|
|
102
|
+
| ---------------------------------- | ---------------------- |
|
|
103
|
+
| Headings `# H1` to `###### H6` | ✅ |
|
|
104
|
+
| Paragraphs | ✅ |
|
|
105
|
+
| Bold `**text**` & Italics `_text_` | ✅ |
|
|
106
|
+
| Blockquotes `> quote` | ✅ |
|
|
107
|
+
| Lists (ordered & unordered) | ✅ |
|
|
108
|
+
| Links `[text](url)` | ✅ |
|
|
109
|
+
| Images `` | ✅ |
|
|
110
|
+
| Code Blocks `` `code` `` | ✅ |
|
|
111
|
+
| Footnotes `[^1]` | ✅ |
|
|
112
|
+
| Tables | 🚧 _(Coming soon)_ |
|
|
113
|
+
| HTML Tags | 🚧 _(Partial support)_ |
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## **🔧 Configuration**
|
|
118
|
+
|
|
119
|
+
You can customize the DOCX output using `ISectionProps` and `IDocxProps`.
|
|
120
|
+
|
|
121
|
+
### **Example: Customizing Styles**
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const docxProps = {
|
|
125
|
+
title: "Styled Document",
|
|
126
|
+
author: "John Doe",
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const sectionProps = {
|
|
130
|
+
page: { margin: { top: 1000, bottom: 1000, left: 1200, right: 1200 } },
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const docxBlob = await toDocx(mdast, docxProps, sectionProps);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
📖 **More details:**
|
|
137
|
+
|
|
138
|
+
- [DOCX.js Document Properties](https://docx.js.org/#/usage/document)
|
|
139
|
+
- [DOCX.js Section Options](https://docx.js.org/#/usage/sections)
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## **📝 Contributing**
|
|
144
|
+
|
|
145
|
+
We welcome contributions! To get started:
|
|
146
|
+
|
|
147
|
+
1. **Fork** the repository
|
|
148
|
+
2. **Create a feature branch** (`git checkout -b feature-new`)
|
|
149
|
+
3. **Commit your changes** (`git commit -m "Add new feature"`)
|
|
150
|
+
4. **Push to GitHub** (`git push origin feature-new`)
|
|
151
|
+
5. **Open a Pull Request** 🚀
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## **📄 License**
|
|
156
|
+
|
|
157
|
+
This project is **licensed under MPL-2.0**. See the [LICENSE](./LICENSE) file for details.
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## **🙌 Acknowledgments**
|
|
162
|
+
|
|
163
|
+
Thanks to the **docx.js** and **unified.js** ecosystems for making this possible.
|
|
164
|
+
|
|
165
|
+
> ⭐ **Star this repository** if you find it useful!
|
|
166
|
+
> ❤️ Support our work by [sponsoring](https://github.com/sponsors/mayank1513).
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
<p align="center">Made with 💖 by <a href="https://mayank-chaudhari.vercel.app" target="_blank">Mayank Kumar Chaudhari</a></p>
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{a as w,b as p,e as f,f as k,g as I,h as y,j as g,n as R}from"./chunk-O6BHSWEF.mjs";var b=(h,m,u)=>{let d=async(t,i)=>{var r,o,s;let n=new Set(i),c={bold:i.has("strong"),italics:i.has("emphasis"),strike:i.has("delete")},a=(o=t.url)!=null?o:h[(r=t.identifier)==null?void 0:r.toUpperCase()],e=(s=t.alt)!=null?s:a==null?void 0:a.split("/").pop();switch(t.type){case"text":return[new p({text:t.value,...c})];case"break":return[new p({break:1})];case"inlineCode":return[new p({text:t.value,...c,style:"code"})];case"emphasis":case"strong":case"delete":return n.add(t.type),l(t,n);case"link":case"linkReference":return n.add("link"),[a.startsWith("#")?new k({anchor:a,children:await l(t,n)}):new I({link:a,children:await l(t,n)})];case"image":case"imageReference":return[new f({...await u(a),altText:{description:e,name:e,title:e}})];case"footnoteReference":return[new g(m[t.identifier].id)];default:return[new p("")]}},l=async(t,i=new Set)=>{var n;return(await Promise.all((n=t.children)==null?void 0:n.map(c=>d(c,i)))).flat()};return l},S=async(h,m,u,d)=>{let{imageResolver:l,useTitle:t,...i}={...R,...d},n=b(m,u,l),c=async(e,r)=>{let o=[...r],s={};switch(r.includes("list")&&(s.bullet={level:r.filter(P=>P==="list").length-1}),r.includes("blockquote")&&(s.style="Quote"),e.type){case"paragraph":return[new y({...s,children:await n(e)})];case"heading":return[new y({heading:t?e.depth===1?"Title":`Heading${e.depth-1}`:`Heading${e.depth}`,children:await n(e)})];case"code":return[new y({alignment:"start",style:"blockCode",children:e.value.split(`
|
|
2
|
+
`).map(P=>new p({text:P,break:1,style:"code"}))})];case"list":case"blockquote":case"listItem":return o.push(e.type),a(e,o);case"thematicBreak":return[new y({border:{top:{style:w.SINGLE,size:6}}})];case"table":case"tableRow":case"tableCell":case"yaml":case"html":default:return console.warn(`Unsupported node type: ${e.type}`,e),[]}},a=async(e,r)=>{var o;return(await Promise.all((o=e.children)==null?void 0:o.map(s=>c(s,r)))).flat()};return{...i,children:await a(h,[])}};export{S as a};
|