zcode-app-cli 3.3.5-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 +24 -0
- package/README.md +642 -0
- package/bin/zcode.js +848 -0
- package/config.example.json +110 -0
- package/package.json +81 -0
- package/vendor/.node-bundle-meta.json +6 -0
- package/vendor/extraction.json +11 -0
- package/vendor/node_modules/@zcode/tui/dist/index.js +161875 -0
- package/vendor/node_modules/@zcode/tui/package.json +14 -0
- package/vendor/packages/android-emulator-plugin/.mcp.json +19 -0
- package/vendor/packages/android-emulator-plugin/.zcode-plugin/plugin.json +63 -0
- package/vendor/packages/android-emulator-plugin/README.md +134 -0
- package/vendor/packages/android-emulator-plugin/commands/android-dev.md +11 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/path.d.ts +4 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/path.js +17 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/result.d.ts +5 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/result.js +22 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/run.d.ts +18 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/run.js +99 -0
- package/vendor/packages/android-emulator-plugin/dist/mcp/server.d.ts +3 -0
- package/vendor/packages/android-emulator-plugin/dist/mcp/server.js +47607 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/app.d.ts +39 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/app.js +50 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/avd.d.ts +37 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/avd.js +185 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/build.d.ts +68 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/build.js +131 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/config.d.ts +7 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/config.js +29 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/device.d.ts +20 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/device.js +91 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/logs.d.ts +15 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/logs.js +31 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/preflight.d.ts +15 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/preflight.js +123 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project-template.d.ts +13 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project-template.js +211 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project.d.ts +33 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project.js +195 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/screenshot.d.ts +14 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/screenshot.js +73 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/sdk.d.ts +14 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/sdk.js +185 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/ui.d.ts +92 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/ui.js +146 -0
- package/vendor/packages/android-emulator-plugin/hooks/hooks.json +3 -0
- package/vendor/packages/android-emulator-plugin/package.json +35 -0
- package/vendor/packages/android-emulator-plugin/skills/android-dev/INSTALL_ENVIRONMENT.md +357 -0
- package/vendor/packages/android-emulator-plugin/skills/android-dev/SKILL.md +73 -0
- package/vendor/packages/android-emulator-plugin/templates/compose-app/README.md +5 -0
- package/vendor/packages/document-skills-plugin/.zcode-plugin/plugin.json +8 -0
- package/vendor/packages/document-skills-plugin/package.json +8 -0
- package/vendor/packages/document-skills-plugin/skills/docx/LICENSE.txt +13 -0
- package/vendor/packages/document-skills-plugin/skills/docx/SKILL.md +201 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/chart-templates.md +386 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/common-rules.md +419 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/decorations.md +537 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/design-system.md +1797 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/docx-js-advanced.md +257 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/docx-js-core.md +332 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/faq.md +323 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/math-formulas.md +276 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/ooxml.md +222 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/toc.md +264 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/comment.md +88 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/create.md +207 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/edit.md +115 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/format.md +120 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/read.md +114 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/academic.md +783 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/contract.md +463 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/copywriting.md +139 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/exam.md +698 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/official-doc.md +411 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/report.md +340 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/resume.md +534 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/__init__.py +1 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/add_toc_placeholders.py +749 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/document.py +1333 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/postcheck.py +807 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/comments.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsExtended.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsIds.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/people.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/utilities.py +374 -0
- package/vendor/packages/document-skills-plugin/skills/docx/setup.sh +177 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/LICENSE.txt +13 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/SKILL.md +919 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/academic.md +1058 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/creative.md +770 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/poster.md +702 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process-advanced.md +284 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process.md +319 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/report.md +1659 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/configs/components.md +153 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/configs/fonts.md +93 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/configs/visual_framework.md +263 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/references/resume-academic.tex +130 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/references/resume-altacv.tex +190 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/cover_validate.js +367 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/design_engine.py +2816 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2pdf-next.js +754 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2poster.js +256 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf.py +2959 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf_qa.py +901 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/poster_validate.py +1337 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/setup.sh +269 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/toc_validate.py +2075 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/charts.md +320 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover-backgrounds.md +384 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover.md +1442 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/fill-engine.md +527 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/geometry.md +142 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/overflow.md +630 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/pagination.md +367 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/palette.md +217 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/typography.md +20 -0
- package/vendor/packages/ios-simulator-plugin/.mcp.json +15 -0
- package/vendor/packages/ios-simulator-plugin/.zcode-plugin/plugin.json +34 -0
- package/vendor/packages/ios-simulator-plugin/README.md +111 -0
- package/vendor/packages/ios-simulator-plugin/commands/ios-dev.md +11 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/path.d.ts +4 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/path.js +18 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/result.d.ts +5 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/result.js +20 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/run.d.ts +17 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/run.js +83 -0
- package/vendor/packages/ios-simulator-plugin/dist/mcp/server.d.ts +3 -0
- package/vendor/packages/ios-simulator-plugin/dist/mcp/server.js +46833 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/build.d.ts +112 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/build.js +99 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/logs.d.ts +21 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/logs.js +26 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/preflight.d.ts +12 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/preflight.js +62 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/project.d.ts +35 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/project.js +288 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/screenshot.d.ts +22 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/screenshot.js +30 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/sim.d.ts +60 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/sim.js +116 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/ui.d.ts +57 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/ui.js +67 -0
- package/vendor/packages/ios-simulator-plugin/hooks/hooks.json +3 -0
- package/vendor/packages/ios-simulator-plugin/package.json +35 -0
- package/vendor/packages/ios-simulator-plugin/skills/ios-dev/SKILL.md +45 -0
- package/vendor/packages/ios-simulator-plugin/templates/swiftui-app/README.md +5 -0
- package/vendor/packages/restore-legacy-sessions-plugin/.zcode-plugin/plugin.json +9 -0
- package/vendor/packages/restore-legacy-sessions-plugin/README.md +35 -0
- package/vendor/packages/restore-legacy-sessions-plugin/commands/restore-legacy-sessions.md +11 -0
- package/vendor/packages/restore-legacy-sessions-plugin/package.json +8 -0
- package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/SKILL.md +92 -0
- package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/scripts/restore-conversation.mjs +648 -0
- package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/scripts/scan-legacy-sessions.mjs +422 -0
- package/vendor/packages/skill-creator-plugin/.zcode-plugin/plugin.json +8 -0
- package/vendor/packages/skill-creator-plugin/package.json +8 -0
- package/vendor/packages/skill-creator-plugin/skills/skill-creator/SKILL.md +159 -0
- package/vendor/packages/zcode-guide-plugin/.zcode-plugin/plugin.json +8 -0
- package/vendor/packages/zcode-guide-plugin/README.md +22 -0
- package/vendor/packages/zcode-guide-plugin/package.json +8 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-commands/SKILL.md +66 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-hooks/SKILL.md +66 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-mcp/SKILL.md +97 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-plugins/SKILL.md +59 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-skills/SKILL.md +61 -0
- package/vendor/packages/zcode-guide-plugin/skills/zcode-configuration-guide/SKILL.md +99 -0
- package/vendor/zcode.cjs +2926 -0
- package/zcode-runtime.lock.json +8 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# docx-js Advanced Features
|
|
2
|
+
|
|
3
|
+
Advanced API for complex document scenarios. Load this when creating documents with TOC, cover pages, footnotes, multi-section layouts, or post-processing needs.
|
|
4
|
+
|
|
5
|
+
## Table of Contents (TOC)
|
|
6
|
+
|
|
7
|
+
**→ See `references/toc.md` for the complete TOC reference** (3-step process, code examples, page numbering, common bugs, checklist).
|
|
8
|
+
|
|
9
|
+
## Cover Page Design (Vertical Centering)
|
|
10
|
+
|
|
11
|
+
Use large `spacing.before` to push content down for visual centering:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// Approximate vertical center on A4:
|
|
15
|
+
// Total printable height ≈ 14000 twips
|
|
16
|
+
// For title at ~40% from top: before = 5600
|
|
17
|
+
const coverSection = {
|
|
18
|
+
properties: {
|
|
19
|
+
page: { /* standard A4 */ },
|
|
20
|
+
// No headers/footers on cover page
|
|
21
|
+
},
|
|
22
|
+
children: [
|
|
23
|
+
new Paragraph({ spacing: { before: 5600 } }), // spacer
|
|
24
|
+
new Paragraph({
|
|
25
|
+
alignment: AlignmentType.CENTER,
|
|
26
|
+
children: [new TextRun({
|
|
27
|
+
text: title,
|
|
28
|
+
font: { ascii: "Calibri", eastAsia: "SimHei" },
|
|
29
|
+
size: 52, bold: true, color: palette.primary,
|
|
30
|
+
})],
|
|
31
|
+
}),
|
|
32
|
+
// ... subtitle, author, date
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For multi-section documents, put the cover in its own section so it can have different headers/footers.
|
|
38
|
+
|
|
39
|
+
## Footnotes
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const { FootnoteReferenceRun, Footnote } = require("docx");
|
|
43
|
+
|
|
44
|
+
const doc = new Document({
|
|
45
|
+
footnotes: {
|
|
46
|
+
1: { children: [new Paragraph({ children: [new TextRun({ text: "Smith, J. (2024). Research Methods. Academic Press, pp. 45-67.", size: 18 })] })] },
|
|
47
|
+
2: { children: [new Paragraph({ children: [new TextRun({ text: "Zhang, W. (2023). \u201c数据分析方法研究\u201d. 科学通报, 68(12), 1234-1250.", size: 18 })] })] },
|
|
48
|
+
},
|
|
49
|
+
sections: [{
|
|
50
|
+
children: [
|
|
51
|
+
new Paragraph({
|
|
52
|
+
children: [
|
|
53
|
+
new TextRun({ text: "According to recent studies" }),
|
|
54
|
+
new FootnoteReferenceRun(1), // superscript [1]
|
|
55
|
+
new TextRun({ text: ", data analysis methods have evolved" }),
|
|
56
|
+
new FootnoteReferenceRun(2), // superscript [2]
|
|
57
|
+
new TextRun({ text: "." }),
|
|
58
|
+
],
|
|
59
|
+
}),
|
|
60
|
+
],
|
|
61
|
+
}],
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Academic Reference Pattern
|
|
66
|
+
|
|
67
|
+
For sequential references [1][2][3]..., pre-define all footnotes in the `footnotes` object with numeric keys, then reference them inline with `FootnoteReferenceRun(n)`.
|
|
68
|
+
|
|
69
|
+
## keepNext — Element Binding
|
|
70
|
+
|
|
71
|
+
Prevent page breaks between related elements:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
// Heading stays with next paragraph
|
|
75
|
+
new Paragraph({
|
|
76
|
+
heading: HeadingLevel.HEADING_2,
|
|
77
|
+
keepNext: true, // don't break after this
|
|
78
|
+
children: [new TextRun({ text: "Table 1: Results" })],
|
|
79
|
+
})
|
|
80
|
+
// Table immediately follows on same page
|
|
81
|
+
|
|
82
|
+
// Caption stays with image
|
|
83
|
+
new Paragraph({
|
|
84
|
+
keepNext: true,
|
|
85
|
+
alignment: AlignmentType.CENTER,
|
|
86
|
+
children: [new TextRun({ text: "Figure 1: Architecture Diagram", italics: true, size: 20 })],
|
|
87
|
+
})
|
|
88
|
+
// ImageRun paragraph follows
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Use `keepNext: true` for:
|
|
92
|
+
- Heading → first paragraph of section
|
|
93
|
+
- Table caption → table
|
|
94
|
+
- Image → image caption
|
|
95
|
+
- "Figure X" label → image
|
|
96
|
+
|
|
97
|
+
## Page Break Rules
|
|
98
|
+
|
|
99
|
+
Follow the document type strategy defined in SOUL.md Rule 1.
|
|
100
|
+
|
|
101
|
+
**Structural breaks (always):**
|
|
102
|
+
- Cover page → TOC
|
|
103
|
+
- TOC → main content
|
|
104
|
+
- Main content → back cover
|
|
105
|
+
|
|
106
|
+
**Content breaks (by document type):**
|
|
107
|
+
- Academic / teaching → `new Paragraph({ children: [new PageBreak()] })` before each H1 chapter
|
|
108
|
+
- Business report → PageBreak before each H1; H2 flows naturally
|
|
109
|
+
- Resume / contract / letter → No content page breaks
|
|
110
|
+
- Short article → No content page breaks
|
|
111
|
+
|
|
112
|
+
**Anti-tear (mandatory):**
|
|
113
|
+
```js
|
|
114
|
+
// Heading stays with next paragraph
|
|
115
|
+
new Paragraph({
|
|
116
|
+
heading: HeadingLevel.HEADING_1,
|
|
117
|
+
keepNext: true,
|
|
118
|
+
children: [new TextRun("Chapter Title")],
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
// Table caption stays with table
|
|
122
|
+
new Paragraph({
|
|
123
|
+
keepNext: true,
|
|
124
|
+
children: [new TextRun({ text: "Table 1: Summary", italics: true })],
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
// Image caption stays with image
|
|
128
|
+
new Paragraph({
|
|
129
|
+
keepNext: true,
|
|
130
|
+
children: [new TextRun({ text: "Figure 1: Architecture", italics: true })],
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Never:**
|
|
135
|
+
- PageBreak inside tables
|
|
136
|
+
- PageBreak as standalone element (must be inside Paragraph)
|
|
137
|
+
- PageBreak at the END of the last section (causes blank page)
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
// Correct: page break between cover and TOC
|
|
141
|
+
new Paragraph({ children: [new PageBreak()] })
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Quotes Escaping in JS Strings
|
|
145
|
+
|
|
146
|
+
**⚠️⚠️⚠️ CRITICAL — #1 MOST COMMON BUG ⚠️⚠️⚠️**
|
|
147
|
+
|
|
148
|
+
Bare Chinese curly quotation marks (`""` `''`) in JS string literals **WILL break syntax and crash document generation**. This bug occurs most often in **Chinese body text** where curly quotes are used for emphasis, proper nouns, event names, or quoted speech — e.g., `"双11"`, `"前低后高"`, `"618"大促`. **Every single occurrence** of `""''` in text content MUST be Unicode-escaped. No exceptions.
|
|
149
|
+
|
|
150
|
+
**MANDATORY RULE: Before writing ANY `TextRun`, `para()`, or string containing Chinese text, scan the text for `""''` characters and replace ALL of them with `\u201c \u201d \u2018 \u2019`.**
|
|
151
|
+
|
|
152
|
+
| Character | Unicode | Escape method |
|
|
153
|
+
|-----------|---------|---------------|
|
|
154
|
+
| `"` `"` | `\u201c` `\u201d` | Unicode escape `\u201c` `\u201d` |
|
|
155
|
+
| `'` `'` | `\u2018` `\u2019` | Unicode escape `\u2018` `\u2019` |
|
|
156
|
+
| `"` | U+0022 | `\"` or wrap string in single quotes / template literal |
|
|
157
|
+
| `'` | U+0027 | `\'` or wrap string in double quotes / template literal |
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
// ❌ WRONG — curly quotes in Chinese text break JS syntax (VERY COMMON MISTAKE)
|
|
161
|
+
content.push(para("2025年四个季度行业增速呈现"前低后高"的态势。在"618"大促、"双11""双12"活动拉动下增长显著。"));
|
|
162
|
+
new TextRun({ text: "他说"你好"" })
|
|
163
|
+
new TextRun({ text: 'It's a test' })
|
|
164
|
+
|
|
165
|
+
// ✅ CORRECT — ALL curly quotes replaced with Unicode escapes
|
|
166
|
+
content.push(para("2025年四个季度行业增速呈现\u201c前低后高\u201d的态势。在\u201c618\u201d大促、\u201c双11\u201d\u201c双12\u201d活动拉动下增长显著。"));
|
|
167
|
+
new TextRun({ text: "他说\u201c你好\u201d" })
|
|
168
|
+
new TextRun({ text: "It\u2019s a test" })
|
|
169
|
+
|
|
170
|
+
// ✅ CORRECT — straight quotes escaped or use alternate delimiters
|
|
171
|
+
new TextRun({ text: "He said \"hello\"" })
|
|
172
|
+
new TextRun({ text: 'He said "hello"' })
|
|
173
|
+
new TextRun({ text: `He said "hello"` })
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Multi-Section Documents
|
|
177
|
+
|
|
178
|
+
Different headers/footers per section:
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
const doc = new Document({
|
|
182
|
+
sections: [
|
|
183
|
+
{
|
|
184
|
+
// Section 1: Cover — no header/footer
|
|
185
|
+
properties: { page: { /* ... */ } },
|
|
186
|
+
children: coverChildren,
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
// Section 2: Front matter — Roman page numbers
|
|
190
|
+
properties: {
|
|
191
|
+
type: SectionType.NEXT_PAGE,
|
|
192
|
+
page: {
|
|
193
|
+
/* size, margin... */
|
|
194
|
+
pageNumbers: { start: 1, formatType: NumberFormat.UPPER_ROMAN },
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
headers: { default: new Header({ children: [] }) },
|
|
198
|
+
footers: {
|
|
199
|
+
default: new Footer({
|
|
200
|
+
children: [new Paragraph({
|
|
201
|
+
alignment: AlignmentType.CENTER,
|
|
202
|
+
children: [new TextRun({ children: [PageNumber.CURRENT], size: 18 })],
|
|
203
|
+
})],
|
|
204
|
+
}),
|
|
205
|
+
},
|
|
206
|
+
children: tocAndAbstract,
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
// Section 3: Main content — Arabic page numbers
|
|
210
|
+
properties: {
|
|
211
|
+
type: SectionType.NEXT_PAGE,
|
|
212
|
+
page: {
|
|
213
|
+
/* size, margin... */
|
|
214
|
+
pageNumbers: { start: 1, formatType: NumberFormat.DECIMAL },
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
headers: {
|
|
218
|
+
default: new Header({
|
|
219
|
+
children: [new Paragraph({
|
|
220
|
+
alignment: AlignmentType.CENTER,
|
|
221
|
+
children: [new TextRun({ text: docTitle, size: 18, color: "888888" })],
|
|
222
|
+
})],
|
|
223
|
+
}),
|
|
224
|
+
},
|
|
225
|
+
footers: { default: footerWithPageNumbers },
|
|
226
|
+
children: mainContent,
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Converting DOCX to PDF
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# Using LibreOffice (headless)
|
|
236
|
+
libreoffice --headless --convert-to pdf output.docx
|
|
237
|
+
|
|
238
|
+
# ⚠️ TOC Rule: If document has TOC, warn user that:
|
|
239
|
+
# 1. LibreOffice conversion may show empty TOC
|
|
240
|
+
# 2. User should open in Word first, update fields (Ctrl+A → F9), save, then convert
|
|
241
|
+
# 3. Or use Word's "Save as PDF" for best results
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Converting DOCX to Images
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
# Step 1: Convert to PDF
|
|
248
|
+
libreoffice --headless --convert-to pdf output.docx
|
|
249
|
+
|
|
250
|
+
# Step 2: Convert PDF to images
|
|
251
|
+
pdftoppm -png -r 200 output.pdf output_page
|
|
252
|
+
|
|
253
|
+
# This generates output_page-1.png, output_page-2.png, etc.
|
|
254
|
+
# Use -r 200 for good quality (200 DPI)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Useful for generating preview thumbnails or when user needs images instead of document files.
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# docx-js API Reference
|
|
2
|
+
|
|
3
|
+
Complete API for creating .docx documents with the `docx` npm package. For advanced features (TOC details, footnotes, PDF conversion), see `docx-js-advanced.md`.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const {
|
|
9
|
+
Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell,
|
|
10
|
+
ImageRun, PageBreak, Header, Footer, PageNumber, NumberFormat,
|
|
11
|
+
AlignmentType, HeadingLevel, WidthType, BorderStyle, ShadingType,
|
|
12
|
+
PageOrientation, TabStopType, TabStopPosition, ExternalHyperlink,
|
|
13
|
+
InternalHyperlink, Bookmark, LevelFormat, TableOfContents,
|
|
14
|
+
} = require("docx");
|
|
15
|
+
const fs = require("fs");
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Document Creation + Export
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
const doc = new Document({
|
|
22
|
+
styles: { /* see Styles section */ },
|
|
23
|
+
numbering: { config: [ /* see Lists section */ ] },
|
|
24
|
+
sections: [{
|
|
25
|
+
properties: {
|
|
26
|
+
page: {
|
|
27
|
+
size: { width: 11906, height: 16838 },
|
|
28
|
+
margin: { top: 1417, bottom: 1417, left: 1701, right: 1417 },
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
headers: { default: new Header({ children: [/* */] }) },
|
|
32
|
+
footers: { default: new Footer({ children: [/* */] }) },
|
|
33
|
+
children: [ /* Paragraphs, Tables, etc. */ ],
|
|
34
|
+
}],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const buffer = await Packer.toBuffer(doc);
|
|
38
|
+
fs.writeFileSync("output.docx", buffer);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Paragraph + TextRun
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
new Paragraph({
|
|
45
|
+
heading: HeadingLevel.HEADING_1, // or HEADING_2, HEADING_3
|
|
46
|
+
alignment: AlignmentType.JUSTIFIED,
|
|
47
|
+
spacing: { before: 240, after: 120, line: 312 }, // 1.3x mandatory
|
|
48
|
+
indent: { firstLine: 480 }, // 2-char CJK indent (480 SimSun / 420 YaHei)
|
|
49
|
+
children: [
|
|
50
|
+
new TextRun({
|
|
51
|
+
text: "Hello",
|
|
52
|
+
bold: true,
|
|
53
|
+
italics: true,
|
|
54
|
+
size: 24, // 12pt = Xiao Si
|
|
55
|
+
font: { ascii: "Calibri", eastAsia: "Microsoft YaHei" },
|
|
56
|
+
color: "000000", // Pure black for Profile A; for Profile B use palette.body
|
|
57
|
+
}),
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Additional text formatting options
|
|
62
|
+
new TextRun({ text: "Underlined", underline: { type: UnderlineType.SINGLE } })
|
|
63
|
+
new TextRun({ text: "Highlighted", highlight: "yellow" })
|
|
64
|
+
new TextRun({ text: "Strikethrough", strike: true })
|
|
65
|
+
new TextRun({ text: "x²", superScript: true })
|
|
66
|
+
new TextRun({ text: "H₂O", subScript: true })
|
|
67
|
+
new SymbolRun({ char: "2022", font: "Symbol" }) // Bullet •
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Table
|
|
71
|
+
|
|
72
|
+
**⚠️ CRITICAL**: Always set `margins` on TableCell (or at Table level for global default). Without margins, text touches borders.
|
|
73
|
+
|
|
74
|
+
**⚠️ CRITICAL**: Use `ShadingType.CLEAR` — never `ShadingType.SOLID` (causes black cells).
|
|
75
|
+
|
|
76
|
+
**⚠️ CRITICAL — Table Cross-Page Control**:
|
|
77
|
+
- Header row MUST set `tableHeader: true` (auto-repeat header on page break)
|
|
78
|
+
- All rows MUST set `cantSplit: true` (prevent row content split across pages)
|
|
79
|
+
- Title paragraph before table MUST set `keepNext: true` (keep title with table)
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
// ⚠️ Title before table — keepNext keeps title with table
|
|
83
|
+
new Paragraph({
|
|
84
|
+
keepNext: true, // ← critical
|
|
85
|
+
children: [new TextRun({ text: "Table 1 Feature Comparison", bold: true, size: 21 })],
|
|
86
|
+
}),
|
|
87
|
+
|
|
88
|
+
new Table({
|
|
89
|
+
width: { size: 100, type: WidthType.PERCENTAGE },
|
|
90
|
+
borders: {
|
|
91
|
+
top: { style: BorderStyle.SINGLE, size: 2, color: "9AA6B2" },
|
|
92
|
+
bottom: { style: BorderStyle.SINGLE, size: 2, color: "9AA6B2" },
|
|
93
|
+
left: { style: BorderStyle.NONE },
|
|
94
|
+
right: { style: BorderStyle.NONE },
|
|
95
|
+
insideHorizontal: { style: BorderStyle.SINGLE, size: 1, color: "D0D0D0" },
|
|
96
|
+
insideVertical: { style: BorderStyle.NONE },
|
|
97
|
+
},
|
|
98
|
+
rows: [
|
|
99
|
+
// ⚠️ Header row — tableHeader + cantSplit
|
|
100
|
+
new TableRow({
|
|
101
|
+
tableHeader: true, // auto-repeat on page break
|
|
102
|
+
cantSplit: true, // prevent row split
|
|
103
|
+
children: ["Header 1", "Header 2"].map(text =>
|
|
104
|
+
new TableCell({
|
|
105
|
+
children: [new Paragraph({ children: [new TextRun({ text, bold: true, size: 21 })] })],
|
|
106
|
+
shading: { type: ShadingType.CLEAR, fill: "F1F5F9" },
|
|
107
|
+
margins: { top: 60, bottom: 60, left: 120, right: 120 },
|
|
108
|
+
width: { size: 50, type: WidthType.PERCENTAGE },
|
|
109
|
+
})
|
|
110
|
+
),
|
|
111
|
+
}),
|
|
112
|
+
// ⚠️ Data rows — cantSplit
|
|
113
|
+
new TableRow({
|
|
114
|
+
cantSplit: true, // prevent row split
|
|
115
|
+
children: ["Data 1", "Data 2"].map(text =>
|
|
116
|
+
new TableCell({
|
|
117
|
+
children: [new Paragraph({ children: [new TextRun({ text, size: 21 })] })],
|
|
118
|
+
margins: { top: 60, bottom: 60, left: 120, right: 120 },
|
|
119
|
+
width: { size: 50, type: WidthType.PERCENTAGE },
|
|
120
|
+
})
|
|
121
|
+
),
|
|
122
|
+
}),
|
|
123
|
+
],
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Column Widths
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
// Fixed widths (twips)
|
|
131
|
+
width: { size: 3000, type: WidthType.DXA }
|
|
132
|
+
// Percentage
|
|
133
|
+
width: { size: 50, type: WidthType.PERCENTAGE }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## ImageRun
|
|
137
|
+
|
|
138
|
+
**⚠️ CRITICAL**: Always include `type` parameter. Always preserve aspect ratio.
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
const imageBuffer = fs.readFileSync("chart.png");
|
|
142
|
+
// Calculate dimensions preserving aspect ratio
|
|
143
|
+
const displayWidth = 500;
|
|
144
|
+
const aspectRatio = originalHeight / originalWidth;
|
|
145
|
+
const displayHeight = Math.round(displayWidth * aspectRatio);
|
|
146
|
+
|
|
147
|
+
new Paragraph({
|
|
148
|
+
alignment: AlignmentType.CENTER,
|
|
149
|
+
children: [
|
|
150
|
+
new ImageRun({
|
|
151
|
+
data: imageBuffer,
|
|
152
|
+
transformation: { width: displayWidth, height: displayHeight },
|
|
153
|
+
type: "png", // REQUIRED: "png", "jpg", "gif", "bmp"
|
|
154
|
+
}),
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## PageBreak
|
|
160
|
+
|
|
161
|
+
**⚠️ CRITICAL**: PageBreak MUST be inside a Paragraph. Standalone PageBreak crashes Word.
|
|
162
|
+
|
|
163
|
+
**⚠️ Best Practice**: Attach PageBreak to the end of a **paragraph with text content**. Avoid empty paragraph + PageBreak (may cause blank pages). If using multi-section structure, prefer section breaks over PageBreak.
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
// ✅ Recommended — PageBreak attached to content paragraph
|
|
167
|
+
new Paragraph({
|
|
168
|
+
children: [
|
|
169
|
+
new TextRun({ text: "End of section" }),
|
|
170
|
+
new PageBreak()
|
|
171
|
+
]
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
// ✅ Acceptable — but prefer section breaks
|
|
175
|
+
new Paragraph({ children: [new PageBreak()] })
|
|
176
|
+
|
|
177
|
+
// ✅ Best — use section breaks instead of PageBreak
|
|
178
|
+
// Place content in different sections — auto page break
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Headers & Footers + Page Numbers
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
headers: {
|
|
185
|
+
default: new Header({
|
|
186
|
+
children: [
|
|
187
|
+
new Paragraph({
|
|
188
|
+
alignment: AlignmentType.CENTER,
|
|
189
|
+
children: [new TextRun({ text: "Document Title", size: 18, color: "888888" })],
|
|
190
|
+
}),
|
|
191
|
+
],
|
|
192
|
+
}),
|
|
193
|
+
},
|
|
194
|
+
footers: {
|
|
195
|
+
default: new Footer({
|
|
196
|
+
children: [
|
|
197
|
+
new Paragraph({
|
|
198
|
+
alignment: AlignmentType.CENTER,
|
|
199
|
+
children: [
|
|
200
|
+
new TextRun({ children: [PageNumber.CURRENT], size: 18 }),
|
|
201
|
+
],
|
|
202
|
+
}),
|
|
203
|
+
],
|
|
204
|
+
}),
|
|
205
|
+
},
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
> ⚠️ **Denominator FORBIDDEN** — never use `PageNumber.TOTAL_PAGES` or "X / Y" format. Show only current page number.
|
|
209
|
+
|
|
210
|
+
## Styles Definition
|
|
211
|
+
|
|
212
|
+
The example below is for **Chinese documents** (default). For **English documents**, replace `font` with `"Times New Roman"` throughout.
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
styles: {
|
|
216
|
+
default: {
|
|
217
|
+
document: {
|
|
218
|
+
run: {
|
|
219
|
+
font: { ascii: "Calibri", eastAsia: "Microsoft YaHei" },
|
|
220
|
+
size: 24, color: "000000", // Pure black for Profile A; for Profile B use palette.body
|
|
221
|
+
},
|
|
222
|
+
paragraph: {
|
|
223
|
+
spacing: { line: 312 }, // 1.3x mandatory
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
heading1: {
|
|
227
|
+
run: { font: { ascii: "Calibri", eastAsia: "SimHei" }, size: 32, bold: true, color: "0B1220" },
|
|
228
|
+
paragraph: { spacing: { before: 360, after: 160, line: 312 } },
|
|
229
|
+
},
|
|
230
|
+
heading2: {
|
|
231
|
+
run: { font: { ascii: "Calibri", eastAsia: "SimHei" }, size: 28, bold: true, color: "0B1220" },
|
|
232
|
+
paragraph: { spacing: { before: 240, after: 120, line: 312 } },
|
|
233
|
+
},
|
|
234
|
+
heading3: {
|
|
235
|
+
run: { font: { ascii: "Calibri", eastAsia: "SimHei" }, size: 24, bold: true, color: "0B1220" },
|
|
236
|
+
paragraph: { spacing: { before: 200, after: 100, line: 312 } },
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Lists
|
|
243
|
+
|
|
244
|
+
**⚠️ CRITICAL**: Each separate numbered list MUST use a unique `reference` name. Reusing the same reference causes numbering to continue instead of restarting.
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
// In Document numbering config
|
|
248
|
+
numbering: {
|
|
249
|
+
config: [
|
|
250
|
+
{
|
|
251
|
+
reference: "list-features", // unique name!
|
|
252
|
+
levels: [{
|
|
253
|
+
level: 0,
|
|
254
|
+
format: LevelFormat.DECIMAL,
|
|
255
|
+
text: "%1.",
|
|
256
|
+
alignment: AlignmentType.LEFT,
|
|
257
|
+
style: { paragraph: { indent: { left: 720, hanging: 360 } } },
|
|
258
|
+
}],
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
reference: "list-benefits", // different name for second list!
|
|
262
|
+
levels: [{ /* same config */ }],
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
// Usage in paragraphs
|
|
268
|
+
new Paragraph({
|
|
269
|
+
numbering: { reference: "list-features", level: 0 },
|
|
270
|
+
children: [new TextRun({ text: "First item" })],
|
|
271
|
+
})
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Bullet Lists
|
|
275
|
+
|
|
276
|
+
```js
|
|
277
|
+
new Paragraph({
|
|
278
|
+
bullet: { level: 0 },
|
|
279
|
+
children: [new TextRun({ text: "Bullet item" })],
|
|
280
|
+
})
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Hyperlinks
|
|
284
|
+
|
|
285
|
+
### External Link
|
|
286
|
+
|
|
287
|
+
```js
|
|
288
|
+
new ExternalHyperlink({
|
|
289
|
+
children: [new TextRun({ text: "Click here", style: "Hyperlink" })],
|
|
290
|
+
link: "https://example.com",
|
|
291
|
+
})
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Internal Link (Bookmark)
|
|
295
|
+
|
|
296
|
+
```js
|
|
297
|
+
// Define bookmark at target
|
|
298
|
+
new Paragraph({
|
|
299
|
+
children: [
|
|
300
|
+
new Bookmark({ id: "section1", children: [new TextRun("Section 1")] }),
|
|
301
|
+
],
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
// Link to bookmark
|
|
305
|
+
new InternalHyperlink({
|
|
306
|
+
children: [new TextRun({ text: "Go to Section 1", style: "Hyperlink" })],
|
|
307
|
+
anchor: "section1",
|
|
308
|
+
})
|
|
309
|
+
```
|
|
310
|
+
## Table of Contents (TOC)
|
|
311
|
+
|
|
312
|
+
**→ See `references/toc.md` for the complete TOC reference.**
|
|
313
|
+
|
|
314
|
+
Quick reminder: (1) Add `TableOfContents` element + PageBreak, (2) Run `python3 "$DOCX_SCRIPTS/add_toc_placeholders.py" output.docx --auto`, (3) Check exit code.
|
|
315
|
+
|
|
316
|
+
## Tabs
|
|
317
|
+
|
|
318
|
+
```js
|
|
319
|
+
new Paragraph({
|
|
320
|
+
tabStops: [
|
|
321
|
+
{ type: TabStopType.RIGHT, position: TabStopPosition.MAX },
|
|
322
|
+
],
|
|
323
|
+
children: [new TextRun("Left"), new TextRun("\t"), new TextRun("Right")]
|
|
324
|
+
})
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Constants Quick Reference
|
|
328
|
+
|
|
329
|
+
- **Underlines:** `SINGLE`, `DOUBLE`, `WAVY`, `DASH`
|
|
330
|
+
- **Borders:** `SINGLE`, `DOUBLE`, `DASHED`, `DOTTED`
|
|
331
|
+
- **Numbering:** `DECIMAL` (1,2,3), `UPPER_ROMAN` (I,II,III), `LOWER_LETTER` (a,b,c)
|
|
332
|
+
- **Symbols:** `"2022"` (•), `"00A9"` (©), `"00AE"` (®), `"2122"` (™)
|