docx-kit 0.0.0 → 0.2.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/README.md +520 -1
- package/dist/browser.d.ts +1836 -0
- package/dist/browser.js +2386 -0
- package/dist/compileDocument-DIFdjo67.mjs +1051 -0
- package/dist/compileDocument-Vy7rz9_8.js +1051 -0
- package/dist/errors-c9CC63Ti.js +63 -0
- package/dist/errors-c9CC63Ti.mjs +63 -0
- package/dist/fs-BuKdtuxB.js +30 -0
- package/dist/fs-DF8ug9Wi.mjs +43 -0
- package/dist/image-CdfLh9GO.js +76 -0
- package/dist/image-CdfLh9GO.mjs +76 -0
- package/dist/node.d.mts +1817 -0
- package/dist/node.mjs +2362 -0
- package/dist/pack-Bkdfq0_u.mjs +79 -0
- package/dist/pack-u3tMQYCL.js +79 -0
- package/package.json +61 -34
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -9
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error handling types and classes for docx-kit.
|
|
4
|
+
*
|
|
5
|
+
* @module errors
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Well-known error codes used throughout the library.
|
|
9
|
+
*
|
|
10
|
+
* Consumers can match against these codes for structured error handling.
|
|
11
|
+
*/
|
|
12
|
+
const ERROR_CODES = {
|
|
13
|
+
/** Document export failed. */
|
|
14
|
+
EXPORT_FAILED: "EXPORT_FAILED",
|
|
15
|
+
/** Image data is empty, corrupt, or unsupported. */
|
|
16
|
+
IMAGE_INVALID_DATA: "IMAGE_INVALID_DATA",
|
|
17
|
+
/** A plugin node referenced an unregistered plugin. */
|
|
18
|
+
PLUGIN_NOT_REGISTERED: "PLUGIN_NOT_REGISTERED",
|
|
19
|
+
/** A plugin's `render()` method threw an error. */
|
|
20
|
+
PLUGIN_RENDER_FAILED: "PLUGIN_RENDER_FAILED",
|
|
21
|
+
/** A `className` referenced a stylesheet key that doesn't exist. */
|
|
22
|
+
STYLE_UNKNOWN_CLASS: "STYLE_UNKNOWN_CLASS",
|
|
23
|
+
/** Table was created with no columns. */
|
|
24
|
+
TABLE_INVALID_COLUMNS: "TABLE_INVALID_COLUMNS",
|
|
25
|
+
/** Encountered a node type that has no registered compiler. */
|
|
26
|
+
UNKNOWN_NODE_TYPE: "UNKNOWN_NODE_TYPE"
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Structured error class for docx-kit.
|
|
30
|
+
*
|
|
31
|
+
* Carries a machine-readable `code`, a human-readable `message`,
|
|
32
|
+
* and optionally the underlying `cause`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* try {
|
|
37
|
+
* await doc.save('output.docx')
|
|
38
|
+
* } catch (err) {
|
|
39
|
+
* if (err instanceof DocxKitError && err.code === ERROR_CODES.EXPORT_FAILED) {
|
|
40
|
+
* console.error('Export failed:', err.message)
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
var DocxKitError = class extends Error {
|
|
46
|
+
/** The underlying error that caused this failure (if any). */
|
|
47
|
+
cause;
|
|
48
|
+
/** Machine-readable error code. */
|
|
49
|
+
code;
|
|
50
|
+
/**
|
|
51
|
+
* @param code - — Known error code from {@link ERROR_CODES}
|
|
52
|
+
* @param message - — Human-readable description
|
|
53
|
+
* @param cause - — Optional underlying error
|
|
54
|
+
*/
|
|
55
|
+
constructor(code, message, cause) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = "DocxKitError";
|
|
58
|
+
this.code = code;
|
|
59
|
+
this.cause = cause;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
63
|
+
export { ERROR_CODES as n, DocxKitError as t };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error handling types and classes for docx-kit.
|
|
4
|
+
*
|
|
5
|
+
* @module errors
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Well-known error codes used throughout the library.
|
|
9
|
+
*
|
|
10
|
+
* Consumers can match against these codes for structured error handling.
|
|
11
|
+
*/
|
|
12
|
+
const ERROR_CODES = {
|
|
13
|
+
/** Document export failed. */
|
|
14
|
+
EXPORT_FAILED: "EXPORT_FAILED",
|
|
15
|
+
/** Image data is empty, corrupt, or unsupported. */
|
|
16
|
+
IMAGE_INVALID_DATA: "IMAGE_INVALID_DATA",
|
|
17
|
+
/** A plugin node referenced an unregistered plugin. */
|
|
18
|
+
PLUGIN_NOT_REGISTERED: "PLUGIN_NOT_REGISTERED",
|
|
19
|
+
/** A plugin's `render()` method threw an error. */
|
|
20
|
+
PLUGIN_RENDER_FAILED: "PLUGIN_RENDER_FAILED",
|
|
21
|
+
/** A `className` referenced a stylesheet key that doesn't exist. */
|
|
22
|
+
STYLE_UNKNOWN_CLASS: "STYLE_UNKNOWN_CLASS",
|
|
23
|
+
/** Table was created with no columns. */
|
|
24
|
+
TABLE_INVALID_COLUMNS: "TABLE_INVALID_COLUMNS",
|
|
25
|
+
/** Encountered a node type that has no registered compiler. */
|
|
26
|
+
UNKNOWN_NODE_TYPE: "UNKNOWN_NODE_TYPE"
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Structured error class for docx-kit.
|
|
30
|
+
*
|
|
31
|
+
* Carries a machine-readable `code`, a human-readable `message`,
|
|
32
|
+
* and optionally the underlying `cause`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* try {
|
|
37
|
+
* await doc.save('output.docx')
|
|
38
|
+
* } catch (err) {
|
|
39
|
+
* if (err instanceof DocxKitError && err.code === ERROR_CODES.EXPORT_FAILED) {
|
|
40
|
+
* console.error('Export failed:', err.message)
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
var DocxKitError = class extends Error {
|
|
46
|
+
/** The underlying error that caused this failure (if any). */
|
|
47
|
+
cause;
|
|
48
|
+
/** Machine-readable error code. */
|
|
49
|
+
code;
|
|
50
|
+
/**
|
|
51
|
+
* @param code - — Known error code from {@link ERROR_CODES}
|
|
52
|
+
* @param message - — Human-readable description
|
|
53
|
+
* @param cause - — Optional underlying error
|
|
54
|
+
*/
|
|
55
|
+
constructor(code, message, cause) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = "DocxKitError";
|
|
58
|
+
this.code = code;
|
|
59
|
+
this.cause = cause;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
63
|
+
export { ERROR_CODES as n, DocxKitError as t };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Packer } from "docx";
|
|
2
|
+
//#region src/node/fs.ts
|
|
3
|
+
/**
|
|
4
|
+
* Node.js filesystem utilities for saving documents to disk.
|
|
5
|
+
*
|
|
6
|
+
* @module node/fs
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Save a compiled document to a file on disk.
|
|
10
|
+
*
|
|
11
|
+
* Uses `Packer.toBuffer()` to produce the .docx binary, then
|
|
12
|
+
* writes via `node:fs/promises`. This function is **Node.js only**.
|
|
13
|
+
*
|
|
14
|
+
* @param doc - — A compiled `docx` `Document` instance
|
|
15
|
+
* @param filename - — Output file path (e.g. `"report.docx"`)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { saveDocument } from 'docx-kit/node'
|
|
20
|
+
*
|
|
21
|
+
* const doc = await compileDocument({ ... })
|
|
22
|
+
* await saveDocument(doc, 'report.docx')
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
async function saveDocument(doc, filename) {
|
|
26
|
+
const { writeFile } = await import("node:fs/promises");
|
|
27
|
+
await writeFile(filename, await Packer.toBuffer(doc));
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { saveDocument };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Packer } from "docx";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __exportAll = (all, no_symbols) => {
|
|
5
|
+
let target = {};
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
11
|
+
return target;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/node/fs.ts
|
|
15
|
+
/**
|
|
16
|
+
* Node.js filesystem utilities for saving documents to disk.
|
|
17
|
+
*
|
|
18
|
+
* @module node/fs
|
|
19
|
+
*/
|
|
20
|
+
var fs_exports = /* @__PURE__ */ __exportAll({ saveDocument: () => saveDocument });
|
|
21
|
+
/**
|
|
22
|
+
* Save a compiled document to a file on disk.
|
|
23
|
+
*
|
|
24
|
+
* Uses `Packer.toBuffer()` to produce the .docx binary, then
|
|
25
|
+
* writes via `node:fs/promises`. This function is **Node.js only**.
|
|
26
|
+
*
|
|
27
|
+
* @param doc - — A compiled `docx` `Document` instance
|
|
28
|
+
* @param filename - — Output file path (e.g. `"report.docx"`)
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { saveDocument } from 'docx-kit/node'
|
|
33
|
+
*
|
|
34
|
+
* const doc = await compileDocument({ ... })
|
|
35
|
+
* await saveDocument(doc, 'report.docx')
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
async function saveDocument(doc, filename) {
|
|
39
|
+
const { writeFile } = await import("node:fs/promises");
|
|
40
|
+
await writeFile(filename, await Packer.toBuffer(doc));
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { saveDocument as n, fs_exports as t };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ImageRun } from "docx";
|
|
2
|
+
//#region src/utils/dataUrl.ts
|
|
3
|
+
/**
|
|
4
|
+
* Cross-platform base64 data-URL decoder.
|
|
5
|
+
*
|
|
6
|
+
* Auto-detects the runtime environment and uses the appropriate
|
|
7
|
+
* implementation:
|
|
8
|
+
* - **Node.js**: `Buffer.from(base64, 'base64')`
|
|
9
|
+
* - **Browser**: `atob(base64)` + manual byte population
|
|
10
|
+
*
|
|
11
|
+
* This is the internal shared version used by the compiler and
|
|
12
|
+
* built-in plugins. For platform-specific direct access, import
|
|
13
|
+
* from `'docx-kit/node'` or `'docx-kit/browser'`.
|
|
14
|
+
*
|
|
15
|
+
* @module utils/dataUrl
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Decode a base64 data-URL to raw bytes (works in both browser & Node.js).
|
|
19
|
+
*
|
|
20
|
+
* Strips the `"data:*;base64,"` prefix and decodes using the appropriate
|
|
21
|
+
* runtime API.
|
|
22
|
+
*
|
|
23
|
+
* @param dataUrl - — A base64 data-URI string (e.g. `"data:image/png;base64,iVBO..."`)
|
|
24
|
+
* @returns Raw bytes as `Uint8Array`
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { dataUrlToUint8Array } from 'docx-kit'
|
|
29
|
+
*
|
|
30
|
+
* const bytes = await dataUrlToUint8Array('data:image/png;base64,iVBORw...')
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
async function dataUrlToUint8Array(dataUrl) {
|
|
34
|
+
const base64 = dataUrl.split(",")[1];
|
|
35
|
+
if (typeof atob === "function") {
|
|
36
|
+
const binary = atob(base64);
|
|
37
|
+
const bytes = new Uint8Array(binary.length);
|
|
38
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
39
|
+
return bytes;
|
|
40
|
+
}
|
|
41
|
+
const { Buffer } = await import("node:buffer");
|
|
42
|
+
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/utils/image.ts
|
|
46
|
+
/**
|
|
47
|
+
* Shared image utilities for internal use by compilers and plugins.
|
|
48
|
+
*
|
|
49
|
+
* Bridges the `docx` library's `IImageOptions` with `docx-kit`'s
|
|
50
|
+
* own `CreateImageRunOptions`, confining type casts to this single module.
|
|
51
|
+
*
|
|
52
|
+
* @module utils/image
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* Create an `ImageRun` with type-safe defaults.
|
|
56
|
+
*
|
|
57
|
+
* All paths that construct an `ImageRun` should use this factory
|
|
58
|
+
* instead of calling `new ImageRun(...)` directly, ensuring that
|
|
59
|
+
* type casts are confined to a single audited function.
|
|
60
|
+
*
|
|
61
|
+
* @param options - — Image data and layout options
|
|
62
|
+
* @returns A configured `ImageRun` instance
|
|
63
|
+
*/
|
|
64
|
+
function createImageRun(options) {
|
|
65
|
+
return new ImageRun({
|
|
66
|
+
data: options.data,
|
|
67
|
+
floating: options.floating,
|
|
68
|
+
type: options.type,
|
|
69
|
+
transformation: {
|
|
70
|
+
height: options.height ?? 180,
|
|
71
|
+
width: options.width ?? 300
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { dataUrlToUint8Array as n, createImageRun as t };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ImageRun } from "docx";
|
|
2
|
+
//#region src/utils/dataUrl.ts
|
|
3
|
+
/**
|
|
4
|
+
* Cross-platform base64 data-URL decoder.
|
|
5
|
+
*
|
|
6
|
+
* Auto-detects the runtime environment and uses the appropriate
|
|
7
|
+
* implementation:
|
|
8
|
+
* - **Node.js**: `Buffer.from(base64, 'base64')`
|
|
9
|
+
* - **Browser**: `atob(base64)` + manual byte population
|
|
10
|
+
*
|
|
11
|
+
* This is the internal shared version used by the compiler and
|
|
12
|
+
* built-in plugins. For platform-specific direct access, import
|
|
13
|
+
* from `'docx-kit/node'` or `'docx-kit/browser'`.
|
|
14
|
+
*
|
|
15
|
+
* @module utils/dataUrl
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Decode a base64 data-URL to raw bytes (works in both browser & Node.js).
|
|
19
|
+
*
|
|
20
|
+
* Strips the `"data:*;base64,"` prefix and decodes using the appropriate
|
|
21
|
+
* runtime API.
|
|
22
|
+
*
|
|
23
|
+
* @param dataUrl - — A base64 data-URI string (e.g. `"data:image/png;base64,iVBO..."`)
|
|
24
|
+
* @returns Raw bytes as `Uint8Array`
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { dataUrlToUint8Array } from 'docx-kit'
|
|
29
|
+
*
|
|
30
|
+
* const bytes = await dataUrlToUint8Array('data:image/png;base64,iVBORw...')
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
async function dataUrlToUint8Array(dataUrl) {
|
|
34
|
+
const base64 = dataUrl.split(",")[1];
|
|
35
|
+
if (typeof atob === "function") {
|
|
36
|
+
const binary = atob(base64);
|
|
37
|
+
const bytes = new Uint8Array(binary.length);
|
|
38
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
39
|
+
return bytes;
|
|
40
|
+
}
|
|
41
|
+
const { Buffer } = await import("node:buffer");
|
|
42
|
+
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/utils/image.ts
|
|
46
|
+
/**
|
|
47
|
+
* Shared image utilities for internal use by compilers and plugins.
|
|
48
|
+
*
|
|
49
|
+
* Bridges the `docx` library's `IImageOptions` with `docx-kit`'s
|
|
50
|
+
* own `CreateImageRunOptions`, confining type casts to this single module.
|
|
51
|
+
*
|
|
52
|
+
* @module utils/image
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* Create an `ImageRun` with type-safe defaults.
|
|
56
|
+
*
|
|
57
|
+
* All paths that construct an `ImageRun` should use this factory
|
|
58
|
+
* instead of calling `new ImageRun(...)` directly, ensuring that
|
|
59
|
+
* type casts are confined to a single audited function.
|
|
60
|
+
*
|
|
61
|
+
* @param options - — Image data and layout options
|
|
62
|
+
* @returns A configured `ImageRun` instance
|
|
63
|
+
*/
|
|
64
|
+
function createImageRun(options) {
|
|
65
|
+
return new ImageRun({
|
|
66
|
+
data: options.data,
|
|
67
|
+
floating: options.floating,
|
|
68
|
+
type: options.type,
|
|
69
|
+
transformation: {
|
|
70
|
+
height: options.height ?? 180,
|
|
71
|
+
width: options.width ?? 300
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { dataUrlToUint8Array as n, createImageRun as t };
|