Merge branch 'master' of github.com:dolanmiu/docx

This commit is contained in:
Dolan Miu
2023-08-06 20:10:11 +01:00
9 changed files with 1034 additions and 1084 deletions

View File

@ -12,7 +12,6 @@
[![Downloads per month][downloads-image]][downloads-url] [![Downloads per month][downloads-image]][downloads-url]
[![GitHub Action Workflow Status][github-actions-workflow-image]][github-actions-workflow-url] [![GitHub Action Workflow Status][github-actions-workflow-image]][github-actions-workflow-url]
[![Known Vulnerabilities][snky-image]][snky-url] [![Known Vulnerabilities][snky-image]][snky-url]
[![Chat on Gitter][gitter-image]][gitter-url]
[![PRs Welcome][pr-image]][pr-url] [![PRs Welcome][pr-image]][pr-url]
[![codecov][codecov-image]][codecov-url] [![codecov][codecov-image]][codecov-url]
@ -107,8 +106,6 @@ Made with 💖
[github-actions-workflow-url]: https://github.com/dolanmiu/docx/actions [github-actions-workflow-url]: https://github.com/dolanmiu/docx/actions
[snky-image]: https://snyk.io/test/github/dolanmiu/docx/badge.svg [snky-image]: https://snyk.io/test/github/dolanmiu/docx/badge.svg
[snky-url]: https://snyk.io/test/github/dolanmiu/docx [snky-url]: https://snyk.io/test/github/dolanmiu/docx
[gitter-image]: https://badges.gitter.im/dolanmiu/docx.svg
[gitter-url]: https://gitter.im/docx-lib/Lobby
[pr-image]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg [pr-image]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg
[pr-url]: http://makeapullrequest.com [pr-url]: http://makeapullrequest.com
[codecov-image]: https://codecov.io/gh/dolanmiu/docx/branch/master/graph/badge.svg [codecov-image]: https://codecov.io/gh/dolanmiu/docx/branch/master/graph/badge.svg

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!doctype html>
<html> <html>
<head> <head>
<script src="../build/index.umd.cjs"></script> <script src="../build/index.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
</head> </head>

View File

@ -5,9 +5,9 @@ import inquirer from "inquirer";
import { $ } from "execa"; import { $ } from "execa";
export type Answers = { export type Answers = {
type: "list" | "number"; readonly type: "list" | "number";
demoNumber?: number; readonly demoNumber?: number;
demoFile?: number; readonly demoFile?: number;
}; };
const dir = "./demo"; const dir = "./demo";
@ -15,8 +15,7 @@ const fileNames = fs.readdirSync(dir);
const keys = fileNames.map((f) => path.parse(f).name); const keys = fileNames.map((f) => path.parse(f).name);
const getFileNumber = (file: string): number => { const getFileNumber = (file: string): number => {
const nameParts = file.split("-"); const [firstPart] = file.split("-");
const firstPart = nameParts[0];
return Number(firstPart); return Number(firstPart);
}; };
@ -35,15 +34,15 @@ const answers = await inquirer.prompt<Answers>([
name: "demoFile", name: "demoFile",
message: "What demo do you wish to run?", message: "What demo do you wish to run?",
choices: demoFiles, choices: demoFiles,
filter: (input) => parseInt(input.split("-")[0]), filter: (input) => parseInt(input.split("-")[0], 10),
when: (answers) => answers.type === "list", when: (a) => a.type === "list",
}, },
{ {
type: "number", type: "number",
name: "demoNumber", name: "demoNumber",
message: "What demo do you wish to run? (Enter a number)", message: "What demo do you wish to run? (Enter a number)",
default: 1, default: 1,
when: (answers) => answers.type === "number", when: (a) => a.type === "number",
}, },
]); ]);
@ -56,6 +55,7 @@ if (files.length === 0) {
const filePath = path.join(dir, files[0]); const filePath = path.join(dir, files[0]);
console.log(`Running demo ${demoNumber}: ${files[0]}`); console.log(`Running demo ${demoNumber}: ${files[0]}`);
await $`ts-node --project demo/tsconfig.json ${filePath}`; const { stdout } = await $`ts-node --project demo/tsconfig.json ${filePath}`;
console.log(stdout);
console.log("Successfully created document!"); console.log("Successfully created document!");
} }

2022
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,13 @@
{ {
"name": "docx", "name": "docx",
"version": "8.2.0", "version": "8.2.2",
"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": "build/index.umd.cjs", "main": "build/index.umd.js",
"module": "./build/index.js", "module": "./build/index.js",
"types": "./build/index.d.ts", "types": "./build/index.d.ts",
"exports": { "exports": {
".": { ".": {
"browser": {
"default": "./build/index.umd.cjs"
},
"require": "./build/index.cjs", "require": "./build/index.cjs",
"types": "./build/index.d.ts", "types": "./build/index.d.ts",
"import": "./build/index.js", "import": "./build/index.js",
@ -58,7 +55,6 @@
], ],
"dependencies": { "dependencies": {
"@types/node": "^20.3.1", "@types/node": "^20.3.1",
"fflate": "^0.8.0",
"jszip": "^3.10.1", "jszip": "^3.10.1",
"nanoid": "^4.0.2", "nanoid": "^4.0.2",
"xml": "^1.0.1", "xml": "^1.0.1",
@ -78,8 +74,8 @@
"@types/xml": "^1.0.8", "@types/xml": "^1.0.8",
"@typescript-eslint/eslint-plugin": "^5.36.1", "@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1", "@typescript-eslint/parser": "^5.36.1",
"@vitest/coverage-v8": "^0.32.0", "@vitest/coverage-v8": "^0.33.0",
"@vitest/ui": "^0.32.0", "@vitest/ui": "^0.33.0",
"cspell": "^6.2.2", "cspell": "^6.2.2",
"docsify-cli": "^4.3.0", "docsify-cli": "^4.3.0",
"eslint": "^8.23.0", "eslint": "^8.23.0",
@ -94,17 +90,17 @@
"inquirer": "^9.2.7", "inquirer": "^9.2.7",
"jsdom": "^22.1.0", "jsdom": "^22.1.0",
"pre-commit": "^1.2.2", "pre-commit": "^1.2.2",
"prettier": "^2.3.1", "prettier": "^3.0.0",
"ts-node": "^10.2.1", "ts-node": "^10.2.1",
"tsconfig-paths": "^4.0.0", "tsconfig-paths": "^4.0.0",
"typedoc": "^0.24.8", "typedoc": "^0.24.8",
"typescript": "5.1.3", "typescript": "5.1.6",
"unzipper": "^0.10.11", "unzipper": "^0.10.11",
"vite": "^4.3.2", "vite": "^4.3.2",
"vite-plugin-dts": "^2.3.0", "vite-plugin-dts": "^3.3.1",
"vite-plugin-node-polyfills": "^0.9.0", "vite-plugin-node-polyfills": "^0.9.0",
"vite-tsconfig-paths": "^4.2.0", "vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.32.0" "vitest": "^0.33.0"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=10"

View File

@ -17,7 +17,11 @@ export class FooterWrapper implements IViewWrapper {
private readonly footer: Footer; private readonly footer: Footer;
private readonly relationships: Relationships; private readonly relationships: Relationships;
public constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) { public constructor(
private readonly media: Media,
referenceId: number,
initContent?: XmlComponent,
) {
this.footer = new Footer(referenceId, initContent); this.footer = new Footer(referenceId, initContent);
this.relationships = new Relationships(); this.relationships = new Relationships();
} }

View File

@ -17,7 +17,11 @@ export class HeaderWrapper implements IViewWrapper {
private readonly header: Header; private readonly header: Header;
private readonly relationships: Relationships; private readonly relationships: Relationships;
public constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) { public constructor(
private readonly media: Media,
referenceId: number,
initContent?: XmlComponent,
) {
this.header = new Header(referenceId, initContent); this.header = new Header(referenceId, initContent);
this.relationships = new Relationships(); this.relationships = new Relationships();
} }

View File

@ -53,7 +53,7 @@ export interface PatchDocumentOptions {
const imageReplacer = new ImageReplacer(); const imageReplacer = new ImageReplacer();
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => { export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Uint8Array> => {
const zipContent = await JSZip.loadAsync(data); const zipContent = await JSZip.loadAsync(data);
const contexts = new Map<string, IContext>(); const contexts = new Map<string, IContext>();
const file = { const file = {
@ -68,11 +68,11 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
const hyperlinkRelationshipAdditions: IHyperlinkRelationshipAddition[] = []; const hyperlinkRelationshipAdditions: IHyperlinkRelationshipAddition[] = [];
let hasMedia = false; let hasMedia = false;
const binaryContentMap = new Map<string, Buffer>(); const binaryContentMap = new Map<string, Uint8Array>();
for (const [key, value] of Object.entries(zipContent.files)) { for (const [key, value] of Object.entries(zipContent.files)) {
if (!key.endsWith(".xml") && !key.endsWith(".rels")) { if (!key.endsWith(".xml") && !key.endsWith(".rels")) {
binaryContentMap.set(key, await value.async("nodebuffer")); binaryContentMap.set(key, await value.async("uint8array"));
continue; continue;
} }
@ -213,7 +213,7 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
} }
return zip.generateAsync({ return zip.generateAsync({
type: "nodebuffer", type: "uint8array",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE", compression: "DEFLATE",
}); });

View File

@ -9,7 +9,10 @@ export default defineConfig({
tsconfigPaths(), tsconfigPaths(),
dts(), dts(),
nodePolyfills({ nodePolyfills({
exclude: ["fs"], exclude: [],
globals: {
Buffer: false,
},
protocolImports: true, protocolImports: true,
}), }),
], ],
@ -26,7 +29,25 @@ export default defineConfig({
lib: { lib: {
entry: [resolve(__dirname, "src/index.ts")], entry: [resolve(__dirname, "src/index.ts")],
name: "docx", name: "docx",
fileName: "index", fileName: (d) => {
if (d === "umd") {
return "index.umd.js";
}
if (d === "cjs") {
return "index.cjs";
}
if (d === "es") {
return "index.js";
}
if (d === "iife") {
return "index.iife.js";
}
return "unknown";
},
formats: ["iife", "es", "cjs", "umd"], formats: ["iife", "es", "cjs", "umd"],
}, },
outDir: resolve(__dirname, "build"), outDir: resolve(__dirname, "build"),