Compare commits

...

20 Commits
9.1.1 ... 9.3.0

Author SHA1 Message Date
a708475539 export IPropertiesOptions for use by other libraries or code using docx library (#2979)
* export IPropertiesOptions for use by other libraries or code using docx library

* fixes #2978: export OutputTypes

* Export file in main index file

* Move type export to inside

---------

Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
2025-03-15 03:18:43 +00:00
9c60cfcbc7 build(deps): bump the npm_and_yarn group with 12 updates (#2992)
Bumps the npm_and_yarn group with 15 updates:

| Package | From | To |
| --- | --- | --- |
| [vitest](https://github.com/vitest-dev/vitest/tree/HEAD/packages/vitest) | `2.1.8` | `2.1.9` |
| [@vitest/coverage-v8](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-v8) | `2.1.8` | `2.1.9` |
| [@vitest/ui](https://github.com/vitest-dev/vitest/tree/HEAD/packages/ui) | `2.1.8` | `2.1.9` |
| [dompurify](https://github.com/cure53/DOMPurify) | `2.5.7` | `removed` |
| [docsify](https://github.com/docsifyjs/docsify) | `4.12.2` | `4.13.1` |
| [docsify-server-renderer](https://github.com/docsifyjs/docsify) | `4.12.2` | `4.13.1` |
| [elliptic](https://github.com/indutny/elliptic) | `6.6.0` | `6.6.1` |
| [esbuild](https://github.com/evanw/esbuild) | `0.21.5` | `0.25.1` |
| [@vitest/coverage-v8](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-v8) | `2.1.9` | `3.0.8` |
| [@vitest/ui](https://github.com/vitest-dev/vitest/tree/HEAD/packages/ui) | `2.1.9` | `3.0.8` |
| [tsx](https://github.com/privatenumber/tsx) | `4.19.2` | `4.19.3` |
| [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) | `6.0.9` | `6.2.2` |
| [vite-plugin-node-polyfills](https://github.com/davidmyersdev/vite-plugin-node-polyfills) | `0.22.0` | `0.23.0` |
| [vitest](https://github.com/vitest-dev/vitest/tree/HEAD/packages/vitest) | `2.1.9` | `3.0.8` |
| [prismjs](https://github.com/PrismJS/prism) | `1.27.0` | `1.30.0` |


Updates `vitest` from 2.1.8 to 2.1.9
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v2.1.9/packages/vitest)

Updates `@vitest/coverage-v8` from 2.1.8 to 2.1.9
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v2.1.9/packages/coverage-v8)

Updates `@vitest/ui` from 2.1.8 to 2.1.9
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v2.1.9/packages/ui)

Removes `dompurify`

Updates `docsify` from 4.12.2 to 4.13.1
- [Release notes](https://github.com/docsifyjs/docsify/releases)
- [Changelog](https://github.com/docsifyjs/docsify/blob/v4.13.1/CHANGELOG.md)
- [Commits](https://github.com/docsifyjs/docsify/compare/v4.12.2...v4.13.1)

Updates `docsify-server-renderer` from 4.12.2 to 4.13.1
- [Release notes](https://github.com/docsifyjs/docsify/releases)
- [Changelog](https://github.com/docsifyjs/docsify/blob/v4.13.1/CHANGELOG.md)
- [Commits](https://github.com/docsifyjs/docsify/compare/v4.12.2...v4.13.1)

Updates `elliptic` from 6.6.0 to 6.6.1
- [Commits](https://github.com/indutny/elliptic/compare/v6.6.0...v6.6.1)

Updates `esbuild` from 0.21.5 to 0.25.1
- [Release notes](https://github.com/evanw/esbuild/releases)
- [Changelog](https://github.com/evanw/esbuild/blob/main/CHANGELOG-2024.md)
- [Commits](https://github.com/evanw/esbuild/compare/v0.21.5...v0.25.1)

Updates `@vitest/coverage-v8` from 2.1.9 to 3.0.8
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v2.1.9/packages/coverage-v8)

Updates `@vitest/ui` from 2.1.9 to 3.0.8
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v2.1.9/packages/ui)

Updates `tsx` from 4.19.2 to 4.19.3
- [Release notes](https://github.com/privatenumber/tsx/releases)
- [Changelog](https://github.com/privatenumber/tsx/blob/master/release.config.cjs)
- [Commits](https://github.com/privatenumber/tsx/compare/v4.19.2...v4.19.3)

Updates `vite` from 6.0.9 to 6.2.2
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v6.2.2/packages/vite)

Updates `vite-plugin-node-polyfills` from 0.22.0 to 0.23.0
- [Release notes](https://github.com/davidmyersdev/vite-plugin-node-polyfills/releases)
- [Commits](https://github.com/davidmyersdev/vite-plugin-node-polyfills/compare/v0.22.0...v0.23.0)

Updates `vitest` from 2.1.9 to 3.0.8
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v2.1.9/packages/vitest)

Updates `prismjs` from 1.27.0 to 1.30.0
- [Release notes](https://github.com/PrismJS/prism/releases)
- [Changelog](https://github.com/PrismJS/prism/blob/master/CHANGELOG.md)
- [Commits](https://github.com/PrismJS/prism/compare/v1.27.0...v1.30.0)

---
updated-dependencies:
- dependency-name: vitest
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: "@vitest/coverage-v8"
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: "@vitest/ui"
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: dompurify
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: docsify
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: docsify-server-renderer
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: elliptic
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: esbuild
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: "@vitest/coverage-v8"
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: "@vitest/ui"
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: tsx
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: vite
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: vite-plugin-node-polyfills
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: vitest
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: prismjs
  dependency-type: indirect
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-15 03:05:07 +00:00
a5454edc61 Feature/math improve (#2976)
* add new feature

* add test file

* update test file

* update test file

* Fix linting

---------

Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
2025-03-15 03:04:01 +00:00
7152abfe48 build(deps-dev): bump eslint-plugin-jsdoc from 50.6.0 to 50.6.6 (#2987)
Bumps [eslint-plugin-jsdoc](https://github.com/gajus/eslint-plugin-jsdoc) from 50.6.0 to 50.6.6.
- [Release notes](https://github.com/gajus/eslint-plugin-jsdoc/releases)
- [Changelog](https://github.com/gajus/eslint-plugin-jsdoc/blob/main/.releaserc)
- [Commits](https://github.com/gajus/eslint-plugin-jsdoc/compare/v50.6.0...v50.6.6)

---
updated-dependencies:
- dependency-name: eslint-plugin-jsdoc
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:44:41 +00:00
075eeb7e3c build(deps-dev): bump eslint from 9.16.0 to 9.22.0 (#2983)
Bumps [eslint](https://github.com/eslint/eslint) from 9.16.0 to 9.22.0.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md)
- [Commits](https://github.com/eslint/eslint/compare/v9.16.0...v9.22.0)

---
updated-dependencies:
- dependency-name: eslint
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:44:33 +00:00
f0acb3f3fb build(deps-dev): bump inquirer from 12.3.0 to 12.4.3 (#2982)
Bumps [inquirer](https://github.com/SBoudrias/Inquirer.js) from 12.3.0 to 12.4.3.
- [Release notes](https://github.com/SBoudrias/Inquirer.js/releases)
- [Commits](https://github.com/SBoudrias/Inquirer.js/compare/inquirer@12.3.0...inquirer@12.4.3)

---
updated-dependencies:
- dependency-name: inquirer
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:44:26 +00:00
ae048833a1 build(deps-dev): bump vite-plugin-dts from 4.3.0 to 4.5.3 (#2973)
Bumps [vite-plugin-dts](https://github.com/qmhc/vite-plugin-dts) from 4.3.0 to 4.5.3.
- [Release notes](https://github.com/qmhc/vite-plugin-dts/releases)
- [Changelog](https://github.com/qmhc/vite-plugin-dts/blob/main/CHANGELOG.md)
- [Commits](https://github.com/qmhc/vite-plugin-dts/compare/v4.3.0...v4.5.3)

---
updated-dependencies:
- dependency-name: vite-plugin-dts
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:44:17 +00:00
ebae72004c Fix support for after auto-spacing. (#2975) 2025-03-14 23:44:09 +00:00
13744abff5 build(deps-dev): bump cspell from 8.16.1 to 8.17.5 (#2969)
Bumps [cspell](https://github.com/streetsidesoftware/cspell/tree/HEAD/packages/cspell) from 8.16.1 to 8.17.5.
- [Release notes](https://github.com/streetsidesoftware/cspell/releases)
- [Changelog](https://github.com/streetsidesoftware/cspell/blob/main/packages/cspell/CHANGELOG.md)
- [Commits](https://github.com/streetsidesoftware/cspell/commits/v8.17.5/packages/cspell)

---
updated-dependencies:
- dependency-name: cspell
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:43:46 +00:00
492ef29d7f build(deps-dev): bump vite from 6.0.7 to 6.0.9 in the npm_and_yarn group (#2940)
Bumps the npm_and_yarn group with 1 update: [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite).


Updates `vite` from 6.0.7 to 6.0.9
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v6.0.9/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:43:39 +00:00
a6e6463a36 build(deps-dev): bump jiti from 2.4.1 to 2.4.2 (#2932)
Bumps [jiti](https://github.com/unjs/jiti) from 2.4.1 to 2.4.2.
- [Release notes](https://github.com/unjs/jiti/releases)
- [Changelog](https://github.com/unjs/jiti/blob/main/CHANGELOG.md)
- [Commits](https://github.com/unjs/jiti/compare/v2.4.1...v2.4.2)

---
updated-dependencies:
- dependency-name: jiti
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:43:32 +00:00
2ef596543b build(deps-dev): bump glob from 11.0.0 to 11.0.1 (#2930)
Bumps [glob](https://github.com/isaacs/node-glob) from 11.0.0 to 11.0.1.
- [Changelog](https://github.com/isaacs/node-glob/blob/main/changelog.md)
- [Commits](https://github.com/isaacs/node-glob/compare/v11.0.0...v11.0.1)

---
updated-dependencies:
- dependency-name: glob
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:43:25 +00:00
85005c3c07 build(deps-dev): bump jsdom from 25.0.1 to 26.0.0 (#2928)
Bumps [jsdom](https://github.com/jsdom/jsdom) from 25.0.1 to 26.0.0.
- [Release notes](https://github.com/jsdom/jsdom/releases)
- [Changelog](https://github.com/jsdom/jsdom/blob/main/Changelog.md)
- [Commits](https://github.com/jsdom/jsdom/compare/25.0.1...26.0.0)

---
updated-dependencies:
- dependency-name: jsdom
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-14 23:43:17 +00:00
0d2b433446 Chore: bump nanoid to fix security issue (#2991) 2025-03-14 23:43:08 +00:00
b1f67652e9 docs: add Playground (Docx.js Editor) url to README.md (#2965) 2025-02-23 20:36:33 +01:00
4e2befb7ef Update docs for new Packer methods (#2961)
To cover #2920
2025-02-17 21:22:07 +00:00
a887927968 Version bump 2025-02-16 18:35:16 +00:00
ee6db1429a Add shading and border settings to paragraph style options. (#2955)
* Add shading and border settings to paragraph style options.

* Fix prettier

---------

Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
2025-02-16 18:33:21 +00:00
170309a7ed Add Packer.pack and Packer.toArrayBuffer (#2959)
* Add Packer.pack and Packer.toArrayBuffer

To mirror patchDocument's outputType parameter.

See https://github.com/dolanmiu/docx/discussions/2920

* Ignore coverage

---------

Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
2025-02-16 18:24:15 +00:00
05fcf6edd4 Feat: subfile overrides (#2941)
* Adds overrides parameter to Packer methods and compiler

* Adds tests for overrides parameter

* Update Packer usage examples
2025-01-27 10:39:23 +00:00
22 changed files with 3363 additions and 4320 deletions

View File

@ -14,6 +14,7 @@
[![Known Vulnerabilities][snky-image]][snky-url] [![Known Vulnerabilities][snky-image]][snky-url]
[![PRs Welcome][pr-image]][pr-url] [![PRs Welcome][pr-image]][pr-url]
[![codecov][codecov-image]][codecov-url] [![codecov][codecov-image]][codecov-url]
[![Docx.js Editor][docxjs-editor-image]][docxjs-editor-url]
<p align="center"> <p align="center">
<img src="https://i.imgur.com/QeL1HuU.png" alt="drawing"/> <img src="https://i.imgur.com/QeL1HuU.png" alt="drawing"/>
@ -64,6 +65,10 @@ More [here](https://github.com/dolanmiu/docx/tree/master/demo)
Please refer to the [documentation at https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more! Please refer to the [documentation at https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!
# Playground
Experience `docx` in action through [Docx.js Editor][docxjs-editor-url], an interactive playground where you can code and preview the results in real-time.
# Examples # Examples
Check the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples. Check the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples.
@ -115,3 +120,5 @@ Made with 💖
[patreon-url]: https://www.patreon.com/dolanmiu [patreon-url]: https://www.patreon.com/dolanmiu
[browserstack-image]: https://user-images.githubusercontent.com/2917613/54233552-128e9d00-4505-11e9-88fb-025a4e04007c.png [browserstack-image]: https://user-images.githubusercontent.com/2917613/54233552-128e9d00-4505-11e9-88fb-025a4e04007c.png
[browserstack-url]: https://www.browserstack.com [browserstack-url]: https://www.browserstack.com
[docxjs-editor-image]: https://img.shields.io/badge/Docx.js%20Editor-2b579a.svg?style=flat&amp;logo=javascript&amp;logoColor=white
[docxjs-editor-url]: https://docxjs-editor.vercel.app/

View File

@ -0,0 +1,60 @@
import * as fs from "fs";
import { BorderStyle, Document, Packer, Paragraph, TextRun } from "docx";
const doc = new Document({
styles: {
paragraphStyles: [
{
id: "withSingleBlackBordersAndYellowShading",
name: "Paragraph Style with Black Borders and Yellow Shading",
basedOn: "Normal",
paragraph: {
shading: {
color: "#fff000",
type: "solid",
},
border: {
top: {
style: BorderStyle.SINGLE,
color: "#000000",
size: 4,
},
bottom: {
style: BorderStyle.SINGLE,
color: "#000000",
size: 4,
},
left: {
style: BorderStyle.SINGLE,
color: "#000000",
size: 4,
},
right: {
style: BorderStyle.SINGLE,
color: "#000000",
size: 4,
},
},
},
},
],
},
sections: [
{
children: [
new Paragraph({
style: "withSingleBlackBordersAndYellowShading",
children: [
new TextRun({
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -2,7 +2,7 @@
> Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`. > Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`.
Packers works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of `version 4+`, this library will not have options to export to PDF. Packers works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob`, `string`, `base64 string`, `ArrayBuffer`, or `Stream`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of `version 4+`, this library will not have options to export to PDF.
### Export as Buffer ### Export as Buffer
@ -14,6 +14,14 @@ Packer.toBuffer(doc).then((buffer) => {
}); });
``` ```
### Export as string
```ts
Packer.toString(doc).then((string) => {
console.log(string);
});
```
### Export as a `base64` string ### Export as a `base64` string
```ts ```ts
@ -32,3 +40,46 @@ Packer.toBlob(doc).then((blob) => {
saveAs(blob, "example.docx"); saveAs(blob, "example.docx");
}); });
``` ```
### Export as ArrayBuffer
This may be useful when working in a Node.js worker.
```ts
Packer.toArrayBuffer(doc).then((arrayBuffer) => {
port.postMessage(arrayBuffer, [arrayBuffer]);
});
```
### Export as a Stream
```ts
Packer.toStream(doc).then((stream) => {
// read from stream
});
```
### Export using optional arguments
The `Packer` methods support 2 optional arguments.
The first is for controlling the indentation of the xml and should be a `boolean` or `keyof typeof PrettifyType`.
The second is an array of subfile overrides (`{path: string, data: string}[]`). These overrides can be used to write additional subfiles to the result or even override default subfiles in the case that the default handling of these subfiles does not meet your needs.
```ts
const overrides = [{ path: "word/commentsExtended.xml", data: "string_data" }];
Packer.toString(doc, true, overrides).then((string) => {
console.log(string);
});
```
### Export to arbitrary formats
You can also use the lower-level `Packer.pack` method to export to any specified type.
```ts
Packer.pack(doc, 'string').then((string) => {
console.log(string);
});
```

7188
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "docx", "name": "docx",
"version": "9.1.1", "version": "9.2.0",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.", "description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"type": "module", "type": "module",
"main": "dist/index.umd.cjs", "main": "dist/index.umd.cjs",
@ -61,7 +61,7 @@
"@types/node": "^22.7.5", "@types/node": "^22.7.5",
"hash.js": "^1.1.7", "hash.js": "^1.1.7",
"jszip": "^3.10.1", "jszip": "^3.10.1",
"nanoid": "^5.0.4", "nanoid": "^5.1.3",
"xml": "^1.0.1", "xml": "^1.0.1",
"xml-js": "^1.6.8" "xml-js": "^1.6.8"
}, },
@ -79,8 +79,8 @@
"@types/xml": "^1.0.8", "@types/xml": "^1.0.8",
"@typescript-eslint/eslint-plugin": "^8.8.1", "@typescript-eslint/eslint-plugin": "^8.8.1",
"@typescript-eslint/parser": "^8.8.1", "@typescript-eslint/parser": "^8.8.1",
"@vitest/coverage-v8": "^2.1.8", "@vitest/coverage-v8": "^3.0.8",
"@vitest/ui": "^2.1.2", "@vitest/ui": "^3.0.8",
"cspell": "^8.2.3", "cspell": "^8.2.3",
"docsify-cli": "^4.3.0", "docsify-cli": "^4.3.0",
"eslint": "^9.13.0", "eslint": "^9.13.0",
@ -95,7 +95,7 @@
"glob": "^11.0.0", "glob": "^11.0.0",
"inquirer": "^12.0.0", "inquirer": "^12.0.0",
"jiti": "^2.3.3", "jiti": "^2.3.3",
"jsdom": "^25.0.1", "jsdom": "^26.0.0",
"pre-commit": "^1.2.2", "pre-commit": "^1.2.2",
"prettier": "^3.1.1", "prettier": "^3.1.1",
"tsconfig-paths": "^4.0.0", "tsconfig-paths": "^4.0.0",
@ -106,9 +106,9 @@
"unzipper": "^0.12.3", "unzipper": "^0.12.3",
"vite": "^6.0.1", "vite": "^6.0.1",
"vite-plugin-dts": "^4.2.4", "vite-plugin-dts": "^4.2.4",
"vite-plugin-node-polyfills": "^0.22.0", "vite-plugin-node-polyfills": "^0.23.0",
"vite-tsconfig-paths": "^5.0.1", "vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.1.8" "vitest": "^3.0.8"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=10"

View File

@ -112,6 +112,41 @@ describe("Compiler", () => {
}, },
); );
it(
"should pack subfile overrides",
async () => {
const file = new File({
sections: [],
comments: {
children: [],
},
});
const subfileData1 = "comments";
const subfileData2 = "commentsExtended";
const overrides = [
{ path: "word/comments.xml", data: subfileData1 },
{ path: "word/commentsExtended.xml", data: subfileData2 },
];
const zipFile = compiler.compile(file, "", overrides);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
expect(fileNames).is.an.instanceof(Array);
expect(fileNames).has.length(20);
expect(fileNames).to.include("word/comments.xml");
expect(fileNames).to.include("word/commentsExtended.xml");
const commentsText = await zipFile.file("word/comments.xml")?.async("text");
const commentsExtendedText = await zipFile.file("word/commentsExtended.xml")?.async("text");
expect(commentsText).toBe(subfileData1);
expect(commentsExtendedText).toBe(subfileData2);
},
{
timeout: 99999999,
},
);
it("should call the format method X times equalling X files to be formatted", () => { it("should call the format method X times equalling X files to be formatted", () => {
// This test is required because before, there was a case where Document was formatted twice, which was inefficient // This test is required because before, there was a case where Document was formatted twice, which was inefficient
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times. // This also caused issues such as running prepForXml multiple times as format() was ran multiple times.

View File

@ -9,7 +9,7 @@ import { ImageReplacer } from "./image-replacer";
import { NumberingReplacer } from "./numbering-replacer"; import { NumberingReplacer } from "./numbering-replacer";
import { PrettifyType } from "./packer"; import { PrettifyType } from "./packer";
type IXmlifyedFile = { export type IXmlifyedFile = {
readonly data: string; readonly data: string;
readonly path: string; readonly path: string;
}; };
@ -47,7 +47,11 @@ export class Compiler {
this.numberingReplacer = new NumberingReplacer(); this.numberingReplacer = new NumberingReplacer();
} }
public compile(file: File, prettifyXml?: (typeof PrettifyType)[keyof typeof PrettifyType]): JSZip { public compile(
file: File,
prettifyXml?: (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): JSZip {
const zip = new JSZip(); const zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml); const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
const map = new Map<string, IXmlifyedFile | readonly IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping)); const map = new Map<string, IXmlifyedFile | readonly IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping));
@ -62,6 +66,10 @@ export class Compiler {
} }
} }
for (const subFile of overrides) {
zip.file(subFile.path, subFile.data);
}
for (const data of file.Media.Array) { for (const data of file.Media.Array) {
if (data.type !== "svg") { if (data.type !== "svg") {
zip.file(`word/media/${data.fileName}`, data.data); zip.file(`word/media/${data.fileName}`, data.data);

View File

@ -46,7 +46,7 @@ describe("Packer", () => {
await Packer.toString(file, true); await Packer.toString(file, true);
expect(spy).toBeCalledWith(expect.anything(), PrettifyType.WITH_2_BLANKS); expect(spy).toBeCalledWith(expect.anything(), PrettifyType.WITH_2_BLANKS, expect.anything());
}); });
it("should use a prettify value", async () => { it("should use a prettify value", async () => {
@ -55,7 +55,7 @@ describe("Packer", () => {
await Packer.toString(file, PrettifyType.WITH_4_BLANKS); await Packer.toString(file, PrettifyType.WITH_4_BLANKS);
expect(spy).toBeCalledWith(expect.anything(), PrettifyType.WITH_4_BLANKS); expect(spy).toBeCalledWith(expect.anything(), PrettifyType.WITH_4_BLANKS, expect.anything());
}); });
it("should use an undefined prettify value", async () => { it("should use an undefined prettify value", async () => {
@ -64,7 +64,32 @@ describe("Packer", () => {
await Packer.toString(file, false); await Packer.toString(file, false);
expect(spy).toBeCalledWith(expect.anything(), undefined); expect(spy).toBeCalledWith(expect.anything(), undefined, expect.anything());
});
});
describe("overrides", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("should use an overrides value", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const spy = vi.spyOn((Packer as any).compiler, "compile");
const overrides = [{ path: "word/comments.xml", data: "comments" }];
await Packer.toString(file, true, overrides);
expect(spy).toBeCalledWith(expect.anything(), expect.anything(), overrides);
});
it("should use a default overrides value", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const spy = vi.spyOn((Packer as any).compiler, "compile");
await Packer.toString(file);
expect(spy).toBeCalledWith(expect.anything(), undefined, []);
}); });
}); });
@ -162,6 +187,33 @@ describe("Packer", () => {
}); });
}); });
describe("#toArrayBuffer()", () => {
it("should create a standard docx file", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn((Packer as any).compiler, "compile").mockReturnValue({
generateAsync: () => vi.fn(),
});
const str = await Packer.toArrayBuffer(file);
assert.isDefined(str);
});
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn((Packer as any).compiler, "compile").mockImplementation(() => {
throw new Error();
});
return Packer.toArrayBuffer(file).catch((error) => {
assert.isDefined(error);
});
});
afterEach(() => {
vi.resetAllMocks();
});
});
describe("#toStream()", () => { describe("#toStream()", () => {
it("should create a standard docx file", async () => { it("should create a standard docx file", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any

View File

@ -1,8 +1,9 @@
import { Stream } from "stream"; import { Stream } from "stream";
import { File } from "@file/file"; import { File } from "@file/file";
import { OutputByType, OutputType } from "@util/output-type";
import { Compiler } from "./next-compiler"; import { Compiler, IXmlifyedFile } from "./next-compiler";
/** /**
* Use blanks to prettify * Use blanks to prettify
@ -21,53 +22,68 @@ const convertPrettifyType = (
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify; prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify;
export class Packer { export class Packer {
public static async toString(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<string> { // eslint-disable-next-line require-await
const zip = this.compiler.compile(file, convertPrettifyType(prettify)); public static async pack<T extends OutputType>(
const zipData = await zip.generateAsync({ file: File,
type: "string", type: T,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<OutputByType[T]> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
return zip.generateAsync({
type,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE", compression: "DEFLATE",
}); });
return zipData;
} }
public static async toBuffer(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<Buffer> { public static toString(
const zip = this.compiler.compile(file, convertPrettifyType(prettify)); file: File,
const zipData = await zip.generateAsync({ prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
type: "nodebuffer", overrides: readonly IXmlifyedFile[] = [],
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ): Promise<string> {
compression: "DEFLATE", return Packer.pack(file, "string", prettify, overrides);
});
return zipData;
} }
public static async toBase64String(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<string> { public static toBuffer(
const zip = this.compiler.compile(file, convertPrettifyType(prettify)); file: File,
const zipData = await zip.generateAsync({ prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
type: "base64", overrides: readonly IXmlifyedFile[] = [],
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ): Promise<Buffer> {
compression: "DEFLATE", return Packer.pack(file, "nodebuffer", prettify, overrides);
});
return zipData;
} }
public static async toBlob(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<Blob> { public static toBase64String(
const zip = this.compiler.compile(file, convertPrettifyType(prettify)); file: File,
const zipData = await zip.generateAsync({ prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
type: "blob", overrides: readonly IXmlifyedFile[] = [],
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ): Promise<string> {
compression: "DEFLATE", return Packer.pack(file, "base64", prettify, overrides);
});
return zipData;
} }
public static toStream(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Stream { public static toBlob(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<Blob> {
return Packer.pack(file, "blob", prettify, overrides);
}
public static toArrayBuffer(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Promise<ArrayBuffer> {
return Packer.pack(file, "arraybuffer", prettify, overrides);
}
public static toStream(
file: File,
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
overrides: readonly IXmlifyedFile[] = [],
): Stream {
const stream = new Stream(); const stream = new Stream();
const zip = this.compiler.compile(file, convertPrettifyType(prettify)); const zip = this.compiler.compile(file, convertPrettifyType(prettify), overrides);
zip.generateAsync({ zip.generateAsync({
type: "nodebuffer", type: "nodebuffer",

View File

@ -21,3 +21,4 @@ export * from "./vertical-align";
export * from "./checkbox"; export * from "./checkbox";
export * from "./fonts"; export * from "./fonts";
export * from "./textbox"; export * from "./textbox";
export { type IPropertiesOptions } from "./core-properties";

View File

@ -24,7 +24,7 @@ class SpacingAttributes extends XmlAttributeComponent<ISpacingProperties> {
line: "w:line", line: "w:line",
lineRule: "w:lineRule", lineRule: "w:lineRule",
beforeAutoSpacing: "w:beforeAutospacing", beforeAutoSpacing: "w:beforeAutospacing",
afterAutoSpacing: "w:afterAutoSpacing", afterAutoSpacing: "w:afterAutospacing",
}; };
} }

View File

@ -0,0 +1,2 @@
export * from "./math-bar";
export * from "./math-bar-properties";

View File

@ -0,0 +1,11 @@
// https://www.datypic.com/sc/ooxml/e-m_pos-1.html
import { Attributes, XmlComponent } from "@file/xml-components";
export class MathBarPos extends XmlComponent {
// TODO: Use correct types rather than any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public constructor(attributes: any) {
super("m:pos");
this.root.push(new Attributes(attributes));
}
}

View File

@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { Formatter } from "@export/formatter";
import { MathBarProperties } from "./math-bar-properties";
describe("MathBarProperties", () => {
describe("#constructor()", () => {
it("should create a MathBarProperties with top key", () => {
const mathBarProperties = new MathBarProperties("top");
const tree = new Formatter().format(mathBarProperties);
expect(tree).to.deep.equal({
"m:barPr": [
{
"m:pos": {
_attr: {
"w:val": "top",
},
},
},
],
});
});
it("should create a MathBarProperties with bottom key", () => {
const mathBarProperties = new MathBarProperties("bot");
const tree = new Formatter().format(mathBarProperties);
expect(tree).to.deep.equal({
"m:barPr": [
{
"m:pos": {
_attr: {
"w:val": "bot",
},
},
},
],
});
});
});
});

View File

@ -0,0 +1,11 @@
// https://www.datypic.com/sc/ooxml/e-m_barPr-1.html
import { XmlComponent } from "@file/xml-components";
import { MathBarPos } from "./math-bar-pos";
export class MathBarProperties extends XmlComponent {
public constructor(type: string) {
super("m:barPr");
this.root.push(new MathBarPos({ val: type }));
}
}

View File

@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { Formatter } from "@export/formatter";
import { MathBar } from "./math-bar";
import { MathRun } from "../math-run";
describe("MathBar", () => {
describe("#constructor()", () => {
it("should create a MathBar with correct root key", () => {
const mathBar = new MathBar({ type: "top", children: [new MathRun("text")] });
const tree = new Formatter().format(mathBar);
expect(tree).to.deep.equal({
"m:bar": [
{
"m:barPr": [
{
"m:pos": {
_attr: {
"w:val": "top",
},
},
},
],
},
{
"m:e": [
{
"m:r": [{ "m:t": ["text"] }],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,18 @@
// https://www.datypic.com/sc/ooxml/e-m_bar-1.html
import { XmlComponent } from "@file/xml-components";
import { MathBarProperties } from "./math-bar-properties";
import type { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
type MathBarOption = {
readonly type: "top" | "bot";
readonly children: readonly MathComponent[];
};
export class MathBar extends XmlComponent {
public constructor(options: MathBarOption) {
super("m:bar");
this.root.push(new MathBarProperties(options.type));
this.root.push(new MathBase(options.children));
}
}

View File

@ -2,7 +2,7 @@
import { XmlComponent } from "@file/xml-components"; import { XmlComponent } from "@file/xml-components";
import { MathPreSubSuperScriptProperties } from "./math-pre-sub-super-script-function-properties"; import { MathPreSubSuperScriptProperties } from "./math-pre-sub-super-script-function-properties";
import { MathComponent } from "../../math-component"; import type { MathComponent } from "../../math-component";
import { MathBase, MathSubScriptElement, MathSuperScriptElement } from "../../n-ary"; import { MathBase, MathSubScriptElement, MathSuperScriptElement } from "../../n-ary";
export type IMathPreSubSuperScriptOptions = { export type IMathPreSubSuperScriptOptions = {

View File

@ -38,6 +38,8 @@ export type ILevelParagraphStylePropertiesOptions = {
}; };
export type IParagraphStylePropertiesOptions = { export type IParagraphStylePropertiesOptions = {
readonly border?: IBordersOptions;
readonly shading?: IShadingAttributesProperties;
readonly numbering?: readonly numbering?:
| { | {
readonly reference: string; readonly reference: string;
@ -49,7 +51,6 @@ export type IParagraphStylePropertiesOptions = {
} & ILevelParagraphStylePropertiesOptions; } & ILevelParagraphStylePropertiesOptions;
export type IParagraphPropertiesOptions = { export type IParagraphPropertiesOptions = {
readonly border?: IBordersOptions;
readonly heading?: (typeof HeadingLevel)[keyof typeof HeadingLevel]; readonly heading?: (typeof HeadingLevel)[keyof typeof HeadingLevel];
readonly bidirectional?: boolean; readonly bidirectional?: boolean;
readonly pageBreakBefore?: boolean; readonly pageBreakBefore?: boolean;
@ -58,7 +59,6 @@ export type IParagraphPropertiesOptions = {
readonly bullet?: { readonly bullet?: {
readonly level: number; readonly level: number;
}; };
readonly shading?: IShadingAttributesProperties;
readonly widowControl?: boolean; readonly widowControl?: boolean;
readonly frame?: IFrameOptions; readonly frame?: IFrameOptions;
readonly suppressLineNumbers?: boolean; readonly suppressLineNumbers?: boolean;

View File

@ -11,6 +11,7 @@ import { ConcreteHyperlink, ExternalHyperlink, ParagraphChild } from "@file/para
import { TargetModeType } from "@file/relationships/relationship/relationship"; import { TargetModeType } from "@file/relationships/relationship/relationship";
import { IContext } from "@file/xml-components"; import { IContext } from "@file/xml-components";
import { uniqueId } from "@util/convenience-functions"; import { uniqueId } from "@util/convenience-functions";
import { OutputByType, OutputType } from "@util/output-type";
import { appendContentType } from "./content-types-manager"; import { appendContentType } from "./content-types-manager";
import { appendRelationship, getNextRelationshipIndex } from "./relationship-manager"; import { appendRelationship, getNextRelationshipIndex } from "./relationship-manager";
@ -47,21 +48,7 @@ type IHyperlinkRelationshipAddition = {
export type IPatch = ParagraphPatch | FilePatch; export type IPatch = ParagraphPatch | FilePatch;
// From JSZip export type PatchDocumentOutputType = OutputType;
type OutputByType = {
readonly base64: string;
// eslint-disable-next-line id-denylist
readonly string: string;
readonly text: string;
readonly binarystring: string;
readonly array: readonly number[];
readonly uint8array: Uint8Array;
readonly arraybuffer: ArrayBuffer;
readonly blob: Blob;
readonly nodebuffer: Buffer;
};
export type PatchDocumentOutputType = keyof OutputByType;
export type PatchDocumentOptions<T extends PatchDocumentOutputType = PatchDocumentOutputType> = { export type PatchDocumentOptions<T extends PatchDocumentOutputType = PatchDocumentOutputType> = {
readonly outputType: T; readonly outputType: T;

View File

@ -1,2 +1,3 @@
export * from "./convenience-functions"; export * from "./convenience-functions";
export * from "./values"; export * from "./values";
export type * from "./output-type";

18
src/util/output-type.ts Normal file
View File

@ -0,0 +1,18 @@
/* v8 ignore start */
// Simply type definitions. Can ignore testing and coverage
// From JSZip
export type OutputByType = {
readonly base64: string;
// eslint-disable-next-line id-denylist
readonly string: string;
readonly text: string;
readonly binarystring: string;
readonly array: readonly number[];
readonly uint8array: Uint8Array;
readonly arraybuffer: ArrayBuffer;
readonly blob: Blob;
readonly nodebuffer: Buffer;
};
export type OutputType = keyof OutputByType;
/* v8 ignore stop */