0.0.0 • Published 4 months ago

mdast2docx v0.0.0

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
4 months ago

MDAST (Markdown Abstract Syntax Tree) to DOCX

šŸš€ Effortlessly convert Markdown Abstract Syntax Trees (MDAST) to DOCX.

Test Maintainability Code Coverage Version Downloads Bundle Size


✨ 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?)

ParameterTypeDescription
astInputsRoot | { ast: Root; props?: ISectionProps }[]Single or multiple MDAST trees with optional section properties.
docxPropsIDocxPropsGeneral document properties (title, styles, metadata).
defaultSectionPropsISectionPropsDefault 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 SyntaxSupported in DOCX
Headings # H1 to ###### H6āœ…
Paragraphsāœ…
Bold **text** & Italics _text_āœ…
Blockquotes > quoteāœ…
Lists (ordered & unordered)āœ… (ordered lists via listPlugin)
Links [text](url)āœ…
Images ![alt](url)āœ… (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:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature-new)
  3. Commit your changes (git commit -m "Add new feature")
  4. Push to GitHub (git push origin feature-new)
  5. 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.