* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types, the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and the pages side by side, or one of them. The demos a page includes are live too. Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain. Running them showed docs that were wrong, fixed here: - Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page marked as landscape - Comments: `comments.children` takes plain objects, not `new Comment(...)` - Custom properties are text only - APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...), createParagraph().heading1(), a border's `value`, TextRun's `.break()` - The templates and patcher examples read files that aren't in the repository. They now patch the templates in demo/assets, or make their own `npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document "ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example now awaits patchDocument and writes the document it makes, as the example after it does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
18 KiB
Change Tracking
Instead of adding a
TextRuninto aParagraph, you can also add anInsertedTextRunorDeletedTextRunwhere you need to supply anid,authoranddatefor the change.
import { Document, Paragraph, TextRun, InsertedTextRun, DeletedTextRun } from "docx";
const paragraph = new Paragraph({
children: [
new TextRun("This is a simple demo "),
new TextRun({
text: "on how to ",
}),
new InsertedTextRun({
text: "mark a text as an insertion ",
id: 0,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
new DeletedTextRun({
text: "or a deletion.",
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
],
});
const doc = new Document({
sections: [
{
children: [paragraph],
},
],
});
Note that for a InsertedTextRun and DeletedTextRun, it is not possible to simply call it with only a text as in new TextRun("some text"), since the additional fields for change tracking need to be provided. Similar to a normal TextRun you can add additional text properties.
import { Document, Paragraph, TextRun, DeletedTextRun } from "docx";
const paragraph = new Paragraph({
children: [
new TextRun("This is a simple demo"),
new DeletedTextRun({
text: "with a deletion.",
color: "ff0000",
bold: true,
size: 24,
id: 0,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
],
});
const doc = new Document({
sections: [
{
children: [paragraph],
},
],
});
In addition to marking text as inserted or deleted, change tracking can also be added via the document settings. This will enable new changes to be tracked as well.
import { Document, Paragraph } from "docx";
const doc = new Document({
features: {
trackRevisions: true,
},
sections: [
{
children: [new Paragraph("Changes made to this document in Word are tracked.")],
},
],
});
If you want to express a style changes, you can add a revision to a TextRun which need to include all previous style attributes.
import { Document, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun({
bold: true,
text: "This text is now bold and was previously not",
revision: {
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:05:00Z",
bold: false,
},
}),
],
}),
],
},
],
});
Paragraph Properties Revisions
You can track changes to paragraph properties (such as alignment, spacing, indentation, borders, heading level, etc.) by adding a revision property directly to the Paragraph options. The revision must include all previous property values.
import { Document, Paragraph, AlignmentType, HeadingLevel } from "docx";
const paragraph = new Paragraph({
text: "This paragraph has changed alignment and heading",
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.RIGHT,
spacing: {
before: 400,
},
revision: {
id: 8,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
heading: HeadingLevel.HEADING_2,
alignment: AlignmentType.LEFT,
spacing: {
before: 200,
},
},
});
const doc = new Document({
sections: [
{
children: [paragraph],
},
],
});
Section Properties Revisions
You can track changes to section properties (such as page size, margins, text direction, columns, vertical alignment, title page, etc.) by adding a revision property to the section properties object. The revision must include all previous property values.
import { Document, Paragraph, TextRun, PageTextDirectionType } from "docx";
const doc = new Document({
sections: [
{
properties: {
page: {
textDirection: PageTextDirectionType.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
margin: {
top: 2000,
right: 1440,
bottom: 1440,
left: 1440,
},
},
revision: {
id: 11,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
page: {
textDirection: PageTextDirectionType.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
margin: {
top: 1440,
right: 1440,
bottom: 1440,
left: 1440,
},
},
},
},
children: [
new Paragraph({
children: [new TextRun("Section with changed text direction")],
}),
],
},
],
});
Table Revisions
Table Properties Revisions
You can track changes to table properties (such as alignment, borders, width, etc.) by adding a revision property to the Table options. The revision must include all previous property values.
import { Document, Table, TableRow, TableCell, Paragraph, AlignmentType } from "docx";
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Cell 1")],
}),
],
}),
],
alignment: AlignmentType.CENTER,
revision: {
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
alignment: AlignmentType.RIGHT,
},
});
const doc = new Document({
sections: [
{
children: [table],
},
],
});
Table Column Widths Revisions
You can track changes to table column widths by providing a columnWidthsRevision property. This tracks changes to the grid column widths.
import { Document, Table, TableRow, TableCell, Paragraph } from "docx";
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Cell 1")],
}),
new TableCell({
children: [new Paragraph("Cell 2")],
}),
],
}),
],
columnWidths: [1234, 321],
columnWidthsRevision: {
id: 1,
columnWidths: [1000, 555],
},
});
const doc = new Document({
sections: [
{
children: [table],
},
],
});
Table Row Revisions
Inserted Table Rows
To mark a table row as inserted, use the insertion property in the TableRow options. In addition, the text content within the row must also be marked as inserted using the paragraph run.insertion property (or by using InsertedTextRun). Both are required for Microsoft Word to display the insertion correctly.
import { Document, Table, TableRow, TableCell, Paragraph, TextRun } from "docx";
const row = new TableRow({
children: [
new TableCell({
children: [
new Paragraph({
children: [new TextRun("Inserted row")],
run: {
insertion: {
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
},
}),
],
}),
],
insertion: {
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
});
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [new TableRow({ children: [new TableCell({ children: [new Paragraph("Existing row")] })] }), row],
}),
],
},
],
});
Deleted Table Rows
To mark a table row as deleted, use the deletion property in the TableRow options. In addition, the text content within the row must also be marked as deleted using the paragraph run.deletion property (or by using DeletedTextRun). Both are required for Microsoft Word to display the deletion correctly.
import { Document, Table, TableRow, TableCell, Paragraph, TextRun } from "docx";
const row = new TableRow({
children: [
new TableCell({
children: [
new Paragraph({
children: [new TextRun("Deleted row")],
run: {
deletion: {
id: 2,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
},
}),
],
}),
],
deletion: {
id: 2,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
});
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [new TableRow({ children: [new TableCell({ children: [new Paragraph("Existing row")] })] }), row],
}),
],
},
],
});
Table Row Properties Revisions
You can track changes to table row properties (such as height, table header, cant split, etc.) by adding a revision property. The revision must include all previous property values.
import { Document, Table, TableRow, TableCell, Paragraph, HeightRule } from "docx";
const row = new TableRow({
children: [
new TableCell({
children: [new Paragraph("Cell content")],
}),
],
height: {
value: 200,
rule: HeightRule.EXACT,
},
tableHeader: false,
revision: {
id: 3,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
height: {
value: 300,
rule: HeightRule.EXACT,
},
tableHeader: true,
},
});
const doc = new Document({
sections: [
{
children: [new Table({ rows: [row] })],
},
],
});
Table Cell Revisions
Inserted Table Cells
To mark a table cell as inserted, use the insertion property in the TableCell options. As with table rows, the text content within the cell must also be marked as inserted using the paragraph run.insertion property (or by using InsertedTextRun). Both are required for Microsoft Word to display the insertion correctly.
import { Document, Table, TableRow, TableCell, Paragraph, TextRun } from "docx";
const cell = new TableCell({
children: [
new Paragraph({
children: [new TextRun("Inserted cell")],
run: {
insertion: {
id: 4,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
},
}),
],
insertion: {
id: 4,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
});
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [new TableRow({ children: [new TableCell({ children: [new Paragraph("Existing cell")] }), cell] })],
}),
],
},
],
});
Deleted Table Cells
To mark a table cell as deleted, use the deletion property in the TableCell options. As with table rows, the text content within the cell must also be marked as deleted using the paragraph run.deletion property (or by using DeletedTextRun). Both are required for Microsoft Word to display the deletion correctly.
import { Document, Table, TableRow, TableCell, Paragraph, TextRun } from "docx";
const cell = new TableCell({
children: [
new Paragraph({
children: [new TextRun("Deleted cell")],
run: {
deletion: {
id: 5,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
},
}),
],
deletion: {
id: 5,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
});
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [new TableRow({ children: [new TableCell({ children: [new Paragraph("Existing cell")] }), cell] })],
}),
],
},
],
});
Table Cell Properties Revisions
You can track changes to table cell properties (such as vertical alignment, text direction, borders, shading, etc.) by adding a revision property. The revision must include all previous property values.
import { Document, Table, TableRow, TableCell, Paragraph, VerticalAlignTable, TextDirection } from "docx";
const cell = new TableCell({
children: [new Paragraph("Cell content")],
verticalAlign: VerticalAlignTable.CENTER,
textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT,
revision: {
id: 6,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
verticalAlign: VerticalAlignTable.TOP,
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
},
});
const doc = new Document({
sections: [
{
children: [new Table({ rows: [new TableRow({ children: [cell] })] })],
},
],
});
Cell Merge Revisions
You can track changes to cell merging (vertical merge) by using the cellMerge property.
import { Document, Table, TableRow, TableCell, Paragraph } from "docx";
const cell = new TableCell({
children: [new Paragraph("Merged cell")],
cellMerge: {
id: 7,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
verticalMerge: "cont", // or "restart"
},
});
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [
new TableRow({ children: [new TableCell({ children: [new Paragraph("Cell above")] })] }),
new TableRow({ children: [cell] }),
],
}),
],
},
],
});
Image Revisions
You can mark an ImageRun as inserted or deleted by passing an insertion or deletion property. This wraps the image run in a <w:ins> or <w:del> element so that Word displays it as a tracked change.
import { Document, ImageRun, Paragraph, TextRun } from "docx";
import * as fs from "fs";
const doc = new Document({
features: {
trackRevisions: true,
},
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Inserted image: "),
new ImageRun({
type: "png",
data: fs.readFileSync("./demo/assets/images/dog.png"),
transformation: { width: 120, height: 120 },
insertion: {
id: 30,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
}),
],
}),
new Paragraph({
children: [
new TextRun("Deleted image: "),
new ImageRun({
type: "png",
data: fs.readFileSync("./demo/assets/images/dog.png"),
transformation: { width: 120, height: 120 },
deletion: {
id: 31,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
}),
],
}),
],
},
],
});
The insertion and deletion options accept the same properties as text track changes:
| Property | Type | Notes | Description |
|---|---|---|---|
| id | number |
Required | Unique revision ID |
| author | string |
Required | Author of the change |
| date | string |
Required | ISO 8601 timestamp of the change |
?> An ImageRun with both insertion and deletion was inserted and then deleted, such as by another author: its run is in a <w:del> inside the <w:ins>. If neither is provided, the image renders normally without track change markup.
Demo
Track Revisions (Text)
Source: https://github.com/dolanmiu/docx/blob/master/demo/track-changes/track-revisions.ts
Track Change Images
Source: https://github.com/dolanmiu/docx/blob/master/demo/track-changes/track-image-revisions.ts