// Zero-dependency OOXML .docx writer with rich inline styles, tables, code blocks, quotes & links.
import { Buffer } from "node:buffer";
import type { Inline, MdBlock } from "./markdown.js";
import { escapeXml as esc, zip } from "./zip.js";
const W = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
const R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
const REL = "http://schemas.openxmlformats.org/package/2006/relationships";
const XML_HEAD = '\n';
export interface DocxOptions {
title?: string;
subtitle?: string;
pageSize?: "letter" | "a4";
}
function renderRun(text: string, rPr = ""): string {
return text
.split("\n")
.map((line) => `${rPr}${esc(line)}`)
.join("");
}
export function renderDocx(blocks: MdBlock[], opts: DocxOptions = {}): Buffer {
const body: string[] = [];
const hyperlinks: string[] = [];
let numberedLists = 0;
const renderInlines = (runs: Inline[], baseRPr = ""): string =>
runs
.map((run) => {
switch (run.type) {
case "bold":
return renderRun(run.text, `${baseRPr}`);
case "italic":
return renderRun(run.text, `${baseRPr}`);
case "code":
return renderRun(
run.text,
'',
);
case "link": {
const rId = `rIdLink${hyperlinks.length + 1}`;
hyperlinks.push(
``,
);
return `${renderRun(run.text, '')}`;
}
default:
return renderRun(run.text, baseRPr ? `${baseRPr}` : "");
}
})
.join("");
if (opts.title) {
body.push(`${renderRun(opts.title)}`);
}
if (opts.subtitle) {
body.push(
`${renderRun(opts.subtitle, '')}`,
);
}
for (const b of blocks) {
switch (b.type) {
case "heading": {
const level = Math.min(b.level, 3);
body.push(
`${renderInlines(b.runs)}`,
);
break;
}
case "paragraph":
body.push(`${renderInlines(b.runs)}`);
break;
case "quote":
body.push(
`${renderInlines(b.runs, '')}`,
);
break;
case "code": {
const codeText = b.code || " ";
body.push(
`${renderRun(codeText, '')}`,
);
break;
}
case "hr":
body.push(
'',
);
break;
case "bullets":
for (const item of b.items) {
body.push(
`${renderInlines(item.runs)}`,
);
}
break;
case "numbered": {
numberedLists++;
const numId = 1 + numberedLists;
for (const item of b.items) {
body.push(
`${renderInlines(item.runs)}`,
);
}
break;
}
case "table": {
const cell = (text: string, isHeader: boolean) =>
`${isHeader ? '' : ""}${renderRun(text, isHeader ? "" : "")}`;
const row = (cells: string[], isHeader: boolean) =>
`${cells.map((c) => cell(c ?? "", isHeader)).join("")}`;
body.push(
`${row(b.header, true)}${b.rows.map((r) => row(r, false)).join("")}`,
);
break;
}
}
}
const pgSz =
opts.pageSize === "a4"
? ''
: '';
const document = `${XML_HEAD}${body.join("")}${pgSz}`;
const heading = (id: number, size: number) =>
``;
const border = (side: string) =>
``;
const styles = `${XML_HEAD}${heading(1, 32)}${heading(2, 26)}${heading(3, 22)}${["top", "left", "bottom", "right", "insideH", "insideV"].map(border).join("")}`;
const level = (fmt: string, text: string) =>
``;
const numbered = Array.from(
{ length: numberedLists },
(_, i) =>
``,
).join("");
const numbering = `${XML_HEAD}${level("bullet", "•")}${level("decimal", "%1.")}${numbered}`;
const documentRels = `${XML_HEAD}${hyperlinks.join("")}`;
const rootRels = `${XML_HEAD}`;
const contentTypes = `${XML_HEAD}`;
return zip([
{ name: "[Content_Types].xml", data: Buffer.from(contentTypes) },
{ name: "_rels/.rels", data: Buffer.from(rootRels) },
{ name: "word/document.xml", data: Buffer.from(document) },
{ name: "word/styles.xml", data: Buffer.from(styles) },
{ name: "word/numbering.xml", data: Buffer.from(numbering) },
{ name: "word/_rels/document.xml.rels", data: Buffer.from(documentRels) },
]);
}