Skip to Content
Md2Docx logo Md2Docx

Overview

Updated 10 Nov 2026

CIcodecovnpm versionnpm downloadsnpm bundle sizelicense

@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, and executive-print with 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 docx

or

$ npm install @m2d/code-lowlight @m2d/core docx

or

$ 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

OptionTypeDefaultDescription
themePresetThemeName | ICodeTheme"github-light"Active theme ("github-light", "github-dark", "monokai", "executive-print", or custom theme).
lineNumbersbooleanfalseWhen true, renders multi-line code blocks as 2-column tables with line numbers.
lowlightLowlightInstancecreateLowlight(common)Custom lowlight instance with registered grammars.
highlightAutobooleantrueFallback to automatic language detection when the language tag is unregistered.
fontFamilystring"Consolas"Monospace font family for Word code runs.
fontSizenumber19Half-points font size (19 = 9.5pt).
diffHighlightbooleantrueHighlights unified diff additions (+) and deletions (-).

License

This library is licensed under the MIT open-source license.


with 💖 by Mayank Kumar Chaudhari

On this page