Skip to content

Commit

Permalink
Allow disabling numbering inherited from a paragraph style (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalda341 authored May 20, 2024
1 parent e379a7f commit f98f852
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
56 changes: 56 additions & 0 deletions docs/usage/numbering.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,62 @@ new Paragraph({
}),
```

## 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:

```ts
const doc = new Document({
...
numbering: {
config: [
{
reference: "my-bullet-points",
levels: [
{
level: 0,
format: LevelFormat.BULLET,
text: "\u1F60",
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,
},
},
},
],
},
...
});
```

```ts
new Paragraph({
text: "No bullet points!",
style: "Bullet",
numbering: false,
}),
```

## Full Example

[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/3-numbering-and-bullet-points.ts ":include")
Expand Down
30 changes: 30 additions & 0 deletions src/file/paragraph/properties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ describe("ParagraphProperties", () => {
});
});

it("should create with numbering disabled", () => {
const properties = new ParagraphProperties({
numbering: false,
});
const tree = new Formatter().format(properties);

expect(tree).to.deep.equal({
"w:pPr": [
{
"w:numPr": [
{
"w:ilvl": {
_attr: {
"w:val": 0,
},
},
},
{
"w:numId": {
_attr: {
"w:val": 0,
},
},
},
],
},
],
});
});

it("should create with widowControl", () => {
const properties = new ParagraphProperties({
widowControl: true,
Expand Down
16 changes: 10 additions & 6 deletions src/file/paragraph/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export interface ILevelParagraphStylePropertiesOptions {
}

export interface IParagraphStylePropertiesOptions extends ILevelParagraphStylePropertiesOptions {
readonly numbering?: {
readonly reference: string;
readonly level: number;
readonly instance?: number;
readonly custom?: boolean;
};
readonly numbering?:
| {
readonly reference: string;
readonly level: number;
readonly instance?: number;
readonly custom?: boolean;
}
| false;
}

export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
Expand Down Expand Up @@ -135,6 +137,8 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
});

this.push(new NumberProperties(`${options.numbering.reference}-${options.numbering.instance ?? 0}`, options.numbering.level));
} else if (options.numbering === false) {
this.push(new NumberProperties(0, 0));
}

if (options.border) {
Expand Down

0 comments on commit f98f852

Please sign in to comment.