mdast2docx v0.0.0
MDAST (Markdown Abstract Syntax Tree) to DOCX
š Effortlessly convert Markdown Abstract Syntax Trees (MDAST) to DOCX.
⨠Features
ā
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 imagePlugin
Note: Images are no longer supported by default. To enable image support, use the
imagePlugin
explicitly.This change helps us keep the library lean and extensible by community via plugins.
š¦ Installation
Install the package using pnpm
, npm
, or yarn
:
pnpm add mdast2docx
or
npm install mdast2docx
or
yarn add mdast2docx
š Usage
Basic Example
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);
})();
š API Reference
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.
š Supported Markdown Elements
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) | ā |
HTML Tags | š§ (Work in progress) |
š§ Configuration
You can customize the DOCX output using ISectionProps
and IDocxProps
.
Example: Customizing Styles
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);
Use Plugins
Plugins allow extending functionality like adding image or table support.
import { toDocx } from "mdast2docx";
import { imagePlugin, tablePlugin, listPlugin, mathPlugin } from "mdast2docx/dist/plugins";
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:
š Contributing
We welcome contributions! To get started:
- Fork the repository
- Create a feature branch (
git checkout -b feature-new
) - Commit your changes (
git commit -m "Add new feature"
) - Push to GitHub (
git push origin feature-new
) - Open a Pull Request š
Extend Functionality with Plugins
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.
š License
This project is licensed under MPL-2.0. See the LICENSE file for details.
š Acknowledgments
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.
4 months ago
4 months ago