Overview
Updated 10 Nov 2026
@m2d/code-lowlight: Battle-tested syntax highlighting plugin for md2docx powered by lowlight (highlight.js). Delivers 100+ language grammars, automatic language detection, preset Word themes, gutter line numbers, and Run Coalescing into clean OpenXML.
✨ Features
- 🌟 Powered by Lowlight (highlight.js): Battle-tested grammar parsers covering 100+ programming languages and dialects.
- 🔍 Auto Language Detection: Automatically analyzes code snippets and detects programming languages when no language tag is provided.
- 🎨 Authentic Word Themes: Includes
github-light,github-dark,monokai, andexecutive-printwith OpenXML background shading (<w:shd w:fill="HEX">). - 🔢 Gutter Line Numbering: Render code blocks as 2-column Word tables with right-aligned, muted line number columns.
- ⚡ OpenXML Run Coalescing: Automatically combines adjacent tokens with matching styles to cut
<w:r>XML node bloat by 30%–50%. - 🛡️ Git Diff Highlighting: Supports diff additions (
+) and deletions (-) with green and red run/background tints.
📦 Installation
$ pnpm add @m2d/code-lowlight @m2d/core docxor
$ npm install @m2d/code-lowlight @m2d/core docxor
$ yarn add @m2d/code-lowlight @m2d/core docx🚀 Quick Start
import { toDocx } from "@m2d/core";
import { codeLowlightPlugin } from "@m2d/code-lowlight";
import remarkParse from "remark-parse";
import { unified } from "unified";
const markdown = `
# System Architecture
\`\`\`typescript showLineNumbers
import { createServer } from "node:http";
const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "ok" }));
});
server.listen(3000);
\`\`\`
`;
const ast = unified().use(remarkParse).parse(markdown);
const docxBlob = await toDocx(
ast,
{},
{
plugins: [
codeLowlightPlugin({
theme: "github-dark",
lineNumbers: true,
}),
],
},
"blob",
);⚙️ Plugin Options
| Option | Type | Default | Description |
|---|---|---|---|
theme | PresetThemeName | ICodeTheme | "github-light" | Active theme ("github-light", "github-dark", "monokai", "executive-print", or custom theme). |
lineNumbers | boolean | false | When true, renders multi-line code blocks as 2-column tables with line numbers. |
lowlight | LowlightInstance | createLowlight(common) | Custom lowlight instance with registered grammars. |
highlightAuto | boolean | true | Fallback to automatic language detection when the language tag is unregistered. |
fontFamily | string | "Consolas" | Monospace font family for Word code runs. |
fontSize | number | 19 | Half-points font size (19 = 9.5pt). |
diffHighlight | boolean | true | Highlights unified diff additions (+) and deletions (-). |
License
This library is licensed under the MIT open-source license.
with 💖 by Mayank Kumar Chaudhari