@open-egretta/lexical-export-docx 0.0.1
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/LICENSE +21 -0
- package/README.md +64 -0
- package/dist/index.cjs +145 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +117 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 open-egretta
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @open-egretta/lexical-export-docx
|
|
2
|
+
|
|
3
|
+
Export [Lexical](https://lexical.dev/) editor content to `.docx` format.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @open-egretta/lexical-export-docx
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires `lexical >= 0.41.0` as a peer dependency.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Inside a React component
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { exportDocx } from "@open-egretta/lexical-export-docx";
|
|
19
|
+
|
|
20
|
+
// editor is a LexicalEditor instance
|
|
21
|
+
const blob = await exportDocx(editor);
|
|
22
|
+
|
|
23
|
+
const url = URL.createObjectURL(blob);
|
|
24
|
+
const a = document.createElement("a");
|
|
25
|
+
a.href = url;
|
|
26
|
+
a.download = "document.docx";
|
|
27
|
+
a.click();
|
|
28
|
+
URL.revokeObjectURL(url);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### From JSON (SSR / Node.js)
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { exportDocxFromJSON } from "@open-egretta/lexical-export-docx";
|
|
35
|
+
import type { SerializedEditorState } from "lexical";
|
|
36
|
+
|
|
37
|
+
const json: SerializedEditorState = {
|
|
38
|
+
/* ... */
|
|
39
|
+
};
|
|
40
|
+
const blob = await exportDocxFromJSON(json);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
### `exportDocx(editor: LexicalEditor): Promise<Blob>`
|
|
46
|
+
|
|
47
|
+
Reads the current editor state and returns a `.docx` `Blob`. Use this inside a React component where you have access to the `LexicalEditor` instance.
|
|
48
|
+
|
|
49
|
+
### `exportDocxFromJSON(json: SerializedEditorState): Promise<Blob>`
|
|
50
|
+
|
|
51
|
+
Converts a serialized editor state directly to a `.docx` `Blob`. Has zero dependency on the Lexical runtime — suitable for SSR, testing, and Node.js environments.
|
|
52
|
+
|
|
53
|
+
## Supported nodes
|
|
54
|
+
|
|
55
|
+
| Node | Details |
|
|
56
|
+
| ----------- | --------------------------- |
|
|
57
|
+
| `paragraph` | Plain paragraph |
|
|
58
|
+
| `heading` | h1 – h6 |
|
|
59
|
+
| `text` | Bold, italic, underline |
|
|
60
|
+
| `list` | Bullet and numbered, nested |
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
exportDocx: () => exportDocx,
|
|
24
|
+
exportDocxFromJSON: () => exportDocxFromJSON
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/core/builder.ts
|
|
29
|
+
var import_docx = require("docx");
|
|
30
|
+
async function buildDocx(elements) {
|
|
31
|
+
const doc = new import_docx.Document({
|
|
32
|
+
//
|
|
33
|
+
numbering: {
|
|
34
|
+
config: [
|
|
35
|
+
{
|
|
36
|
+
reference: "default-numbering",
|
|
37
|
+
levels: [
|
|
38
|
+
{
|
|
39
|
+
level: 0,
|
|
40
|
+
format: import_docx.NumberFormat.DECIMAL,
|
|
41
|
+
text: "%1.",
|
|
42
|
+
alignment: import_docx.AlignmentType.LEFT,
|
|
43
|
+
style: {
|
|
44
|
+
paragraph: {
|
|
45
|
+
indent: { left: 480, hanging: 480 }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
//
|
|
54
|
+
sections: [
|
|
55
|
+
{
|
|
56
|
+
children: elements
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
});
|
|
60
|
+
return import_docx.Packer.toBlob(doc);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/nodes/paragraph.ts
|
|
64
|
+
var import_docx3 = require("docx");
|
|
65
|
+
|
|
66
|
+
// src/nodes/text.ts
|
|
67
|
+
var import_docx2 = require("docx");
|
|
68
|
+
function convertText(node) {
|
|
69
|
+
return new import_docx2.TextRun({
|
|
70
|
+
text: node.text,
|
|
71
|
+
bold: (node.format & 1) !== 0,
|
|
72
|
+
italics: (node.format & 2) !== 0,
|
|
73
|
+
underline: (node.format & 8) !== 0 ? {} : void 0
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/nodes/paragraph.ts
|
|
78
|
+
function convertParagraph(node) {
|
|
79
|
+
const runs = node.children.filter((child) => child.type === "text").map((child) => convertText(child));
|
|
80
|
+
return new import_docx3.Paragraph({ children: runs });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/nodes/heading.ts
|
|
84
|
+
var import_docx4 = require("docx");
|
|
85
|
+
var HEADING_MAP = {
|
|
86
|
+
h1: import_docx4.HeadingLevel.HEADING_1,
|
|
87
|
+
h2: import_docx4.HeadingLevel.HEADING_2,
|
|
88
|
+
h3: import_docx4.HeadingLevel.HEADING_3,
|
|
89
|
+
h4: import_docx4.HeadingLevel.HEADING_4,
|
|
90
|
+
h5: import_docx4.HeadingLevel.HEADING_5,
|
|
91
|
+
h6: import_docx4.HeadingLevel.HEADING_6
|
|
92
|
+
};
|
|
93
|
+
function convertHeading(node) {
|
|
94
|
+
const runs = node.children.filter((child) => child.type === "text").map((child) => convertText(child));
|
|
95
|
+
return new import_docx4.Paragraph({
|
|
96
|
+
heading: HEADING_MAP[node.tag],
|
|
97
|
+
children: runs
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/nodes/list.ts
|
|
102
|
+
var import_docx5 = require("docx");
|
|
103
|
+
function convertList(node) {
|
|
104
|
+
return node.children.filter(
|
|
105
|
+
(child) => child.type === "listitem"
|
|
106
|
+
).map((item) => {
|
|
107
|
+
const runs = item.children.filter((child) => child.type === "text").map((child) => convertText(child));
|
|
108
|
+
return new import_docx5.Paragraph({
|
|
109
|
+
bullet: node.listType === "bullet" ? { level: item.indent } : void 0,
|
|
110
|
+
numbering: node.listType === "number" ? { reference: "default-numbering", level: item.indent } : void 0,
|
|
111
|
+
children: runs
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/serializer.ts
|
|
117
|
+
function serialize(children) {
|
|
118
|
+
return children.flatMap((node) => {
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case "paragraph":
|
|
121
|
+
return convertParagraph(node);
|
|
122
|
+
case "list":
|
|
123
|
+
return convertList(node);
|
|
124
|
+
case "heading":
|
|
125
|
+
return convertHeading(node);
|
|
126
|
+
default:
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/index.ts
|
|
133
|
+
async function exportDocx(editor) {
|
|
134
|
+
const json = editor.getEditorState().toJSON();
|
|
135
|
+
return exportDocxFromJSON(json);
|
|
136
|
+
}
|
|
137
|
+
async function exportDocxFromJSON(json) {
|
|
138
|
+
const elements = serialize(json.root.children);
|
|
139
|
+
return buildDocx(elements);
|
|
140
|
+
}
|
|
141
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
142
|
+
0 && (module.exports = {
|
|
143
|
+
exportDocx,
|
|
144
|
+
exportDocxFromJSON
|
|
145
|
+
});
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/core/builder.ts
|
|
2
|
+
import { AlignmentType, Document, NumberFormat, Packer } from "docx";
|
|
3
|
+
async function buildDocx(elements) {
|
|
4
|
+
const doc = new Document({
|
|
5
|
+
//
|
|
6
|
+
numbering: {
|
|
7
|
+
config: [
|
|
8
|
+
{
|
|
9
|
+
reference: "default-numbering",
|
|
10
|
+
levels: [
|
|
11
|
+
{
|
|
12
|
+
level: 0,
|
|
13
|
+
format: NumberFormat.DECIMAL,
|
|
14
|
+
text: "%1.",
|
|
15
|
+
alignment: AlignmentType.LEFT,
|
|
16
|
+
style: {
|
|
17
|
+
paragraph: {
|
|
18
|
+
indent: { left: 480, hanging: 480 }
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
//
|
|
27
|
+
sections: [
|
|
28
|
+
{
|
|
29
|
+
children: elements
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
});
|
|
33
|
+
return Packer.toBlob(doc);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/nodes/paragraph.ts
|
|
37
|
+
import { Paragraph as Paragraph2 } from "docx";
|
|
38
|
+
|
|
39
|
+
// src/nodes/text.ts
|
|
40
|
+
import { TextRun } from "docx";
|
|
41
|
+
function convertText(node) {
|
|
42
|
+
return new TextRun({
|
|
43
|
+
text: node.text,
|
|
44
|
+
bold: (node.format & 1) !== 0,
|
|
45
|
+
italics: (node.format & 2) !== 0,
|
|
46
|
+
underline: (node.format & 8) !== 0 ? {} : void 0
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/nodes/paragraph.ts
|
|
51
|
+
function convertParagraph(node) {
|
|
52
|
+
const runs = node.children.filter((child) => child.type === "text").map((child) => convertText(child));
|
|
53
|
+
return new Paragraph2({ children: runs });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/nodes/heading.ts
|
|
57
|
+
import { Paragraph as Paragraph3, HeadingLevel } from "docx";
|
|
58
|
+
var HEADING_MAP = {
|
|
59
|
+
h1: HeadingLevel.HEADING_1,
|
|
60
|
+
h2: HeadingLevel.HEADING_2,
|
|
61
|
+
h3: HeadingLevel.HEADING_3,
|
|
62
|
+
h4: HeadingLevel.HEADING_4,
|
|
63
|
+
h5: HeadingLevel.HEADING_5,
|
|
64
|
+
h6: HeadingLevel.HEADING_6
|
|
65
|
+
};
|
|
66
|
+
function convertHeading(node) {
|
|
67
|
+
const runs = node.children.filter((child) => child.type === "text").map((child) => convertText(child));
|
|
68
|
+
return new Paragraph3({
|
|
69
|
+
heading: HEADING_MAP[node.tag],
|
|
70
|
+
children: runs
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/nodes/list.ts
|
|
75
|
+
import { Paragraph as Paragraph4 } from "docx";
|
|
76
|
+
function convertList(node) {
|
|
77
|
+
return node.children.filter(
|
|
78
|
+
(child) => child.type === "listitem"
|
|
79
|
+
).map((item) => {
|
|
80
|
+
const runs = item.children.filter((child) => child.type === "text").map((child) => convertText(child));
|
|
81
|
+
return new Paragraph4({
|
|
82
|
+
bullet: node.listType === "bullet" ? { level: item.indent } : void 0,
|
|
83
|
+
numbering: node.listType === "number" ? { reference: "default-numbering", level: item.indent } : void 0,
|
|
84
|
+
children: runs
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/serializer.ts
|
|
90
|
+
function serialize(children) {
|
|
91
|
+
return children.flatMap((node) => {
|
|
92
|
+
switch (node.type) {
|
|
93
|
+
case "paragraph":
|
|
94
|
+
return convertParagraph(node);
|
|
95
|
+
case "list":
|
|
96
|
+
return convertList(node);
|
|
97
|
+
case "heading":
|
|
98
|
+
return convertHeading(node);
|
|
99
|
+
default:
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/index.ts
|
|
106
|
+
async function exportDocx(editor) {
|
|
107
|
+
const json = editor.getEditorState().toJSON();
|
|
108
|
+
return exportDocxFromJSON(json);
|
|
109
|
+
}
|
|
110
|
+
async function exportDocxFromJSON(json) {
|
|
111
|
+
const elements = serialize(json.root.children);
|
|
112
|
+
return buildDocx(elements);
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
exportDocx,
|
|
116
|
+
exportDocxFromJSON
|
|
117
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@open-egretta/lexical-export-docx",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Export Lexical editor content to .docx format",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"lexical",
|
|
21
|
+
"editor",
|
|
22
|
+
"docx",
|
|
23
|
+
"export"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/open-egretta/lexical-export-docx"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
31
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
32
|
+
"test": "vitest"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"docx": "^9.6.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"lexical": ">=0.41.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@lexical/list": "^0.41.0",
|
|
42
|
+
"@lexical/rich-text": "^0.41.0",
|
|
43
|
+
"lexical": "^0.41.0",
|
|
44
|
+
"tsup": "^8.5.1",
|
|
45
|
+
"tsx": "^4.21.0",
|
|
46
|
+
"typescript": "^5.9.3",
|
|
47
|
+
"vitest": "^4.0.18"
|
|
48
|
+
}
|
|
49
|
+
}
|