mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* fix: patch placeholders in links, content controls and tracked changes patchDocument and patchDetector only read runs that are direct children of a paragraph, so a placeholder in a link, a content control, text typed with Track Changes on, a smart tag or a field's result was left as it was, with no error. Runs in those elements are now read and patched where they are. Where the content can't go in what holds the placeholder, it goes where Word can open it: a link is taken out of a tracked insertion (whose piece after it gets an id of its own) or a link, a plain text content control given more than text becomes a rich text one, and a content control showing its placeholder text stops showing it. Addresses #2679 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: cover every branch of patching runs in links and content controls CI's coverage fell below 100%: an empty link, a content control without properties, and runs that stay together when a tracked insertion is split weren't tested. The name fallbacks in the replacer's lookups could never be reached, so the lookups take an element's name as it is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * feat: find placeholders with custom delimiters with patchDetector, and say why a placeholder isn't replaced patchDetector takes placeholderDelimiters, as patchDocument does, so it finds the same placeholders. Before, it only knew {{ and }}, and found nothing in a template with <<name>>. templates.md has a section on a placeholder that isn't replaced: how to find the patches whose placeholder isn't in the template with patchDetector, as patchDocument leaves them out without an error, and the usual reasons, such as a key written with its braces. Addresses #2679 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: unbind a content control a patch fills, so Word doesn't put the placeholder back Word fills a control bound to custom XML or a document property (w:dataBinding) in from its data when it opens a document. A patched placeholder in one came back: Word for Mac showed {{name}} from the XML, though the control held the patch's text. The binding is now removed from a control a patch's content goes in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * perf: split a tracked change around a link in linear time The stretches were built by copying the stretch so far for each element, so a tracked insertion of n runs took n² steps to split. Each stretch is now sliced from where it starts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * docs: list a placeholder only in alt text among the reasons a patch isn't applied patchDetector finds placeholders in pictures' and charts' alt text, so comparing its keys with the patches' doesn't show a text patch for one, which leaves it as it is. It's now one of the listed reasons, saying so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: don't let a start delimiter before a placeholder hide it from patchDetector A key ran from the first start delimiter to the next end delimiter, so in "<<unfinished <<name>>" patchDetector found "unfinished <<name", not "name", which patchDocument patches. With one-character delimiters such as < and >, any "<" in the text did this. A key now has no start delimiter in it. The search from a start delimiter with no end after it also stops at the next one, rather than going on to the end of the paragraph each time: 120,000 characters of unmatched "<" took 1.4 s, and now take no time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: check the link a patch puts in a content control without properties The test only checked the control's text and its one child, so content that lost the link would have passed it. 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>
Welcome to docx
What is docx?
docx is a TypeScript/JavaScript library for generating Word documents (.docx files) programmatically. It provides a declarative API that works seamlessly in both Node.js and browser environments.
Why docx?
- Declarative API - Define what you want (objects/config), not how to build it step-by-step
- TypeScript First - Full type definitions with IntelliSense support
- Universal - Works in Node.js, browsers, and serverless environments
- No Dependencies - Zero external runtime dependencies
- Full Featured - Tables, images, shapes, charts, headers, footers, styles, and more
Quick Navigation
| I want to... | Go to... |
|---|---|
| Get started quickly | Quickstart Guide |
| Learn a specific feature | Usage Guides |
| See working examples | Demo Files |
| Browse the API | API Documentation |
| Modify existing documents | Patcher |
Installation
npm install --save docx
Then you can require or import as usual:
const docx = require("docx");
import * as docx from "docx";
// or
import { Document, Packer, Paragraph, TextRun } from "docx";
Shapes, watermarks and charts come with the package too, from docx/shapes, docx/watermarks and docx/charts:
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
import { ChartRun } from "docx/charts";
Basic Usage
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
text: " - Bold text",
bold: true,
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
Made with 💖
