docx 9.9.0 → 9.11.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 +15 -0
- package/dist/html.cjs +14192 -0
- package/dist/html.d.cts +152 -0
- package/dist/html.d.ts +152 -0
- package/dist/html.iife.js +14193 -0
- package/dist/html.mjs +14190 -0
- package/dist/html.umd.cjs +14198 -0
- package/dist/index.cjs +729 -173
- package/dist/index.d.cts +194 -21
- package/dist/index.d.ts +194 -21
- package/dist/index.iife.js +729 -173
- package/dist/index.mjs +729 -173
- package/dist/index.umd.cjs +729 -173
- package/dist/layout.cjs +1 -6
- package/dist/layout.iife.js +1 -6
- package/dist/layout.mjs +1 -6
- package/dist/layout.umd.cjs +1 -6
- package/package.json +24 -5
package/dist/html.d.cts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { Paragraph } from 'docx';
|
|
2
|
+
import type { ParagraphChild } from 'docx';
|
|
3
|
+
import type { Table } from 'docx';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An image's data, or an object with it and, for an SVG, a PNG of it, which Word shows where it can't show SVGs. An
|
|
7
|
+
* SVG given without one is shown as nothing there.
|
|
8
|
+
*/
|
|
9
|
+
export declare type HtmlImage = HtmlImageData | {
|
|
10
|
+
readonly data: HtmlImageData;
|
|
11
|
+
readonly fallback?: HtmlImageData;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The options of `htmlToContent` and `htmlToRuns`.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
/** An image's data: its bytes, as a Uint8Array (such as a Node.js Buffer) or an ArrayBuffer, or base64, or a data URL */
|
|
20
|
+
export declare type HtmlImageData = Uint8Array | ArrayBuffer | string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for turning HTML into a document's content.
|
|
24
|
+
*/
|
|
25
|
+
export declare type HtmlOptions = {
|
|
26
|
+
/**
|
|
27
|
+
* CSS for the HTML, as if it were in a `<style>` element after the HTML's own, such as the style sheet of the
|
|
28
|
+
* editor it comes from. Rules in `@media print` blocks are used, and those in other `@media` blocks aren't.
|
|
29
|
+
*/
|
|
30
|
+
readonly stylesheet?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The Word styles to give the elements that CSS selectors match, by the styles' ids, such as
|
|
33
|
+
* `{ "p.lead": "Lead", "blockquote": "Quote", "code": "InlineCode", "table": "GridTable4" }`. A block is given a
|
|
34
|
+
* paragraph style, an inline element a character style, and a table a table style. When selectors that match an
|
|
35
|
+
* element give it different styles, the most specific selector's is used, as in CSS. The styles must be in the
|
|
36
|
+
* document, such as in its `styles` option or the template it patches.
|
|
37
|
+
*/
|
|
38
|
+
readonly styleMap?: Readonly<Record<string, string>>;
|
|
39
|
+
/**
|
|
40
|
+
* The images that `<img src>` refers to by addresses other than data URLs, as docx/html doesn't download anything:
|
|
41
|
+
* an object of each address and its image, or a function that gives the image for an address, or undefined when it
|
|
42
|
+
* has none. An address is looked for as it is written, and then resolved against `baseUrl`.
|
|
43
|
+
*/
|
|
44
|
+
readonly images?: Readonly<Record<string, HtmlImage>> | ((source: string) => HtmlImage | undefined);
|
|
45
|
+
/**
|
|
46
|
+
* The widest an image can be, in pixels, so it fits on the page: 601 by default, the width of the text on the page
|
|
47
|
+
* of a document with docx's default page size and margins. Wider images are scaled down.
|
|
48
|
+
*/
|
|
49
|
+
readonly maxImageWidth?: number;
|
|
50
|
+
/** The address that relative links and image addresses are relative to, such as `https://example.com/blog/` */
|
|
51
|
+
readonly baseUrl?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Turns `<ins>` and `<del>`, such as an HTML diff's, into Word's tracked changes, by this author, and at the time
|
|
54
|
+
* of their `datetime` attribute, or this time, or now: an ISO 8601 date and time such as
|
|
55
|
+
* `2026-10-13T09:30:00Z`. Without it, they are underlined and struck through, as a browser shows them.
|
|
56
|
+
*/
|
|
57
|
+
readonly trackChanges?: {
|
|
58
|
+
readonly author: string;
|
|
59
|
+
readonly date?: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* What the HTML's notes are in Word, such as the footnotes of Markdown, Pandoc and Google Docs: `"footnotes"`, at
|
|
63
|
+
* the foot of the page each is referred to on, by default, or `"endnotes"`, at the end of the document, as a web
|
|
64
|
+
* page has them. Word's own HTML says which of its notes are footnotes and which are endnotes, which they stay.
|
|
65
|
+
*/
|
|
66
|
+
readonly notes?: "footnotes" | "endnotes";
|
|
67
|
+
/** Called with each thing in the HTML that the document leaves out or has differently */
|
|
68
|
+
readonly onWarning?: (warning: HtmlWarning) => void;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Turns HTML into paragraphs and tables, to go in a document's sections, headers, footers, table cells and anywhere
|
|
73
|
+
* else they go, among others, or to patch a template with.
|
|
74
|
+
*
|
|
75
|
+
* It reads what rich-text editors write, such as TinyMCE, CKEditor, Quill, TipTap and Lexical, and what Word and
|
|
76
|
+
* Google Docs put on the clipboard, as a browser reads it: its `<style>` elements and `style` attributes, the styles
|
|
77
|
+
* of the classes those editors write, and HTML's own meanings, such as `<b>`'s bold. Paragraphs, headings, lists and
|
|
78
|
+
* links are in the Word document's own styles, such as Heading 1 and Hyperlink, with only what the HTML says, such as
|
|
79
|
+
* a colour or a font size, written as direct formatting.
|
|
80
|
+
*
|
|
81
|
+
* It has headings, paragraphs and line breaks; bold, italic, underlined, struck through, superscript and subscript
|
|
82
|
+
* text, its colours, backgrounds, fonts, sizes and spacing; alignment, indents, line spacing, borders and shading of
|
|
83
|
+
* paragraphs; lists, numbered or not, in each other, with their numbers' styles and starts; tables, with merged cells,
|
|
84
|
+
* header rows, widths, borders, backgrounds and alignment; images, with their sizes and alt text, floating or not;
|
|
85
|
+
* links, within the document too; checkboxes; math, as Word's equations; page breaks; and tracked changes.
|
|
86
|
+
*
|
|
87
|
+
* Nothing is downloaded: images that aren't data URLs are given with the `images` option. HTML that isn't valid is
|
|
88
|
+
* read as a browser reads it, and what the document can't have, such as a video, is left out, with a warning to
|
|
89
|
+
* `onWarning`. It never throws for the HTML it is given.
|
|
90
|
+
*
|
|
91
|
+
* @param html - The HTML: some of a document's body, such as an editor's content, or a whole document
|
|
92
|
+
* @param options - Options, such as the images and CSS of the HTML
|
|
93
|
+
* @returns The paragraphs and tables
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* import { Document, Packer } from "docx";
|
|
98
|
+
* import { htmlToContent } from "docx/html";
|
|
99
|
+
*
|
|
100
|
+
* const doc = new Document({
|
|
101
|
+
* sections: [{ children: htmlToContent("<h1>Report</h1><p>The <b>results</b> are in.</p>") }],
|
|
102
|
+
* });
|
|
103
|
+
* const buffer = await Packer.toBuffer(doc);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare const htmlToContent: (html: string, options?: HtmlOptions) => readonly (Paragraph | Table)[];
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Turns HTML into the content of a paragraph: runs of text, hyperlinks, images and the rest, such as for a paragraph
|
|
110
|
+
* of a document's own, or to patch a paragraph of a template with.
|
|
111
|
+
*
|
|
112
|
+
* It reads HTML as `htmlToContent` does. Paragraphs in the HTML are lines of the paragraph, and the content of tables
|
|
113
|
+
* and lists is kept, without the tables and lists, with a warning, as a paragraph can't have them.
|
|
114
|
+
*
|
|
115
|
+
* @param html - The HTML, such as `"<b>Due</b> on <i>Friday</i>"`
|
|
116
|
+
* @param options - Options, such as the images and CSS of the HTML
|
|
117
|
+
* @returns The paragraph's content
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* new Paragraph({ children: htmlToRuns("Total: <b>£120</b>") });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export declare const htmlToRuns: (html: string, options?: HtmlOptions) => readonly ParagraphChild[];
|
|
125
|
+
|
|
126
|
+
/** Something in the HTML that the document leaves out or has differently */
|
|
127
|
+
export declare type HtmlWarning = {
|
|
128
|
+
readonly code: HtmlWarningCode;
|
|
129
|
+
/** What it is, in English, such as `The image "photo.webp" is WebP, which Word can't show, so it is left out` */
|
|
130
|
+
readonly message: string;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/** What kind of thing a warning is about */
|
|
134
|
+
export declare type HtmlWarningCode =
|
|
135
|
+
/** An element the document can't have, such as `<video>` or `<iframe>`, which is left out */
|
|
136
|
+
"unsupported-element"
|
|
137
|
+
/** An image whose data isn't in the HTML or the `images` option, which is left out */
|
|
138
|
+
| "missing-image"
|
|
139
|
+
/** An image in a format Word can't show, such as WebP, or data that isn't an image, which is left out */
|
|
140
|
+
| "unsupported-image"
|
|
141
|
+
/** A link to a `javascript:` address, or another that could run code, whose text is kept without the link */
|
|
142
|
+
| "unsafe-link"
|
|
143
|
+
/** Math that can't be read, whose text is kept */
|
|
144
|
+
| "invalid-math"
|
|
145
|
+
/** Content in more than a thousand elements in each other, whose text is kept without its formatting */
|
|
146
|
+
| "too-deep"
|
|
147
|
+
/** A table, list or other block in HTML given to `htmlToRuns`, whose content is kept in the paragraph */
|
|
148
|
+
| "block-in-paragraph"
|
|
149
|
+
/** A table in a note, whose cells' paragraphs are kept in the footnote or endnote without it */
|
|
150
|
+
| "table-in-note";
|
|
151
|
+
|
|
152
|
+
export { }
|
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { Paragraph } from 'docx';
|
|
2
|
+
import type { ParagraphChild } from 'docx';
|
|
3
|
+
import type { Table } from 'docx';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An image's data, or an object with it and, for an SVG, a PNG of it, which Word shows where it can't show SVGs. An
|
|
7
|
+
* SVG given without one is shown as nothing there.
|
|
8
|
+
*/
|
|
9
|
+
export declare type HtmlImage = HtmlImageData | {
|
|
10
|
+
readonly data: HtmlImageData;
|
|
11
|
+
readonly fallback?: HtmlImageData;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The options of `htmlToContent` and `htmlToRuns`.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
/** An image's data: its bytes, as a Uint8Array (such as a Node.js Buffer) or an ArrayBuffer, or base64, or a data URL */
|
|
20
|
+
export declare type HtmlImageData = Uint8Array | ArrayBuffer | string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for turning HTML into a document's content.
|
|
24
|
+
*/
|
|
25
|
+
export declare type HtmlOptions = {
|
|
26
|
+
/**
|
|
27
|
+
* CSS for the HTML, as if it were in a `<style>` element after the HTML's own, such as the style sheet of the
|
|
28
|
+
* editor it comes from. Rules in `@media print` blocks are used, and those in other `@media` blocks aren't.
|
|
29
|
+
*/
|
|
30
|
+
readonly stylesheet?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The Word styles to give the elements that CSS selectors match, by the styles' ids, such as
|
|
33
|
+
* `{ "p.lead": "Lead", "blockquote": "Quote", "code": "InlineCode", "table": "GridTable4" }`. A block is given a
|
|
34
|
+
* paragraph style, an inline element a character style, and a table a table style. When selectors that match an
|
|
35
|
+
* element give it different styles, the most specific selector's is used, as in CSS. The styles must be in the
|
|
36
|
+
* document, such as in its `styles` option or the template it patches.
|
|
37
|
+
*/
|
|
38
|
+
readonly styleMap?: Readonly<Record<string, string>>;
|
|
39
|
+
/**
|
|
40
|
+
* The images that `<img src>` refers to by addresses other than data URLs, as docx/html doesn't download anything:
|
|
41
|
+
* an object of each address and its image, or a function that gives the image for an address, or undefined when it
|
|
42
|
+
* has none. An address is looked for as it is written, and then resolved against `baseUrl`.
|
|
43
|
+
*/
|
|
44
|
+
readonly images?: Readonly<Record<string, HtmlImage>> | ((source: string) => HtmlImage | undefined);
|
|
45
|
+
/**
|
|
46
|
+
* The widest an image can be, in pixels, so it fits on the page: 601 by default, the width of the text on the page
|
|
47
|
+
* of a document with docx's default page size and margins. Wider images are scaled down.
|
|
48
|
+
*/
|
|
49
|
+
readonly maxImageWidth?: number;
|
|
50
|
+
/** The address that relative links and image addresses are relative to, such as `https://example.com/blog/` */
|
|
51
|
+
readonly baseUrl?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Turns `<ins>` and `<del>`, such as an HTML diff's, into Word's tracked changes, by this author, and at the time
|
|
54
|
+
* of their `datetime` attribute, or this time, or now: an ISO 8601 date and time such as
|
|
55
|
+
* `2026-10-13T09:30:00Z`. Without it, they are underlined and struck through, as a browser shows them.
|
|
56
|
+
*/
|
|
57
|
+
readonly trackChanges?: {
|
|
58
|
+
readonly author: string;
|
|
59
|
+
readonly date?: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* What the HTML's notes are in Word, such as the footnotes of Markdown, Pandoc and Google Docs: `"footnotes"`, at
|
|
63
|
+
* the foot of the page each is referred to on, by default, or `"endnotes"`, at the end of the document, as a web
|
|
64
|
+
* page has them. Word's own HTML says which of its notes are footnotes and which are endnotes, which they stay.
|
|
65
|
+
*/
|
|
66
|
+
readonly notes?: "footnotes" | "endnotes";
|
|
67
|
+
/** Called with each thing in the HTML that the document leaves out or has differently */
|
|
68
|
+
readonly onWarning?: (warning: HtmlWarning) => void;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Turns HTML into paragraphs and tables, to go in a document's sections, headers, footers, table cells and anywhere
|
|
73
|
+
* else they go, among others, or to patch a template with.
|
|
74
|
+
*
|
|
75
|
+
* It reads what rich-text editors write, such as TinyMCE, CKEditor, Quill, TipTap and Lexical, and what Word and
|
|
76
|
+
* Google Docs put on the clipboard, as a browser reads it: its `<style>` elements and `style` attributes, the styles
|
|
77
|
+
* of the classes those editors write, and HTML's own meanings, such as `<b>`'s bold. Paragraphs, headings, lists and
|
|
78
|
+
* links are in the Word document's own styles, such as Heading 1 and Hyperlink, with only what the HTML says, such as
|
|
79
|
+
* a colour or a font size, written as direct formatting.
|
|
80
|
+
*
|
|
81
|
+
* It has headings, paragraphs and line breaks; bold, italic, underlined, struck through, superscript and subscript
|
|
82
|
+
* text, its colours, backgrounds, fonts, sizes and spacing; alignment, indents, line spacing, borders and shading of
|
|
83
|
+
* paragraphs; lists, numbered or not, in each other, with their numbers' styles and starts; tables, with merged cells,
|
|
84
|
+
* header rows, widths, borders, backgrounds and alignment; images, with their sizes and alt text, floating or not;
|
|
85
|
+
* links, within the document too; checkboxes; math, as Word's equations; page breaks; and tracked changes.
|
|
86
|
+
*
|
|
87
|
+
* Nothing is downloaded: images that aren't data URLs are given with the `images` option. HTML that isn't valid is
|
|
88
|
+
* read as a browser reads it, and what the document can't have, such as a video, is left out, with a warning to
|
|
89
|
+
* `onWarning`. It never throws for the HTML it is given.
|
|
90
|
+
*
|
|
91
|
+
* @param html - The HTML: some of a document's body, such as an editor's content, or a whole document
|
|
92
|
+
* @param options - Options, such as the images and CSS of the HTML
|
|
93
|
+
* @returns The paragraphs and tables
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* import { Document, Packer } from "docx";
|
|
98
|
+
* import { htmlToContent } from "docx/html";
|
|
99
|
+
*
|
|
100
|
+
* const doc = new Document({
|
|
101
|
+
* sections: [{ children: htmlToContent("<h1>Report</h1><p>The <b>results</b> are in.</p>") }],
|
|
102
|
+
* });
|
|
103
|
+
* const buffer = await Packer.toBuffer(doc);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare const htmlToContent: (html: string, options?: HtmlOptions) => readonly (Paragraph | Table)[];
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Turns HTML into the content of a paragraph: runs of text, hyperlinks, images and the rest, such as for a paragraph
|
|
110
|
+
* of a document's own, or to patch a paragraph of a template with.
|
|
111
|
+
*
|
|
112
|
+
* It reads HTML as `htmlToContent` does. Paragraphs in the HTML are lines of the paragraph, and the content of tables
|
|
113
|
+
* and lists is kept, without the tables and lists, with a warning, as a paragraph can't have them.
|
|
114
|
+
*
|
|
115
|
+
* @param html - The HTML, such as `"<b>Due</b> on <i>Friday</i>"`
|
|
116
|
+
* @param options - Options, such as the images and CSS of the HTML
|
|
117
|
+
* @returns The paragraph's content
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* new Paragraph({ children: htmlToRuns("Total: <b>£120</b>") });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export declare const htmlToRuns: (html: string, options?: HtmlOptions) => readonly ParagraphChild[];
|
|
125
|
+
|
|
126
|
+
/** Something in the HTML that the document leaves out or has differently */
|
|
127
|
+
export declare type HtmlWarning = {
|
|
128
|
+
readonly code: HtmlWarningCode;
|
|
129
|
+
/** What it is, in English, such as `The image "photo.webp" is WebP, which Word can't show, so it is left out` */
|
|
130
|
+
readonly message: string;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/** What kind of thing a warning is about */
|
|
134
|
+
export declare type HtmlWarningCode =
|
|
135
|
+
/** An element the document can't have, such as `<video>` or `<iframe>`, which is left out */
|
|
136
|
+
"unsupported-element"
|
|
137
|
+
/** An image whose data isn't in the HTML or the `images` option, which is left out */
|
|
138
|
+
| "missing-image"
|
|
139
|
+
/** An image in a format Word can't show, such as WebP, or data that isn't an image, which is left out */
|
|
140
|
+
| "unsupported-image"
|
|
141
|
+
/** A link to a `javascript:` address, or another that could run code, whose text is kept without the link */
|
|
142
|
+
| "unsafe-link"
|
|
143
|
+
/** Math that can't be read, whose text is kept */
|
|
144
|
+
| "invalid-math"
|
|
145
|
+
/** Content in more than a thousand elements in each other, whose text is kept without its formatting */
|
|
146
|
+
| "too-deep"
|
|
147
|
+
/** A table, list or other block in HTML given to `htmlToRuns`, whose content is kept in the paragraph */
|
|
148
|
+
| "block-in-paragraph"
|
|
149
|
+
/** A table in a note, whose cells' paragraphs are kept in the footnote or endnote without it */
|
|
150
|
+
| "table-in-note";
|
|
151
|
+
|
|
152
|
+
export { }
|