* 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>
22 KiB
Bullets and Numbering
!> Bullets and Numbering requires an understanding of Sections and Paragraphs.
docx is quite flexible in its bullets and numbering system, allowing
the user great freedom in how bullets and numbers are to be styled and
displayed. E.g., numbers can be shown using Arabic numerals, roman
numerals, or even ordinal words ("one", "two", "three", ...). The
format also supports re-using bullets/numbering styles throughout the
document, so that different lists using the same style need not
redefine them.
Configuration
Numbering is configured by adding config into Document:
new Document({
numbering: {
config: [...]
}
})
Each config entry includes the following properties:
. Each level includes the following properties:
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| reference | string |
Required | A unique string |
| levels | ILevelOptions[] |
Required | a series of levels which form a sequence starting at 0 indicating the top-level list look and increasing from there to describe the sublists, then sub-sublists, etc |
Level Options
Levels define the numbering definition itself, what it looks like, the indention, the alignment and the style. The reason why it is an array is because it allows the ability to create sub-lists. A sub list will have a different configuration because you may want the sub-list to have a different indentation or different bullet.
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| level | number |
Required | The list level this definition is for. 0 is for the root level, 1 is for a sub list, 2 is for a sub-sub-list etc. |
| format | LevelFormat |
Optional | DECIMAL, UPPER_ROMAN, LOWER_ROMAN, UPPER_LETTER, LOWER_LETTER, ORDINAL, CARDINAL_TEXT, ORDINAL_TEXT, HEX, CHICAGO, IDEOGRAPH__DIGITAL, JAPANESE_COUNTING, AIUEO, IROHA, DECIMAL_FULL_WIDTH, DECIMAL_HALF_WIDTH, JAPANESE_LEGAL, JAPANESE_DIGITAL_TEN_THOUSAND, DECIMAL_ENCLOSED_CIRCLE, DECIMAL_FULL_WIDTH2, AIUEO_FULL_WIDTH, IROHA_FULL_WIDTH, DECIMAL_ZERO, BULLET, GANADA, CHOSUNG, DECIMAL_ENCLOSED_FULLSTOP, DECIMAL_ENCLOSED_PARENTHESES, DECIMAL_ENCLOSED_CIRCLE_CHINESE, IDEOGRAPH_ENCLOSED_CIRCLE, IDEOGRAPH_TRADITIONAL, IDEOGRAPH_ZODIAC, IDEOGRAPH_ZODIAC_TRADITIONAL, TAIWANESE_COUNTING, IDEOGRAPH_LEGAL_TRADITIONAL, TAIWANESE_COUNTING_THOUSAND, TAIWANESE_DIGITAL, CHINESE_COUNTING, CHINESE_LEGAL_SIMPLIFIED, CHINESE_COUNTING_THOUSAND, KOREAN_DIGITAL, KOREAN_COUNTING, KOREAN_LEGAL, KOREAN_DIGITAL2, VIETNAMESE_COUNTING, RUSSIAN_LOWER, RUSSIAN_UPPER, NONE, NUMBER_IN_DASH, HEBREW1, HEBREW2, ARABIC_ALPHA, ARABIC_ABJAD, HINDI_VOWELS, HINDI_CONSONANTS, HINDI_NUMBERS, HINDI_COUNTING, THAI_LETTERS, THAI_NUMBERS, THAI_COUNTING, BAHT_TEXT, DOLLAR_TEXT, CUSTOM |
| text | string |
Optional | A unique string to describe the shape of the bullet |
| alignment | string |
Required | START, CENTER, END, BOTH, MEDIUM_KASHIDA, DISTRIBUTE, NUM_TAB, HIGH_KASHIDA, LOW_KASHIDA, THAI_DISTRIBUTE, LEFT, RIGHT, JUSTIFIED |
| style | object |
Optional | An object containing run, paragraph, and style sub-properties (see below) |
Style sub-properties
| Property | Type | Notes | Description |
|---|---|---|---|
| run | object |
Optional | Run style properties for the numbering text (font, size, bold, etc.) |
| paragraph | object |
Optional | Paragraph style properties for the level (indent, spacing, etc.) |
| style | string |
Optional | A paragraph style ID to associate with the level. Paragraphs using that style will automatically adopt this numbering level. See Associating a level with a paragraph style |
Using ordered lists in docx
Add a numbering section to the Document to numbering style, define your levels. Use LevelFormat.UPPER_ROMAN for the format in levels. And then on a Paragraph, we can use the numbering created:
import { AlignmentType, Document, LevelFormat, Paragraph } from "docx";
const doc = new Document({
numbering: {
config: [
{
reference: "my-numbering",
levels: [
{
level: 0,
format: LevelFormat.UPPER_ROMAN,
text: "%1",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { left: 2880, hanging: 2420 },
},
},
},
],
},
],
},
sections: [
{
children: [
new Paragraph({
text: "Hey you!",
numbering: {
reference: "my-numbering",
level: 0,
},
}),
new Paragraph({
text: "Hey you again!",
numbering: {
reference: "my-numbering",
level: 0,
},
}),
],
},
],
});
Numbering options
Along with reference and level, the numbering object supports an optional instance property:
| Property | Type | Notes |
|---|---|---|
| reference | string |
The numbering style reference, must match a defined config |
| level | number |
The list level (0 = top-level, 1 = sub-list, etc.) |
| instance | number |
(Optional) Identifies the instance of the list. Lists with the same instance (or none) continue numbering across paragraphs. A new instance value restarts numbering from 1. |
Example:
import { AlignmentType, Document, LevelFormat, Paragraph } from "docx";
const doc = new Document({
numbering: {
config: [
{
reference: "my-numbering",
levels: [
{
level: 0,
format: LevelFormat.DECIMAL,
text: "%1.",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { left: 720, hanging: 260 },
},
},
},
],
},
],
},
sections: [
{
children: [
new Paragraph({
text: "First list item",
numbering: { reference: "my-numbering", level: 0, instance: 1 },
}),
new Paragraph({
text: "Second list item",
numbering: { reference: "my-numbering", level: 0, instance: 1 },
}),
new Paragraph({
text: "New list, starts again at 1",
numbering: { reference: "my-numbering", level: 0, instance: 2 },
}),
],
},
],
});
Un-ordered lists / Bullet points
Add a numbering section to the Document to numbering style, define your levels. Use LevelFormat.BULLET for the format in levels. And then on a Paragraph, we can use the numbering created:
import { AlignmentType, convertInchesToTwip, Document, LevelFormat, Paragraph } from "docx";
const doc = new Document({
numbering: {
config: [
{
reference: "my-bullet-points",
levels: [
{
level: 0,
format: LevelFormat.BULLET,
text: "\u2022",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.25) },
},
},
},
],
},
],
},
sections: [
{
children: [
new Paragraph({
text: "Hey you!",
numbering: {
reference: "my-bullet-points",
level: 0,
},
}),
new Paragraph({
text: "Hey you again!",
numbering: {
reference: "my-bullet-points",
level: 0,
},
}),
],
},
],
});
Disabling numbering inherited from paragraph style
If the numbering is set on a paragraph style, you may wish to disable it for a specific paragraph:
import { AlignmentType, convertInchesToTwip, Document, LevelFormat, Paragraph } from "docx";
const doc = new Document({
numbering: {
config: [
{
reference: "my-bullet-points",
levels: [
{
level: 0,
format: LevelFormat.BULLET,
text: "\u2022",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.25) },
},
},
},
],
},
],
},
styles: {
paragraphStyles: [
{
id: "bullet",
name: "Bullet",
basedOn: "Normal",
next: "Normal",
run: {},
paragraph: {
numbering: {
reference: "my-bullet-points",
level: 0,
},
},
},
],
},
sections: [
{
children: [
new Paragraph({
text: "A bullet point from the style",
style: "bullet",
}),
new Paragraph({
text: "No bullet points!",
style: "bullet",
numbering: false,
}),
],
},
],
});
Associating a level with a paragraph style
You can link a numbering level to a paragraph style using style.style. Any paragraph that uses the associated style will automatically adopt the numbering level without needing to specify numbering on each paragraph individually.
This is useful for creating reusable list styles — define the numbering once and apply it via the paragraph style:
import { AlignmentType, Document, LevelFormat, Paragraph } from "docx";
const doc = new Document({
styles: {
paragraphStyles: [
{
id: "numberedListParagraph",
name: "Numbered List Paragraph",
paragraph: {
numbering: {
reference: "styled-numbering",
level: 0,
},
spacing: { after: 120 },
},
},
],
},
numbering: {
config: [
{
reference: "styled-numbering",
levels: [
{
level: 0,
format: LevelFormat.DECIMAL,
text: "%1.",
alignment: AlignmentType.START,
style: {
style: "numberedListParagraph",
paragraph: {
indent: { left: 720, hanging: 260 },
},
},
},
],
},
],
},
sections: [
{
children: [
new Paragraph({
text: "First styled list item",
style: "numberedListParagraph",
}),
new Paragraph({
text: "Second styled list item",
style: "numberedListParagraph",
}),
],
},
],
});
The style.style value must match the id of a paragraph style defined in styles.paragraphStyles. This creates a <w:pStyle> element inside the numbering level definition, linking the two.
Examples
Numbering and Bullet Points
Source: https://github.com/dolanmiu/docx/blob/master/demo/lists/numbering-and-bullet-points.ts
Numbering with Paragraph Styles
Source: https://github.com/dolanmiu/docx/blob/master/demo/lists/numbering-level-paragraph-style.ts