
remark plugin to compile markdown to docx (Microsoft Word, Office Open XML).
- Uses docx for compilation.
- Works in any environment (e.g. browser, Node.js, CLI).
- Provides reasonable default style and also tunable enough (WIP).
- Supports advanced layouts (RTL, vertical, 2 columns).
- Has own plugin system. You can fully customize markdown to Word transformation.
Supported mdast nodes
Currently, some of the default styles may not be nice. If you have feature requests or improvements, please create a issue or PR.
Demo
https://inokawa.github.io/remark-docx/
Install
npm install remark-docx
Getting started
Browser
import { unified } from "unified";
import markdown from "remark-parse";
import docx from "remark-docx";
import { saveAs } from "file-saver";
const processor = unified().use(markdown).use(docx);
const text = "# hello world";
(async () => {
const doc = await processor.process(text);
const uint8Array = doc.value;
saveAs(new Blob([uint8Array as BlobPart]), "example.docx");
})();
Node.js
import { unified } from "unified";
import markdown from "remark-parse";
import docx from "remark-docx";
import * as fs from "fs";
const processor = unified().use(markdown).use(docx);
const text = "# hello world";
(async () => {
const doc = await processor.process(text);
const uint8Array = doc.value;
fs.writeFileSync("example.docx", uint8Array);
})();
CLI
See remark-cli for usage.
npm install remark-cli
npx remark input.md --use remark-docx --output output.docx
To embed local images, use remark-embed-images with image option.
npx remark input.md --use remark-embed-images --use remark-docx=image:true --output output.docx
Image
Set image option to embed png, jpg, gif and bmp images.
const processor = unified().use(markdown).use(docx, { image: true });
Images are fetched with fetch API by default. You can customize it with load option, which also receives the processed file to resolve relative urls.
import * as fs from "node:fs/promises";
import { pathToFileURL } from "node:url";
const processor = unified()
.use(markdown)
.use(docx, {
image: {
load: (url, file) => fs.readFile(new URL(url, pathToFileURL(file.path))),
},
});
To embed other images such as svg and webp, also set toPng option to convert them to png, which Word can show. On browser, you can use browserToPng, which converts images that the browser can show.
import docx, { browserToPng } from "remark-docx";
const processor = unified()
.use(markdown)
.use(docx, { image: { toPng: browserToPng } });
On other environments like Node.js, implement it by yourself.
import sharp from "sharp";
const processor = unified()
.use(markdown)
.use(docx, {
image: {
toPng: ({ buffer }) => sharp(buffer).png().toBuffer(),
},
});
Plugins
Code
Syntax highlight
Syntax highlighting with shiki.
import { unified } from "unified";
import markdown from "remark-parse";
import docx from "remark-docx";
import { shikiPlugin } from "remark-docx/plugins/shiki";
const processor = unified()
.use(markdown)
.use(docx, { plugins: [shikiPlugin({ theme: "dark-plus" })] });
Image
Render code blocks as images with codeToImagePlugin. render returns image data such as PNG and SVG, or null to leave the code block to the next plugins.
Mermaid
Render Mermaid in code blocks with mermaid language. It only works in browser.
import { unified } from "unified";
import markdown from "remark-parse";
import docx, { browserToPng } from "remark-docx";
import { codeToImagePlugin } from "remark-docx/plugins/code-to-image";
import mermaid from "mermaid";
const processor = unified()
.use(markdown)
.use(docx, {
image: { toPng: browserToPng },
plugins: [
codeToImagePlugin({
render: async ({ lang, value }) => {
if (lang !== "mermaid") return null;
// Mermaid leaves its elements in the container if it fails to render
const container = document.createElement("div");
document.body.appendChild(container);
try {
const { svg } = await mermaid.render("mermaid", value, container);
return new TextEncoder().encode(svg);
} finally {
container.remove();
}
},
}),
],
});
Graphviz
Render Graphviz in code blocks with dot language.
import { unified } from "unified";
import markdown from "remark-parse";
import docx from "remark-docx";
import { codeToImagePlugin } from "remark-docx/plugins/code-to-image";
import sharp from "sharp";
import { instance } from "@viz-js/viz";
const viz = await instance();
const processor = unified()
.use(markdown)
.use(docx, {
image: { toPng: ({ buffer }) => sharp(buffer).png().toBuffer() },
plugins: [
codeToImagePlugin({
render: ({ lang, value }) =>
lang === "dot"
? new TextEncoder().encode(
viz.renderString(value, { format: "svg" }),
)
: null,
}),
],
});
Vega-Lite
Render Vega-Lite in code blocks with vega-lite language.
import { unified } from "unified";
import markdown from "remark-parse";
import docx from "remark-docx";
import { codeToImagePlugin } from "remark-docx/plugins/code-to-image";
import sharp from "sharp";
import { View, parse } from "vega";
import { compile } from "vega-lite";
const processor = unified()
.use(markdown)
.use(docx, {
image: { toPng: ({ buffer }) => sharp(buffer).png().toBuffer() },
plugins: [
codeToImagePlugin({
render: async ({ lang, value }) =>
lang === "vega-lite"
? new TextEncoder().encode(
await new View(parse(compile(JSON.parse(value)).spec), {
renderer: "none",
}).toSVG(),
)
: null,
}),
],
});
SMILES
Render SMILES in code blocks with smiles language, using OpenChemLib.
import { unified } from "unified";
import markdown from "remark-parse";
import docx from "remark-docx";
import { codeToImagePlugin } from "remark-docx/plugins/code-to-image";
import sharp from "sharp";
import { Molecule } from "openchemlib";
const processor = unified()
.use(markdown)
.use(docx, {
image: { toPng: ({ buffer }) => sharp(buffer).png().toBuffer() },
plugins: [
codeToImagePlugin({
render: ({ lang, value }) =>
lang === "smiles"
? new TextEncoder().encode(
Molecule.fromSmiles(value).toSVG(300, 200),
)
: null,
}),
],
});
Other languages can be rendered with servers such as Kroki. Note that code blocks are sent to the server.
Documentation
Contribute
All contributions are welcome. If you find a problem, feel free to create an issue or a PR.
Making a Pull Request
- Fork this repo.
- Run
npm install. - Commit your fix.
- Add tests to cover the fix.
- Make a PR and confirm all the CI checks passed.