bun-docx 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/LICENSE +21 -0
- package/README.md +150 -0
- package/dist/index.js +17606 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kirill Klimuk
|
|
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,150 @@
|
|
|
1
|
+
# docx-cli
|
|
2
|
+
|
|
3
|
+
A Bun-built CLI for AI agents (Claude, Codex) to read, edit, and comment on `.docx` files with full format fidelity. Outputs JSON-AST for precise locator-based editing; preserves anything it doesn't model by mutating XML in place.
|
|
4
|
+
|
|
5
|
+
## Stack
|
|
6
|
+
|
|
7
|
+
- **Runtime**: Bun (`node:util` parseArgs, JSX with custom factory, native zlib)
|
|
8
|
+
- **Parser**: [`jszip`](https://www.npmjs.com/package/jszip) + [`fast-xml-parser`](https://www.npmjs.com/package/fast-xml-parser) + [`fast-xml-builder`](https://www.npmjs.com/package/fast-xml-builder)
|
|
9
|
+
- **Quality**: Biome + Knip + tsc; LibreOffice headless for round-trip integration tests
|
|
10
|
+
- **Standard**: ECMA-376 Part 1 §17 (WordprocessingML), Transitional profile
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
**Standalone binary** (no Bun required):
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
curl -fsSL https://raw.githubusercontent.com/kklimuk/docx-cli/main/install.sh | sh
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Honors `PREFIX` (default `$HOME/.local/bin`) and `VERSION` (default `latest`):
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
PREFIX=/usr/local sh -c "$(curl -fsSL https://raw.githubusercontent.com/kklimuk/docx-cli/main/install.sh)"
|
|
24
|
+
VERSION=v0.2.0 sh -c "$(curl -fsSL https://raw.githubusercontent.com/kklimuk/docx-cli/main/install.sh)"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Pre-built binaries are published for linux/x64, linux/arm64, darwin/x64, darwin/arm64, windows/x64.
|
|
28
|
+
|
|
29
|
+
**npm** (requires Bun >= 1.3):
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
bun add -g bun-docx
|
|
33
|
+
# or
|
|
34
|
+
bunx bun-docx read doc.docx
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Commands
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
docx create FILE [--title T] [--author A] [--text "..."]
|
|
41
|
+
docx read FILE
|
|
42
|
+
docx insert FILE --after p3 --text "..." [--style HeadingN] [--color HEX] [--bold] [--italic]
|
|
43
|
+
docx insert FILE --after p3 --runs '[{"type":"text","text":"X","bold":true}]'
|
|
44
|
+
docx edit FILE --at p3 --text "..." | --runs '[...]'
|
|
45
|
+
docx delete FILE --at p3
|
|
46
|
+
|
|
47
|
+
docx comments add FILE --range p3:5-20 --text "..." [--author NAME]
|
|
48
|
+
docx comments reply FILE --to c0 --text "..."
|
|
49
|
+
docx comments resolve FILE --id c0 [--unset]
|
|
50
|
+
docx comments delete FILE --id c0
|
|
51
|
+
docx comments restore FILE --id c0
|
|
52
|
+
docx comments list FILE [--include-resolved] [--thread c0]
|
|
53
|
+
|
|
54
|
+
docx images list FILE
|
|
55
|
+
docx images extract FILE --to ./media [--id imgN]
|
|
56
|
+
docx images replace FILE --at imgN --with ./new.png
|
|
57
|
+
|
|
58
|
+
docx track-changes FILE on|off
|
|
59
|
+
docx schema [--ts]
|
|
60
|
+
docx locators [--json]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Every command has `--help`. All mutating commands accept `--dry-run`. JSON output by default for `read` and `*.list`; structured `{ok, code, error, hint}` on failure.
|
|
64
|
+
|
|
65
|
+
### Locators
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
pN paragraph N (e.g., p3)
|
|
69
|
+
pN:S-E characters S..E within paragraph N
|
|
70
|
+
pN:S-pM:E cross-paragraph range
|
|
71
|
+
tN table N; tN:rRcC for cell at row R, col C
|
|
72
|
+
cN, imgN comment / image ids
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Run `docx locators` for the full reference.
|
|
76
|
+
|
|
77
|
+
## Development
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
bun install && bun run prepare # set up + git hooks
|
|
81
|
+
bun dev <subcommand> # run via source
|
|
82
|
+
bun run check # biome + knip + tsc
|
|
83
|
+
bun run test:unit # core + cli tests (fast)
|
|
84
|
+
bun run test:integration # LibreOffice round-trip (needs `soffice` on PATH)
|
|
85
|
+
bun test # everything
|
|
86
|
+
bun run build # produce dist/docx via bun build --compile
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### LibreOffice (for integration tests)
|
|
90
|
+
|
|
91
|
+
- **macOS**: `brew install --cask libreoffice`
|
|
92
|
+
- **Linux**: `sudo apt-get install libreoffice-core libreoffice-writer`
|
|
93
|
+
- **Windows**: <https://www.libreoffice.org/download/>
|
|
94
|
+
|
|
95
|
+
## Architecture
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
src/
|
|
99
|
+
index.ts # binary entrypoint
|
|
100
|
+
cli/
|
|
101
|
+
index.ts # parseArgs dispatch
|
|
102
|
+
help.ts # top-level --help
|
|
103
|
+
respond.ts # JSON ack / structured error helpers
|
|
104
|
+
create/ # create FILE
|
|
105
|
+
read/ # read FILE
|
|
106
|
+
insert/ # insert FILE (uses ./emit Paragraph component)
|
|
107
|
+
edit/ # edit FILE
|
|
108
|
+
delete/ # delete FILE
|
|
109
|
+
comments/ # add | reply | resolve | delete | restore | list
|
|
110
|
+
images/ # list | extract | replace
|
|
111
|
+
track-changes/ # FILE on|off
|
|
112
|
+
schema/ # AST JSON Schema + TS source
|
|
113
|
+
locators-cmd/ # grammar reference
|
|
114
|
+
core/
|
|
115
|
+
package/ # JSZip open/close, named-part read/write
|
|
116
|
+
parser/ # XmlNode class + parse/serialize + JSX factory
|
|
117
|
+
jsx/ # h, Fragment, namespaces (w, r, a, wp, pic, ...)
|
|
118
|
+
ast/ # types + DocView + XML→AST reader
|
|
119
|
+
locators/ # parse "p3:5-20" + resolve to refs
|
|
120
|
+
tests/
|
|
121
|
+
core/, cli/, integration/ # 51 tests, 9 files
|
|
122
|
+
fixtures/ # 11 .docx fixtures across producers
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## How It Works
|
|
126
|
+
|
|
127
|
+
**In-place XML mutation.** The AST returned by `read` is a *view* over the parsed XML tree, not a separate model. When you `edit` or `comments add`, we mutate the underlying XML nodes directly and serialize back. Anything we don't model in the AST (custom styles, theme colors, schema extensions) survives because we never re-emit untouched regions.
|
|
128
|
+
|
|
129
|
+
**JSX for emitters.** Constructing OOXML fragments imperatively (`<w:rPr>` → `<w:b/>` → `<w:color w:val="800080"/>`) gets verbose. We write emitters in JSX with a custom factory: `<w.rPr><w.b/><w.color w-val="800080"/></w.rPr>` becomes the right `XmlNode` tree. Component names are PascalCase (`<Paragraph>`, `<RunProperties>`); they return `XmlNode | null` so empty wrappers get omitted automatically.
|
|
130
|
+
|
|
131
|
+
**Span-aware comments.** `comments add --range p3:5-20` finds the runs that contain offsets 5 and 20, splits them at the boundaries (preserving rPr formatting on both halves), and inserts `<w:commentRangeStart>` / `<w:commentRangeEnd>` markers between the slices. The `<w:commentReference>` run goes after the end marker.
|
|
132
|
+
|
|
133
|
+
**ParaId auto-injection.** Comments authored by tools like mammoth or older Word versions lack `w14:paraId`, which `commentsExtended.xml` requires for resolve/reply. We detect this on resolve/reply and inject a fresh paraId, also adding the `xmlns:w14` namespace declaration to the `<w:comments>` root if missing.
|
|
134
|
+
|
|
135
|
+
**Cross-format image replacement.** `images replace --at img0 --with new.png` detects the new MIME type via `Bun.file().type`, renames the part (`word/media/image1.jpeg` → `word/media/image1.png`), rewrites the relationship `Target`, and ensures `[Content_Types].xml` has a `<Default>` for the new extension.
|
|
136
|
+
|
|
137
|
+
**Trash for restore.** `comments delete` journals the removed comment XML + anchor info to `<dir>/.docx-cli/trash.json` so `comments restore --id cN` can re-anchor it at its original location.
|
|
138
|
+
|
|
139
|
+
## CI
|
|
140
|
+
|
|
141
|
+
GitHub Actions (`.github/workflows/ci.yml`) runs four jobs on push to `main` and on PRs:
|
|
142
|
+
|
|
143
|
+
| Job | What |
|
|
144
|
+
|---|---|
|
|
145
|
+
| `check` | `biome check . && knip-bun && tsc --noEmit` |
|
|
146
|
+
| `unit-tests` | `bun run test:unit` (core + cli, fast) |
|
|
147
|
+
| `integration-tests` | Installs LibreOffice, runs `bun run test:integration` |
|
|
148
|
+
| `build-binary` | Smoke-builds via `bun build --compile` and runs `--version` |
|
|
149
|
+
|
|
150
|
+
`.github/workflows/release.yml` triggers on `v*` tags, matrix-builds the five binaries, and uploads them to a GitHub Release via [`softprops/action-gh-release`](https://github.com/softprops/action-gh-release).
|