mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* build(deps): add fflate, to zip and unzip packages in place of JSZip Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * perf: hash with docx's own SHA-1 and SHA-256, in place of hash.js hash.js was about 6.5 KB of a browser bundle, minified and gzipped, as its whole library is bundled for the two hashes docx uses: SHA-1 for the names of media files (`hashedId`) and SHA-256 for the password that protects a document. It hasn't had a release since 2018, and brought `inherits` and `minimalistic-assert` with it. The platforms' own hashes can't replace it, as browsers only hash asynchronously and documents are written synchronously. The hashes are the same: they match FIPS 180-4's examples and node:crypto, and the password hash Word accepts is unchanged. They are also faster: hashing a 5 MB image takes about half the time, and a password's 100,000 rounds about 130 ms rather than 155 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * perf: zip and unzip with fflate, in place of JSZip JSZip was about 30 KB of a browser bundle, minified and gzipped, as its prebuilt browser file brings its own copy of pako and more. fflate's zipSync and unzipSync, the only parts docx needs, are about 6 KB. Together with the hashes, the bundle of a page that packs a document goes from 123 KB to 94 KB. Nothing changes for users: - Packer and patchDocument give each output type as JSZip gave it, including "string" and "binarystring" as a character for each byte, a Blob with the .docx or .docm MIME type, and the same errors for "nodebuffer" without Buffer, a Blob without Blob, and "text", which JSZip never gave. - patchDocument and patchDetector still take a JSZip instance, which is read through its generateAsync, so the types no longer import jszip. They also still take the other forms JSZip read: a string with a character for each byte, an array of bytes, a Blob, an ArrayBuffer and bytes from another realm. - A part's text keeps a byte order mark at its start, as it did. - Packer.toStream still compiles synchronously, so it throws as before, and gives the document a turn of the event loop later. Zipping is now synchronous, and faster: packing a document with 5,000 paragraphs and 5 MB of images takes 83 ms rather than 182 ms. The zip files no longer have folder entries, which JSZip added for each folder. Word doesn't write them, and nothing reads them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: test the packer and the patcher's zip reading without JSZip These specs mocked JSZip or read the JSZip instance the compiler returned. They now build real zip files and read what the Packer and the patcher give, so they test the zipping too: - loadZip reads every form a template can be given in, a JSZip instance through its generateAsync, and leaves out folders - Packer.toStream throws when the document can't be written, and gives it even to a listener added after an await - toBlob, toArrayBuffer and toStream give real zip files, rather than what a mocked compiler gave The counts of the files in a package no longer include the folder entries JSZip added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: test zipping, unzipping and each output type of a zip file Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * chore: read and write .docx files in the scripts with fflate, in place of JSZip docx is replacing JSZip with fflate (#3834), and jszip is leaving package.json, so the scripts that read documents (compare-layout.ts, the layout probes' line checkers) and the probes that edit docx's output before writing it use fflate's unzipSync and zipSync. They leave out folder entries when they unzip, as Word's own files have none, and zip at level 6. The written documents have the same files in the same order, with the same contents, as before. check-api.ts's comment no longer says the built declarations import jszip. Those of releases up to 9.14.0 still do, and the import, which no longer resolves, is any there, which the check accepts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: load Node's types from docx's declarations, which JSZip's used to load The declarations use Node's types, such as Buffer and NodeJS.ReadableStream. They had them because they imported JSZip's declarations, which reference Node's types. Without JSZip, a project that doesn't load every @types package, such as one with `types` set, or TypeScript 6's default, would find no Buffer in docx's types, and docx's own optional entries didn't build. The reference is kept in the built declarations with `preserve="true"`, as TypeScript otherwise leaves it out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: read and make zip files with fflate in the specs of charts, embedded files, macros, shapes and the core, in place of JSZip jszip is leaving docx's dependencies, so these specs read packed documents and make changed templates with the helpers in tests/zip, which unzip and zip with fflate, as docx now does. Each test checks what it checked before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: read and make zip files with fflate in the specs of docx/html, docx/layout and the patcher, in place of JSZip jszip is leaving docx's dependencies, as docx now zips and unzips with fflate, so these specs read packed documents and make templates with the helpers in src/tests/zip.ts. Templates that were JSZip instances are now zip files' bytes, and the specs read text and bytes from the unzipped files directly, so some helpers that only awaited JSZip are no longer asynchronous. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: patch real zip files in patchDocument's spec, rather than mocking JSZip JSZip is leaving the dependencies. The spec mocked JSZip.loadAsync and passed an empty Buffer as the template, so patchDocument never unzipped anything. Each template is now a real zip file, made with the specs' zip helper, so the tests exercise the unzipping too. The tests of a template opened with JSZip pass an object with generateAsync, which is how a JSZip instance is read now. The path keys of the templates are covered by one file-level naming-convention exception, in place of the comments on each patch key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * build(deps): remove jszip, as nothing uses it now Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: refuse a zip file's entry of the wrong size before taking memory for it, and inflate only what patchDetector reads fflate takes room for the size a zip file gives an entry before inflating it, so a template of a few hundred bytes could make docx take 4 GB, which fails in a browser or a process with a memory limit. JSZip refused such an entry ("uncompressed data size mismatch"). An entry whose given size is more than DEFLATE can inflate its data to (1032 times) is now refused before fflate takes the room, and one that inflates to less than its given size after. JSZip also inflated only the entries that were read, so patchDetector, which reads only the XML parts in word/, never inflated a template's images or embedded files. unzipFiles and loadZip now take the paths to read, and patchDetector gives them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: refuse a zip file whose entries claim more compressed data than the zip file holds, before taking memory for them The size an entry inflates to was checked against its compressed size, but that is also read from the zip file, so an entry could claim compressed data larger than the zip file and pass. The entries docx reads now have their compressed sizes added up, and a total larger than the zip file is refused before fflate takes room for the entry. Together with DEFLATE's ratio, that keeps the room taken to 1032 times the zip file's size, as a real zip bomb's is. The tests damage the entry's data too, so only a refusal before inflating gives their error: without either check, each fails with fflate's own error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
618 lines
22 KiB
TypeScript
618 lines
22 KiB
TypeScript
/**
|
|
* The shared kit of the lay-stops2 probe documents: what each document is laid out with, and the helpers that write its
|
|
* probes. Each probe starts a page of its own, or is marked off by a line above and below it, and each line of text names
|
|
* its probe, such as "TV3a cell 1", so word-stops.py finds it in a PDF Word saves of the document.
|
|
*
|
|
* The documents are A4 with margins of an inch (1440 twips), so the text is 9026 twips wide and 13958 tall, in Calibri 11
|
|
* at single spacing with no space before or after: 51 lines of 268.55 twips to a page, as in round 8's probes.
|
|
*
|
|
* What docx can't write, such as `w:tcFitText` or a hidden paragraph mark, is written as a marker and replaced in the
|
|
* package's XML (`inject`), as `word-watertight-text.ts` does.
|
|
*/
|
|
// cspell:ignore bbox
|
|
import { mkdirSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
|
|
|
|
import {
|
|
AlignmentType,
|
|
BorderStyle,
|
|
Document,
|
|
ImageRun,
|
|
FootnoteReferenceRun,
|
|
type IParagraphOptions,
|
|
type ISectionOptions,
|
|
type ITableCellOptions,
|
|
type ITableOptions,
|
|
type IRunOptions,
|
|
Packer,
|
|
Paragraph,
|
|
type ParagraphChild,
|
|
Table,
|
|
TableCell,
|
|
TableRow,
|
|
TextRun,
|
|
WidthType,
|
|
} from "docx";
|
|
|
|
export const PAGE_WIDTH = 11906;
|
|
export const PAGE_HEIGHT = 16838;
|
|
export const MARGIN = 1440;
|
|
export const TEXT_WIDTH = PAGE_WIDTH - 2 * MARGIN;
|
|
|
|
export type Child = Paragraph | Table;
|
|
export type Options = Omit<IParagraphOptions, "children" | "text">;
|
|
|
|
/** A one-line paragraph of text, in the paragraph's font */
|
|
export const line = (text: string, options: Options = {}, run: Omit<IRunOptions, "text"> = {}): Paragraph =>
|
|
new Paragraph({ ...options, children: [new TextRun({ ...run, text })] });
|
|
|
|
/** Lines of one paragraph each: "<probe> fill 1" and on */
|
|
export const fill = (probe: string, count: number, from = 1, options: Options = {}): Paragraph[] =>
|
|
Array.from({ length: count }, (_, index) => line(`${probe} fill ${from + index}`, options));
|
|
|
|
/** One paragraph of lines split by line breaks, each "<probe> <word> <n>" */
|
|
export const lines = (probe: string, count: number, options: Options = {}, word = "line", run: Omit<IRunOptions, "text"> = {}): Paragraph =>
|
|
new Paragraph({
|
|
...options,
|
|
children: Array.from(
|
|
{ length: count },
|
|
(_, index) => new TextRun({ ...run, text: `${probe} ${word} ${index + 1}`, ...(index > 0 ? { break: 1 } : {}) }),
|
|
),
|
|
});
|
|
|
|
const WORDS = "the in foot mouth made on river was and the coast boat to the by lighthouse of summer the survey the from".split(" ");
|
|
/** Prose of a number of words, the same words in the same order wherever it is used, so a line can be told by its text */
|
|
export const prose = (count: number, from = 0): string =>
|
|
Array.from({ length: count }, (_, index) => WORDS[(from + index) % WORDS.length]).join(" ");
|
|
|
|
/** A paragraph of prose, starting with its probe's name */
|
|
export const para = (probe: string, words: number, options: Options = {}, run: Omit<IRunOptions, "text"> = {}): Paragraph =>
|
|
line(`${probe} ${prose(words)}`, options, run);
|
|
|
|
/** A probe: its name, the line above it, what it has, and the line below it, on a page of its own */
|
|
export const probe = (name: string, children: readonly Child[], { below = true }: { readonly below?: boolean } = {}): Child[] =>
|
|
// ONLY, in the environment, keeps the probes whose names start with it, to lay one out alone
|
|
process.env.ONLY && !process.env.ONLY.split(",").some((only) => name.startsWith(only))
|
|
? []
|
|
: [line(`${name} above`, { pageBreakBefore: true }), ...children, ...(below ? [line(`${name} below`)] : [])];
|
|
|
|
/** A probe on the page where the one before ends, between a line above and a line below it */
|
|
export const group = (name: string, children: readonly Child[]): Child[] =>
|
|
process.env.ONLY && !process.env.ONLY.split(",").some((only) => name.startsWith(only))
|
|
? []
|
|
: [line(`${name} above`), ...children, line(`${name} below`)];
|
|
|
|
/** A page break before the next probe, as a paragraph of its own that names no probe */
|
|
export const newPage = (): Paragraph => new Paragraph({ pageBreakBefore: true, children: [] });
|
|
|
|
let footnoteCount = 0;
|
|
const footnoteBodies: Record<number, { children: Paragraph[] }> = {};
|
|
/** A footnote reference, with the footnote's paragraphs or tables */
|
|
export const footnote = (...children: Child[]): FootnoteReferenceRun => {
|
|
footnoteCount++;
|
|
// A footnote can have a table in it, though the option's type only names paragraphs
|
|
footnoteBodies[footnoteCount] = { children: children as Paragraph[] };
|
|
return new FootnoteReferenceRun(footnoteCount);
|
|
};
|
|
/** A paragraph of text with a footnote reference after it */
|
|
export const withNote = (text: string, note: readonly Child[], options: Options = {}): Paragraph =>
|
|
new Paragraph({ ...options, children: [new TextRun(text), footnote(...note)] });
|
|
|
|
export const SINGLE = { style: BorderStyle.SINGLE, size: 4, color: "000000" } as const;
|
|
export const ALL_BORDERS = {
|
|
top: SINGLE,
|
|
bottom: SINGLE,
|
|
left: SINGLE,
|
|
right: SINGLE,
|
|
insideHorizontal: SINGLE,
|
|
insideVertical: SINGLE,
|
|
} as const;
|
|
|
|
/** A cell of one or more paragraphs, each a line of text, or the children given */
|
|
export const cell = (content: string | readonly Child[], options: Omit<ITableCellOptions, "children"> = {}): TableCell =>
|
|
new TableCell({
|
|
...options,
|
|
children: typeof content === "string" ? content.split("\n").map((text) => line(text)) : [...content],
|
|
});
|
|
|
|
/** A table of rows of cells, with single borders all round, as docx's demos have them, unless the options say otherwise */
|
|
export const table = (rows: readonly (readonly (string | TableCell)[])[], options: Omit<ITableOptions, "rows"> = {}): Table =>
|
|
new Table({
|
|
borders: ALL_BORDERS,
|
|
...options,
|
|
rows: rows.map(
|
|
(cells) => new TableRow({ children: cells.map((content) => (typeof content === "string" ? cell(content) : content)) }),
|
|
),
|
|
});
|
|
|
|
/** A table whose columns are given widths in twips, and its cells the same */
|
|
export const fixedTable = (
|
|
widths: readonly number[],
|
|
rows: readonly (readonly string[])[],
|
|
options: Omit<ITableOptions, "rows"> = {},
|
|
): Table =>
|
|
new Table({
|
|
borders: ALL_BORDERS,
|
|
columnWidths: [...widths],
|
|
width: { size: widths.reduce((a, b) => a + b, 0), type: WidthType.DXA },
|
|
...options,
|
|
rows: rows.map(
|
|
(cells) =>
|
|
new TableRow({ children: cells.map((text, index) => cell(text, { width: { size: widths[index], type: WidthType.DXA } })) }),
|
|
),
|
|
});
|
|
|
|
// A grey square PNG, drawn at the size given
|
|
export const PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQImWNoaGgAAAMEAYEezv+mAAAAAElFTkSuQmCC", "base64");
|
|
/** A picture in the line, `width` by `height` points */
|
|
export const picture = (width: number, height = width): ImageRun =>
|
|
new ImageRun({ type: "png", data: PNG, transformation: { width: (width * 96) / 72, height: (height * 96) / 72 } });
|
|
|
|
/** A marker run, replaced in the XML by an injection of the same name */
|
|
export const marker = (name: string): TextRun => new TextRun(`@@${name}@@`);
|
|
/** A paragraph with a marker run in it, among other runs */
|
|
export const marked = (name: string, before: string, after = "", options: Options = {}): Paragraph =>
|
|
new Paragraph({ ...options, children: [new TextRun(before), marker(name), ...(after ? [new TextRun(after)] : [])] });
|
|
|
|
/**
|
|
* What to do to a part of the package once docx has written it: a function of its XML, or a part to add. Markers are
|
|
* replaced with `replaceMarkerRun`, `replaceMarkerParagraphProperties` and the like
|
|
*/
|
|
export type Injection = (parts: Map<string, string>) => void;
|
|
|
|
/** Replaces the run docx wrote for a marker with XML of its own */
|
|
export const replaceMarkerRun =
|
|
(name: string, xml: string, part = "word/document.xml"): Injection =>
|
|
(parts) => {
|
|
const text = parts.get(part)!;
|
|
const pattern = new RegExp(`<w:r>(<w:rPr>(?:(?!</w:rPr>).)*</w:rPr>)?<w:t xml:space="preserve">@@${name}@@</w:t></w:r>`, "g");
|
|
if (!pattern.test(text)) {
|
|
throw new Error(`No marker ${name} in ${part}`);
|
|
}
|
|
parts.set(part, text.replace(pattern, xml));
|
|
};
|
|
|
|
/**
|
|
* Adds XML to the properties of the paragraph (or run of the paragraph's mark) the marker is in, and takes the marker
|
|
* out: `pPr` is put at the end of the paragraph's properties, before its mark's run properties and section properties
|
|
*/
|
|
export const injectIntoParagraph =
|
|
(
|
|
name: string,
|
|
{ pPr = "", rPr = "", before = "", after = "" }: { pPr?: string; rPr?: string; before?: string; after?: string },
|
|
part = "word/document.xml",
|
|
): Injection =>
|
|
(parts) => {
|
|
const text = parts.get(part)!;
|
|
const at = text.indexOf(`@@${name}@@`);
|
|
if (at < 0) {
|
|
throw new Error(`No marker ${name} in ${part}`);
|
|
}
|
|
const start = Math.max(text.lastIndexOf("<w:p>", at), text.lastIndexOf("<w:p ", at));
|
|
const end = text.indexOf("</w:p>", at) + "</w:p>".length;
|
|
let paragraph = text.slice(start, end);
|
|
// The marker's run goes
|
|
paragraph = paragraph.replace(
|
|
new RegExp(`<w:r>(<w:rPr>(?:(?!</w:rPr>).)*</w:rPr>)?<w:t xml:space="preserve">@@${name}@@</w:t></w:r>`),
|
|
"",
|
|
);
|
|
const open = paragraph.indexOf(">") + 1;
|
|
for (const child of childrenIn(pPr)) {
|
|
paragraph = withProperty(paragraph, "w:pPr", child, open);
|
|
}
|
|
if (rPr) {
|
|
const propertiesEnd = paragraph.indexOf("</w:pPr>");
|
|
const markProps = paragraph.indexOf("<w:rPr>");
|
|
paragraph =
|
|
propertiesEnd >= 0 && markProps >= 0 && markProps < propertiesEnd
|
|
? paragraph.replace("<w:rPr>", `<w:rPr>${rPr}`)
|
|
: withProperty(paragraph, "w:pPr", `<w:rPr>${rPr}</w:rPr>`, open);
|
|
}
|
|
parts.set(part, text.slice(0, start) + before + paragraph + after + text.slice(end));
|
|
};
|
|
|
|
/** The order of the children of the properties elements, as their schema (wml.xsd) has them */
|
|
export const ORDER: Readonly<Record<string, readonly string[]>> = {
|
|
"w:pPr": [
|
|
"pStyle",
|
|
"keepNext",
|
|
"keepLines",
|
|
"pageBreakBefore",
|
|
"framePr",
|
|
"widowControl",
|
|
"numPr",
|
|
"suppressLineNumbers",
|
|
"pBdr",
|
|
"shd",
|
|
"tabs",
|
|
"suppressAutoHyphens",
|
|
"kinsoku",
|
|
"wordWrap",
|
|
"overflowPunct",
|
|
"topLinePunct",
|
|
"autoSpaceDE",
|
|
"autoSpaceDN",
|
|
"bidi",
|
|
"adjustRightInd",
|
|
"snapToGrid",
|
|
"spacing",
|
|
"ind",
|
|
"contextualSpacing",
|
|
"mirrorIndents",
|
|
"suppressOverlap",
|
|
"jc",
|
|
"textDirection",
|
|
"textAlignment",
|
|
"textboxTightWrap",
|
|
"outlineLvl",
|
|
"divId",
|
|
"cnfStyle",
|
|
"rPr",
|
|
"sectPr",
|
|
"pPrChange",
|
|
],
|
|
"w:tcPr": [
|
|
"cnfStyle",
|
|
"tcW",
|
|
"gridSpan",
|
|
"hMerge",
|
|
"vMerge",
|
|
"tcBorders",
|
|
"shd",
|
|
"noWrap",
|
|
"tcMar",
|
|
"textDirection",
|
|
"tcFitText",
|
|
"vAlign",
|
|
"hideMark",
|
|
"headers",
|
|
"cellIns",
|
|
"cellDel",
|
|
"cellMerge",
|
|
"tcPrChange",
|
|
],
|
|
"w:tblPr": [
|
|
"tblStyle",
|
|
"tblpPr",
|
|
"tblOverlap",
|
|
"bidiVisual",
|
|
"tblStyleRowBandSize",
|
|
"tblStyleColBandSize",
|
|
"tblW",
|
|
"jc",
|
|
"tblCellSpacing",
|
|
"tblInd",
|
|
"tblBorders",
|
|
"shd",
|
|
"tblLayout",
|
|
"tblCellMar",
|
|
"tblLook",
|
|
"tblCaption",
|
|
"tblDescription",
|
|
"tblPrChange",
|
|
],
|
|
"w:trPr": [
|
|
"cnfStyle",
|
|
"divId",
|
|
"gridBefore",
|
|
"gridAfter",
|
|
"wBefore",
|
|
"wAfter",
|
|
"cantSplit",
|
|
"trHeight",
|
|
"tblHeader",
|
|
"tblCellSpacing",
|
|
"jc",
|
|
"hidden",
|
|
"ins",
|
|
"del",
|
|
"trPrChange",
|
|
],
|
|
"w:settings": [
|
|
"writeProtection",
|
|
"view",
|
|
"zoom",
|
|
"removePersonalInformation",
|
|
"removeDateAndTime",
|
|
"doNotDisplayPageBoundaries",
|
|
"displayBackgroundShape",
|
|
"printPostScriptOverText",
|
|
"printFractionalCharacterWidth",
|
|
"printFormsData",
|
|
"embedTrueTypeFonts",
|
|
"embedSystemFonts",
|
|
"saveSubsetFonts",
|
|
"saveFormsData",
|
|
"mirrorMargins",
|
|
"alignBordersAndEdges",
|
|
"bordersDoNotSurroundHeader",
|
|
"bordersDoNotSurroundFooter",
|
|
"gutterAtTop",
|
|
"hideSpellingErrors",
|
|
"hideGrammaticalErrors",
|
|
"activeWritingStyle",
|
|
"proofState",
|
|
"formsDesign",
|
|
"attachedTemplate",
|
|
"linkStyles",
|
|
"stylePaneFormatFilter",
|
|
"stylePaneSortMethod",
|
|
"documentType",
|
|
"mailMerge",
|
|
"revisionView",
|
|
"trackRevisions",
|
|
"doNotTrackMoves",
|
|
"doNotTrackFormatting",
|
|
"documentProtection",
|
|
"autoFormatOverride",
|
|
"styleLockTheme",
|
|
"styleLockQFSet",
|
|
"defaultTabStop",
|
|
"autoHyphenation",
|
|
"consecutiveHyphenLimit",
|
|
"hyphenationZone",
|
|
"doNotHyphenateCaps",
|
|
"showEnvelope",
|
|
"summaryLength",
|
|
"clickAndTypeStyle",
|
|
"defaultTableStyle",
|
|
"evenAndOddHeaders",
|
|
"bookFoldRevPrinting",
|
|
"bookFoldPrinting",
|
|
"bookFoldPrintingSheets",
|
|
"drawingGridHorizontalSpacing",
|
|
"drawingGridVerticalSpacing",
|
|
"displayHorizontalDrawingGridEvery",
|
|
"displayVerticalDrawingGridEvery",
|
|
"doNotUseMarginsForDrawingGridOrigin",
|
|
"drawingGridHorizontalOrigin",
|
|
"drawingGridVerticalOrigin",
|
|
"doNotShadeFormData",
|
|
"noPunctuationKerning",
|
|
"characterSpacingControl",
|
|
"printTwoOnOne",
|
|
"strictFirstAndLastChars",
|
|
"noLineBreaksAfter",
|
|
"noLineBreaksBefore",
|
|
"savePreviewPicture",
|
|
"doNotValidateAgainstSchema",
|
|
"saveInvalidXml",
|
|
"ignoreMixedContent",
|
|
"alwaysShowPlaceholderText",
|
|
"doNotDemarcateInvalidXml",
|
|
"saveXmlDataOnly",
|
|
"useXSLTWhenSaving",
|
|
"saveThroughXslt",
|
|
"showXMLTags",
|
|
"alwaysMergeEmptyNamespace",
|
|
"updateFields",
|
|
"hdrShapeDefaults",
|
|
"footnotePr",
|
|
"endnotePr",
|
|
"compat",
|
|
"docVars",
|
|
"rsids",
|
|
"mathPr",
|
|
"attachedSchema",
|
|
"themeFontLang",
|
|
"clrSchemeMapping",
|
|
"doNotIncludeSubdocsInStats",
|
|
"doNotAutoCompressPictures",
|
|
"forceUpgrade",
|
|
"captions",
|
|
"readModeInkLockDown",
|
|
"smartTagType",
|
|
"schemaLibrary",
|
|
"shapeDefaults",
|
|
"doNotEmbedSmartTags",
|
|
"decimalSymbol",
|
|
"listSeparator",
|
|
],
|
|
"w:sectPr": [
|
|
"headerReference",
|
|
"footerReference",
|
|
"footnotePr",
|
|
"endnotePr",
|
|
"type",
|
|
"pgSz",
|
|
"pgMar",
|
|
"paperSrc",
|
|
"pgBorders",
|
|
"lnNumType",
|
|
"pgNumType",
|
|
"cols",
|
|
"formProt",
|
|
"vAlign",
|
|
"noEndnote",
|
|
"titlePg",
|
|
"textDirection",
|
|
"bidi",
|
|
"rtlGutter",
|
|
"docGrid",
|
|
"printerSettings",
|
|
"sectPrChange",
|
|
],
|
|
};
|
|
|
|
/** The local name of the element an XML string starts with, such as "tcW" for "<w:tcW .../>" */
|
|
const nameAt = (xml: string): string => /^<\w+:(\w+)/.exec(xml)?.[1] ?? "";
|
|
|
|
/** The top-level children of an element's content, as strings */
|
|
export const childrenIn = (content: string): string[] => {
|
|
const children: string[] = [];
|
|
let at = 0;
|
|
while (at < content.length) {
|
|
const start = content.indexOf("<", at);
|
|
if (start < 0) {
|
|
break;
|
|
}
|
|
const name = /^<([\w:]+)/.exec(content.slice(start))![1];
|
|
const close = content.indexOf(">", start);
|
|
if (content[close - 1] === "/") {
|
|
children.push(content.slice(start, close + 1));
|
|
at = close + 1;
|
|
continue;
|
|
}
|
|
// The matching end, counting elements of the same name inside
|
|
let depth = 1;
|
|
let cursor = close + 1;
|
|
const tags = new RegExp(`<${name}[ >/]|</${name}>`, "g");
|
|
tags.lastIndex = cursor;
|
|
for (let match = tags.exec(content); match; match = tags.exec(content)) {
|
|
if (match[0].startsWith("</")) {
|
|
depth--;
|
|
} else if (!content.slice(match.index, content.indexOf(">", match.index) + 1).endsWith("/>")) {
|
|
depth++;
|
|
}
|
|
if (depth === 0) {
|
|
cursor = match.index + match[0].length;
|
|
break;
|
|
}
|
|
}
|
|
children.push(content.slice(start, cursor));
|
|
at = cursor;
|
|
}
|
|
return children;
|
|
};
|
|
|
|
/**
|
|
* Puts a child into the properties element (such as `w:tcPr`) that starts at or after `from` in the XML, before `before`,
|
|
* in the place its schema gives it, in place of one of the same name; or makes the element at `from` when there is none.
|
|
* Returns the new XML
|
|
*/
|
|
export const withProperty = (xml: string, element: string, child: string, from: number, before = xml.length): string => {
|
|
const opening = new RegExp(`<${element}[ >]`, "g");
|
|
opening.lastIndex = from;
|
|
const start = opening.exec(xml)?.index ?? -1;
|
|
const order = ORDER[element];
|
|
if (start < 0 || start >= before) {
|
|
return `${xml.slice(0, from)}<${element}>${child}</${element}>${xml.slice(from)}`;
|
|
}
|
|
const contentStart = xml.indexOf(">", start) + 1;
|
|
const end = xml.indexOf(`</${element}>`, start);
|
|
const children = childrenIn(xml.slice(contentStart, end)).filter((one) => nameAt(one) !== nameAt(child));
|
|
const rank = (one: string): number => order.indexOf(nameAt(one));
|
|
const index = children.findIndex((one) => rank(one) > rank(child));
|
|
children.splice(index < 0 ? children.length : index, 0, child);
|
|
return `${xml.slice(0, contentStart)}${children.join("")}${xml.slice(end)}`;
|
|
};
|
|
|
|
/** Puts children into the settings, each in the place its schema gives it */
|
|
export const settings =
|
|
(...children: readonly string[]): Injection =>
|
|
(parts) => {
|
|
let text = parts.get("word/settings.xml")!;
|
|
for (const child of children) {
|
|
text = withProperty(text, "w:settings", child, 0);
|
|
}
|
|
parts.set("word/settings.xml", text);
|
|
};
|
|
|
|
/** Replaces text in a part */
|
|
export const replaceText =
|
|
(from: string | RegExp, to: string, part = "word/document.xml"): Injection =>
|
|
(parts) => {
|
|
const text = parts.get(part)!;
|
|
if (typeof from === "string" ? !text.includes(from) : !from.test(text)) {
|
|
throw new Error(`Nothing to replace for ${String(from)} in ${part}`);
|
|
}
|
|
parts.set(part, typeof from === "string" ? text.split(from).join(to) : text.replace(from, to));
|
|
};
|
|
|
|
/** Writes each U+00AD in the text as a soft hyphen (`w:softHyphen`), which Word breaks at, where it draws U+00AD as a hyphen */
|
|
export const softHyphens =
|
|
(part = "word/document.xml"): Injection =>
|
|
(parts) => {
|
|
const text = parts.get(part)!;
|
|
parts.set(
|
|
part,
|
|
text.replace(/<w:t xml:space="preserve">([^<]*\u00AD[^<]*)<\/w:t>/g, (_, content: string) =>
|
|
content
|
|
.split("\u00AD")
|
|
.map((piece) => `<w:t xml:space="preserve">${piece}</w:t>`)
|
|
.join("<w:softHyphen/>"),
|
|
),
|
|
);
|
|
};
|
|
|
|
export type ProbeDocument = {
|
|
/** Its file name, without .docx */
|
|
readonly name: string;
|
|
/** Its sections, or its probes' children in one section of the kit's page */
|
|
readonly sections?: readonly ISectionOptions[];
|
|
readonly children?: readonly Child[];
|
|
readonly injections?: readonly Injection[];
|
|
/** Options of the document beyond the kit's own */
|
|
readonly options?: Omit<ConstructorParameters<typeof Document>[0], "sections">;
|
|
/** The size of the default font, in half-points */
|
|
readonly size?: number;
|
|
readonly font?: string;
|
|
/** Parts to add to the package as they are, such as a picture an injection refers to */
|
|
readonly files?: Readonly<Record<string, Uint8Array>>;
|
|
};
|
|
|
|
/** The kit's page, for a section */
|
|
export const PAGE = {
|
|
page: {
|
|
size: { width: PAGE_WIDTH, height: PAGE_HEIGHT },
|
|
margin: { top: MARGIN, bottom: MARGIN, left: MARGIN, right: MARGIN, header: 708, footer: 708 },
|
|
},
|
|
} as const;
|
|
|
|
/** Writes a probe document into the folder given as the script's first argument, or build/word-stops */
|
|
export const write = async ({
|
|
name,
|
|
sections,
|
|
children,
|
|
injections = [],
|
|
options = {},
|
|
size = 22,
|
|
font = "Calibri",
|
|
files = {},
|
|
}: ProbeDocument): Promise<string> => {
|
|
const doc = new Document({
|
|
...options,
|
|
styles: {
|
|
...(options.styles ?? {}),
|
|
default: {
|
|
document: { run: { font, size }, paragraph: { spacing: { before: 0, after: 0, line: 240 } } },
|
|
...(options.styles?.default ?? {}),
|
|
},
|
|
},
|
|
footnotes: { ...footnoteBodies, ...((options as { footnotes?: object }).footnotes ?? {}) },
|
|
sections: sections ? [...sections] : [{ properties: PAGE, children: [...(children ?? [])] }],
|
|
});
|
|
const zip: Record<string, Uint8Array> = unzipSync(await Packer.toBuffer(doc), { filter: ({ name }) => !name.endsWith("/") });
|
|
const parts = new Map<string, string>();
|
|
for (const path of Object.keys(zip).filter((file) => /\.(xml|rels)$/.test(file))) {
|
|
parts.set(path, strFromU8(zip[path]));
|
|
}
|
|
for (const injection of injections) {
|
|
try {
|
|
injection(parts);
|
|
} catch (error) {
|
|
// A probe left out with ONLY leaves its markers out too
|
|
if (!process.env.ONLY) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
for (const [path, text] of parts) {
|
|
zip[path] = strToU8(text);
|
|
}
|
|
for (const [path, data] of Object.entries(files)) {
|
|
zip[path] = data;
|
|
}
|
|
const folder = process.argv[2] ?? "build/word-stops";
|
|
mkdirSync(folder, { recursive: true });
|
|
const out = join(folder, `${name}.docx`);
|
|
writeFileSync(out, zipSync(zip, { level: 6 }));
|
|
console.log(`wrote ${out}`);
|
|
return out;
|
|
};
|
|
|
|
export { AlignmentType, BorderStyle, Paragraph, Table, TableCell, TableRow, TextRun, WidthType };
|
|
export type { ParagraphChild };
|