docx-kit 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node.d.ts ADDED
@@ -0,0 +1,77 @@
1
+ import { Document } from "docx";
2
+
3
+ //#region src/node/fs.d.ts
4
+ /**
5
+ * Save a compiled document to a file on disk.
6
+ *
7
+ * Uses `Packer.toBuffer()` to produce the .docx binary, then
8
+ * writes via `node:fs/promises`. This function is **Node.js only**.
9
+ *
10
+ * @param doc - — A compiled `docx` `Document` instance
11
+ * @param filename - — Output file path (e.g. `"report.docx"`)
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { saveDocument } from 'docx-kit/node'
16
+ *
17
+ * const doc = await compileDocument({ ... })
18
+ * await saveDocument(doc, 'report.docx')
19
+ * ```
20
+ */
21
+ declare function saveDocument(doc: Document, filename: string): Promise<void>;
22
+ //#endregion
23
+ //#region src/node/dataUrl.d.ts
24
+ /**
25
+ * Node.js base64 data-URL decoder (uses `Buffer` from `node:buffer`).
26
+ *
27
+ * @module node/dataUrl
28
+ */
29
+ /**
30
+ * Decode a base64 data-URL to raw bytes using Node.js `Buffer`.
31
+ *
32
+ * Strips the `"data:*;base64,"` prefix and decodes via
33
+ * `Buffer.from(base64, 'base64')`.
34
+ *
35
+ * @param dataUrl - — A base64 data-URI string (e.g. `"data:image/png;base64,iVBO..."`)
36
+ * @returns Raw bytes as `Uint8Array`
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { dataUrlToUint8Array } from 'docx-kit/node'
41
+ *
42
+ * const bytes = await dataUrlToUint8Array('data:image/png;base64,iVBORw...')
43
+ * ```
44
+ */
45
+ declare function dataUrlToUint8Array(dataUrl: string): Promise<Uint8Array>;
46
+ //#endregion
47
+ //#region src/node.d.ts
48
+ /**
49
+ * ### ❌ `echartsPlugin()` — Not available in Node.js
50
+ *
51
+ * The ECharts plugin requires a browser `window` and `document` to
52
+ * render charts into a DOM container. Node.js has no native DOM.
53
+ *
54
+ * **Workarounds:**
55
+ * - Use a server-side canvas library (e.g. `node-canvas` + `echarts`)
56
+ * - Pre-render charts on the client and pass the image data to docx-kit
57
+ *
58
+ * @deprecated This API is not available in Node.js. See workarounds above.
59
+ */
60
+ declare const echartsPlugin: never;
61
+ /**
62
+ * ### ❌ `normalizeImageData()` — Not built-in for Node.js
63
+ *
64
+ * This utility converts `Blob` instances to `Uint8Array`. While `Blob`
65
+ * is available in Node.js ≥ 18, it is rarely the carrier for image data
66
+ * in Node workflows (most users work with `Buffer` or file paths directly).
67
+ *
68
+ * If you need Blob→Uint8Array in Node.js, use:
69
+ * ```ts
70
+ * const bytes = new Uint8Array(await blob.arrayBuffer())
71
+ * ```
72
+ *
73
+ * @deprecated Use inline `new Uint8Array(await blob.arrayBuffer())` instead.
74
+ */
75
+ declare const normalizeImageData: never;
76
+ //#endregion
77
+ export { dataUrlToUint8Array, echartsPlugin, normalizeImageData, saveDocument };
package/dist/node.js ADDED
@@ -0,0 +1,30 @@
1
+ import { n as saveDocument } from "./fs-DF8ug9Wi.js";
2
+ //#region src/node/dataUrl.ts
3
+ /**
4
+ * Node.js base64 data-URL decoder (uses `Buffer` from `node:buffer`).
5
+ *
6
+ * @module node/dataUrl
7
+ */
8
+ /**
9
+ * Decode a base64 data-URL to raw bytes using Node.js `Buffer`.
10
+ *
11
+ * Strips the `"data:*;base64,"` prefix and decodes via
12
+ * `Buffer.from(base64, 'base64')`.
13
+ *
14
+ * @param dataUrl - — A base64 data-URI string (e.g. `"data:image/png;base64,iVBO..."`)
15
+ * @returns Raw bytes as `Uint8Array`
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { dataUrlToUint8Array } from 'docx-kit/node'
20
+ *
21
+ * const bytes = await dataUrlToUint8Array('data:image/png;base64,iVBORw...')
22
+ * ```
23
+ */
24
+ async function dataUrlToUint8Array(dataUrl) {
25
+ const { Buffer } = await import("node:buffer");
26
+ const base64 = dataUrl.split(",")[1];
27
+ return new Uint8Array(Buffer.from(base64, "base64"));
28
+ }
29
+ //#endregion
30
+ export { dataUrlToUint8Array, saveDocument };
@@ -0,0 +1,79 @@
1
+ import "./fs-DF8ug9Wi.js";
2
+ import { t as DocxKitError } from "./errors-c9CC63Ti.js";
3
+ import { Packer } from "docx";
4
+ //#region src/renderer/pack.ts
5
+ /**
6
+ * Document packaging — converts a `docx` `Document` instance to
7
+ * various output formats (Blob, Buffer, base64, file).
8
+ *
9
+ * These are thin wrappers around the `docx` `Packer` API, with
10
+ * normalization for cross-platform compatibility.
11
+ *
12
+ * @module renderer/pack
13
+ */
14
+ /**
15
+ * Pack a document to a base64-encoded string.
16
+ *
17
+ * @param doc - — A compiled `docx` `Document` instance
18
+ * @returns Base64-encoded .docx data
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const doc = await compileDocument({ ... })
23
+ * const b64 = await packToBase64String(doc)
24
+ * ```
25
+ */
26
+ async function packToBase64String(doc) {
27
+ try {
28
+ return await Packer.toBase64String(doc);
29
+ } catch (err) {
30
+ throw new DocxKitError("EXPORT_FAILED", "Failed to pack document to base64", err);
31
+ }
32
+ }
33
+ /**
34
+ * Pack a document to a `Blob` (browser-friendly).
35
+ *
36
+ * @param doc - — A compiled `docx` `Document` instance
37
+ * @returns A `Blob` containing the .docx binary
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const blob = await packToBlob(doc)
42
+ * const url = URL.createObjectURL(blob)
43
+ * // trigger download in browser
44
+ * ```
45
+ */
46
+ async function packToBlob(doc) {
47
+ try {
48
+ return await Packer.toBlob(doc);
49
+ } catch (err) {
50
+ throw new DocxKitError("EXPORT_FAILED", "Failed to pack document to blob", err);
51
+ }
52
+ }
53
+ /**
54
+ * Pack a document to a `Uint8Array` (browser & Node.js).
55
+ *
56
+ * Normalizes the `Packer.toBuffer()` Node.js Buffer to a
57
+ * standard `Uint8Array` for cross-platform compatibility.
58
+ *
59
+ * @param doc - — A compiled `docx` `Document` instance
60
+ * @returns Raw .docx bytes as `Uint8Array`
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const bytes = await packToBuffer(doc)
65
+ * // In Node.js:
66
+ * import { writeFileSync } from 'node:fs'
67
+ * writeFileSync('output.docx', bytes)
68
+ * ```
69
+ */
70
+ async function packToBuffer(doc) {
71
+ try {
72
+ const buffer = await Packer.toBuffer(doc);
73
+ return new Uint8Array(buffer);
74
+ } catch (err) {
75
+ throw new DocxKitError("EXPORT_FAILED", "Failed to pack document to buffer", err);
76
+ }
77
+ }
78
+ //#endregion
79
+ export { packToBase64String, packToBlob, packToBuffer };
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "docx-kit",
3
3
  "type": "module",
4
- "version": "0.0.0",
5
- "packageManager": "pnpm@10.27.0",
6
- "description": "TypeScript library for generating Word (.docx) documents easily with rich content, styles, and intuitive, customizable API.",
4
+ "version": "0.1.0",
5
+ "description": "CSS-like DOCX API Kit — a type-safe, plugin-extensible DOCX generation library built on dolanmiu/docx.",
7
6
  "keywords": [
8
7
  "document",
9
8
  "docx",
@@ -20,7 +19,10 @@
20
19
  "email": "ntnyq13@gmail.com"
21
20
  },
22
21
  "homepage": "https://github.com/ntnyq/docx-kit#readme",
23
- "repository": "ntnyq/docx-kit",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "ntnyq/docx-kit"
25
+ },
24
26
  "bugs": {
25
27
  "url": "https://github.com/ntnyq/docx-kit/issues"
26
28
  },
@@ -29,6 +31,14 @@
29
31
  ".": {
30
32
  "types": "./dist/index.d.ts",
31
33
  "default": "./dist/index.js"
34
+ },
35
+ "./browser": {
36
+ "types": "./dist/browser.d.ts",
37
+ "default": "./dist/browser.js"
38
+ },
39
+ "./node": {
40
+ "types": "./dist/node.d.ts",
41
+ "default": "./dist/node.js"
32
42
  }
33
43
  },
34
44
  "main": "./dist/index.js",
@@ -40,40 +50,53 @@
40
50
  "access": "public"
41
51
  },
42
52
  "sideEffects": false,
43
- "scripts": {
44
- "build": "tsdown",
45
- "dev": "tsdown --watch",
46
- "lint": "eslint",
47
- "prepare": "husky",
48
- "prepublishOnly": "pnpm run build",
49
- "release": "run-s release:check release:version",
50
- "release:check": "run-s lint typecheck test",
51
- "release:version": "bumpp",
52
- "test": "vitest",
53
- "typecheck": "tsc --noEmit"
53
+ "peerDependencies": {
54
+ "echarts": ">=5.0.0",
55
+ "qrcode": ">=1.5.0"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "echarts": {
59
+ "optional": true
60
+ },
61
+ "qrcode": {
62
+ "optional": true
63
+ }
54
64
  },
55
65
  "dependencies": {
56
- "@ntnyq/utils": "^0.10.0",
57
- "docx": "^9.5.1",
58
- "echarts": "^6.0.0",
59
- "qrcode": "^1.5.4"
66
+ "docx": "^9.7.1"
60
67
  },
61
68
  "devDependencies": {
62
- "@ntnyq/eslint-config": "^5.9.0",
69
+ "@ntnyq/eslint-config": "^6.1.5",
63
70
  "@ntnyq/prettier-config": "^3.0.1",
64
- "@types/node": "^25.0.3",
65
- "bumpp": "^10.3.2",
66
- "eslint": "^9.39.2",
71
+ "@types/node": "^25.9.2",
72
+ "@types/qrcode": "^1.5.6",
73
+ "bumpp": "^11.1.0",
74
+ "echarts": "^6.1.0",
75
+ "eslint": "^10.4.1",
67
76
  "husky": "^9.1.7",
68
- "nano-staged": "^0.9.0",
69
- "npm-run-all2": "^8.0.4",
70
- "prettier": "^3.7.4",
71
- "tsdown": "^0.19.0-beta.2",
72
- "typescript": "^5.9.3",
73
- "vitest": "^4.0.16"
77
+ "nano-staged": "^1.0.2",
78
+ "npm-run-all2": "^9.0.1",
79
+ "prettier": "^3.8.4",
80
+ "qrcode": "^1.5.4",
81
+ "tsdown": "^0.22.2",
82
+ "typescript": "^6.0.3",
83
+ "vite": "^8.0.16",
84
+ "vitest": "^4.1.8"
74
85
  },
75
86
  "nano-staged": {
76
87
  "*.{js,ts,mjs,cjs,md,vue,yml,yaml,toml,json}": "eslint --fix",
77
88
  "*.{css,scss,html}": "prettier -uw"
89
+ },
90
+ "scripts": {
91
+ "build": "tsdown",
92
+ "dev": "tsdown --watch",
93
+ "docs:build": "pnpm -C docs run build",
94
+ "docs:dev": "pnpm -C docs run dev",
95
+ "lint": "eslint",
96
+ "release": "run-s release:check release:version",
97
+ "release:check": "run-s lint typecheck test",
98
+ "release:version": "bumpp",
99
+ "test": "vitest",
100
+ "typecheck": "tsc --noEmit"
78
101
  }
79
- }
102
+ }