Legal Markdown JS can generate native Word documents (.docx) using the same
3-phase processing pipeline as Markdown/HTML/PDF.
- Native OOXML output compatible with Microsoft Word and LibreOffice.
- Optional reviewer variant:
.HIGHLIGHT.docx. - CSS-to-DOCX style adaptation from
default.cssandhighlight.css. - Header/footer generation (logo/version/page numbers by default).
- Programmatic custom header/footer HTML templates (including images).
--docxgenerates<base>.docx.--docx --highlightgenerates both<base>.docxand<base>.HIGHLIGHT.docx.
In interactive CLI (legal-md-ui), naming follows the same logic as PDF/HTML.
# Basic DOCX
legal-md input.md --docx
# DOCX + highlighted reviewer copy
legal-md input.md --docx --highlight
# DOCX with custom stylesheet and title
legal-md input.md --docx --css ./styles/legal.css --title "Service Agreement"
# stdin -> DOCX file (binary output must go to file)
cat input.md | legal-md --stdin --docx ./output/service-agreement.docxImportant CLI constraints:
--docx --stdoutis rejected.--stdin --docx --stdoutis rejected.
DOCX is binary output and must be written to a file path.
DOCX can be activated from frontmatter:
---
title: Master Services Agreement
force_commands: --docx --highlight --css styles/legal.css --title "{{title}}"
---import { generateDocx, generateDocxVersions } from 'legal-markdown-js';
await generateDocx(content, './output/contract.docx', {
title: 'Contract',
cssPath: './styles/legal.css',
includeHighlighting: false,
format: 'A4',
landscape: false,
});
await generateDocxVersions(content, './output/contract.docx', {
title: 'Contract',
cssPath: './styles/legal.css',
});
// Generates contract.docx + contract.HIGHLIGHT.docx| Option | Type | Description |
|---|---|---|
title |
string |
Document title metadata |
cssPath |
string |
Base CSS used for DOCX style adaptation |
highlightCssPath |
string |
CSS used for highlight styles when enabled |
includeHighlighting |
boolean |
Enables highlight-style mapping |
format |
'A4' | 'Letter' | 'Legal' |
Page size |
landscape |
boolean |
Landscape orientation |
margin |
{ top,right,bottom,left } |
Page margins |
basePath |
string |
Base path for resolving relative images |
headerTemplate |
string |
Custom header HTML template |
footerTemplate |
string |
Custom footer HTML template |
Custom header/footer templates are currently available through the API
(generateDocx) and internal pipeline options, not CLI flags.
await generateDocx(content, './output/contract.docx', {
title: 'Contract',
basePath: './output',
headerTemplate:
'<p class="text-center">ACME <img src="./logo.png" width="18" height="18" /></p>',
footerTemplate: '<p class="text-center text-muted">Confidential</p>',
});Notes:
- Template HTML is converted with the same DOCX HTML converter as body content.
- Relative image paths in templates are resolved with
basePath. - If
headerTemplate/footerTemplateis provided, it replaces default auto-header/auto-footer behavior.
DOCX does not execute CSS at render time. Instead, Legal Markdown adapts CSS tokens into DOCX styles and numbering definitions.
- CSS selectors are parsed and mapped to paragraph/run/table styles.
- CSS variables (for example
var(--color-primary)) are resolved. @media,:hover, and other browser-only constructs are ignored.!importantis tolerated in mapped color values.- Field highlight runs use native Word formatting:
font color + highlight(no run borders/shading).
Important architecture detail:
- No static
.stylesfile is generated. - Styles are adapted from CSS at export time and emitted into DOCX XML
(
word/styles.xml,word/numbering.xml).
- Body text, headings (
h1..h6), blockquotes, code blocks, inline code. - Tables with border/fill/text styling from CSS tokens.
- Field-review classes in highlight mode:
.imported-value,.missing-value,.highlight.
The following classes are recognized by DOCX generation:
| Class | DOCX behavior |
|---|---|
.text-center |
Paragraph centered (w:jc center) |
.text-muted |
Run color set to muted token |
.confidential |
Paragraph style confidentialBanner |
.separator |
Paragraph style separatorBanner |
.algorithm |
Paragraph style algorithmBlock |
.table-of-contents |
TOC container style and list marker suppression |
.signatures (table) |
Signature-table layout/borders adapted for DOCX |
.page-break |
Page break before |
.page-break-before |
Page break before |
.page-break-after |
Page break after |
DOCX export maps list-style-type (global CSS and inline styles) to DOCX
numbering/bullets.
Ordered list mappings:
decimallower-alpha/lower-latinupper-alpha/upper-latinlower-romanupper-roman
Unordered list mappings:
disccirclesquaredash
Special case:
list-style-type: nonesuppresses list markers (used for TOC-like layouts).
Highlight palette note:
- Word highlight uses a fixed palette. CSS highlight backgrounds are mapped to the nearest available Word highlight color.
DOCX options supported:
format:A4|Letter|Legallandscape:true|falsemargin:{ top, right, bottom, left }with unit parsing (cm,mm,in,pt,px, twips fallback)
When custom templates are not provided:
- Header uses logo if
--logo-filenameis declared in CSS. - Footer includes
v<version>when metadata hasversion. - Footer includes
Pg: current / total.
Logo behavior:
- PNG required.
- Loaded from configured images directory (or URL when valid).
- If loading/validation fails, generation continues with empty header.
- Interactive/browser states:
:hover, scripting behavior. - Responsive layout semantics from media queries.
- Pixel-perfect browser layout parity.
DOCX export aims for semantic and visual equivalence, not exact browser rendering.
- Unexpected style result:
- Validate selector is directly mappable (class/tag, not interactive state).
- Prefer explicit CSS tokens over heavy cascading.
- Lists not matching expected marker:
- Check
list-style-typeonul/olor inline style. - Ensure it is one of the supported values above.
- Header/footer images not rendering:
- Verify
basePathfor relative paths in templates. - Verify the image file exists and is readable.