better docx
Usage

Change Tracking

Instead of adding a TextRun into a Paragraph, you can also add an InsertedTextRun or DeletedTextRun where you need to supply an id, author and date for the change.

import { Paragraph, TextRun, InsertedTextRun, DeletedTextRun } from "betterdocx";

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",
        }),
    ],
});

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. Beyond id, author and date, both accept the full set of run options (IRunOptions), so you can style them like a normal TextRun — including break, children, fonts, colors and so on.

import { Paragraph, TextRun, InsertedTextRun, DeletedTextRun } from "betterdocx";

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",
        }),
    ],
});

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 } from "betterdocx";

const doc = new Document({
    features: {
        trackRevisions: true,
    },
});

Tracking formatting changes

To express a tracked formatting change (rather than an insertion or deletion), add a revision to a run. This emits a w:rPrChange element containing the previous formatting of the run, so Word can show what changed. The current formatting is set on the run itself as usual.

revision requires id, author and date, and additionally accepts all run formatting properties (bold, italics, size, color, font, etc.) describing the run's formatting before the change.

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,
    },
});

Deleting page number fields

A DeletedTextRun can also mark page-number fields as deleted. Pass the field via children, using any PageNumber value. The field instruction is then emitted in its deleted form (w:delInstrText). This is useful when tracking changes inside headers and footers.

import { DeletedTextRun, PageNumber } from "betterdocx";

new DeletedTextRun({
    children: ["Page ", PageNumber.CURRENT, " of ", PageNumber.TOTAL_PAGES],
    id: 2,
    author: "Firstname Lastname",
    date: "2020-10-06T09:00:00Z",
});

This special deleted form only exists for DeletedTextRun. In an InsertedTextRun, page-number children are emitted as regular field codes.

On this page