documents.js 0.0.0 → 1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joseph Mearman
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,76 @@
1
+ # documents.js
2
+
3
+ > Bidirectional docx/pptx ⇄ PDF conversion and a read+write editable OOXML document model, built on [ooxml.js](https://github.com/ExaDev/ooxml.js) and Zod 4 codecs.
4
+
5
+ `documents.js` depends on `ooxml.js` for lossless docx/pptx/xlsx ⇄ JSON handling and extends it in two directions `ooxml.js` deliberately does not cover: full PDF support, and a read-**and-write** manipulation API for docx/pptx content (`ooxml.js`'s own typed readers are one-way and explicitly forbid write-back). PDF reading, writing, and the docx⇄pdf/pptx⇄pdf conversion codecs are entirely hand-written — no external PDF library is a dependency.
6
+
7
+ **Status: early bootstrap.** This repository currently contains only project scaffolding; the source tree, tooling, and package have not been built yet.
8
+
9
+ ## Getting started
10
+
11
+ Requires Node.js `>=20` and pnpm `11.6.0` (pinned via `packageManager` in `package.json`, once it exists).
12
+
13
+ ```sh
14
+ pnpm install
15
+ ```
16
+
17
+ Install as a dependency in another project:
18
+
19
+ ```sh
20
+ pnpm add documents.js
21
+ # or
22
+ npm install documents.js
23
+ ```
24
+
25
+ ## Build, test, and lint
26
+
27
+ Once the tooling scaffold lands, the scripts mirror `ooxml.js` exactly:
28
+
29
+ ```sh
30
+ pnpm build # tsdown -> dist/ (ESM + CJS + .d.ts)
31
+ pnpm typecheck # tsc --noEmit
32
+ pnpm lint # eslint . --max-warnings 0
33
+ pnpm test # vitest run --project unit
34
+ pnpm test:watch # vitest --project unit
35
+ pnpm test:smoke # rebuilds dist/, then verifies ESM/CJS parity
36
+ pnpm test:corpus # optional real-world PDF conformance checks against a local, gitignored test/corpus/
37
+ ```
38
+
39
+ To run a single test file: `pnpm vitest run src/path/to/file.test.ts`.
40
+
41
+ ## Architecture
42
+
43
+ The package is layered from a lossless OOXML core (delegated entirely to `ooxml.js`) outward to conversion and editing:
44
+
45
+ - **`src/model/`** — Zod schemas only: unit conversions (EMU/twip/point/half-point), geometry, color, and the two pivot models — `LayoutDocument` (the PDF-side pivot: pages of positioned text/image/rect/line/ellipse/link items, PDF-native coordinates) and `ContentDocument` (the semantic pivot: a discriminated union of `wordprocessing` and `presentation` variants sharing paragraph/run/table/image building blocks).
46
+ - **`src/bytes/`** and **`src/image/`** — generic byte and image-container primitives (PNG decode/encode, JPEG marker scanning) with zero PDF or OOXML knowledge. `src/bytes/flate.ts` is the only file that imports `fflate`, mirroring how `ooxml.js`'s own `src/zip.ts` wraps `fflate` for ZIP handling.
47
+ - **`src/xml/`** and **`src/opc/`** — parent-aware XML query/mutation and OPC package mechanics (relationships, content types, media parts) built over `ooxml.js`'s `Package`/`XmlNode`.
48
+ - **`src/edit/`** — the read+write editable model: live-view wrappers over the actual `XmlElement` objects inside a decoded `Package`, so mutations edit the tree in place and untouched content stays byte-faithful on save. This is the novel piece beyond what `ooxml.js` itself provides.
49
+ - **`src/pdf/`** — a fully hand-written PDF codec: object model, writer (content-stream generation, standard-14 font metrics, xref table), and reader (tokenizer, cross-reference/object-stream resolution, content-stream interpreter, font/Unicode recovery). No external PDF library.
50
+ - **`src/ooxml/`** — resolves a `Package` into a `ContentDocument`: the docx style cascade (`basedOn` chains, theme fonts, toggle properties) and the pptx placeholder→layout→master→theme inheritance cascade.
51
+ - **`src/layout/`** — the conversion algorithms: `ContentDocument → LayoutDocument` (docx flow/pagination; pptx direct EMU-to-point placement) and the reverse (`LayoutDocument → ContentDocument`, via line/paragraph/shape clustering).
52
+ - **`src/convert/`** — the `DocumentConverter` port/contract and its local adapter, plus the `docxPdfCodec`/`pptxPdfCodec` Zod codecs and ergonomic `docxToPdf`/`pdfToDocx`/`pptxToPdf`/`pdfToPptx` wrappers.
53
+
54
+ ## Conventions
55
+
56
+ - **Zod-first schema/type/guard**, matching `ooxml.js`: every model type is inferred from its Zod schema, never hand-written. Recursive types (`ContentBlock`, mirroring `ooxml.js`'s `XmlNode`) use a hand-written structural guard + `z.custom`, not `z.lazy`.
57
+ - **No type assertions.** Every third-party or loosely-typed value (from `fast-xml-parser` via `ooxml.js`) is narrowed through a type guard or a Zod parse at the boundary. `src/pdf/`'s own object model narrows natively on its `kind` discriminant, so it needs no such guard.
58
+ - **Dependency direction is strictly downward and checkable**: `model`/`bytes` import nothing local; `image` imports `bytes` only; `pdf` imports `model`+`bytes`+`image` only (no OOXML knowledge); `ooxml/*` imports `xml`/`model` only (no PDF knowledge); `layout` imports `model` only (pure, no I/O); `convert` composes everything else. No `PdfObject`/`PdfDict`/`PdfStream` type may appear outside `src/pdf/`.
59
+ - **Conventional commits**, enforced via commitlint + husky, matching `ooxml.js`.
60
+
61
+ ## Gotchas and quirks
62
+
63
+ - **`ooxml.js`'s typed readers (`readDocx`/`readPptx`) are not used as a basis for conversion.** They flatten body content with a recursive-descendant search (destroying document order for paragraphs inside tables) and carry no font/size/color/geometry data. `documents.js` walks `word/document.xml`/`ppt/slides/slideN.xml` directly.
64
+ - **The docx⇄pdf and pptx⇄pdf conversion codecs are explicitly not round-trip-lossless** — in deliberate contrast to `ooxml.js`'s own `packageCodec`, which is. A `z.codec()` here means a validated, named pair of format-*converting* transforms, not a lossless round-trip guarantee.
65
+ - **PDF output uses standard-14 fonts only (no embedding).** Helvetica/Times-Roman are metric-compatible substitutes for Arial/Times New Roman, but Word's actual default fonts (Calibri, Aptos) are not — expect a faithful visual approximation, not a line-identical reproduction of Word/PowerPoint's own rendering.
66
+ - **Reading arbitrary real-world PDFs is the hardest part of this package.** The hand-written parser targets cleanly-generated output from mainstream producers (Word, PowerPoint, Chrome, LibreOffice, Acrobat) and fails loudly and specifically on adversarial or badly malformed files, rather than matching a mature library's robustness.
67
+ - **Encrypted PDFs are unsupported** (`/Encrypt` present → throws), including the common empty-user-password case.
68
+ - **JPEG images pass through losslessly** (embedded/extracted via PDF's `DCTDecode` filter with no decode/re-encode); PNG-sourced images go through a real, narrowly-scoped hand-written codec.
69
+
70
+ ## References
71
+
72
+ - [ooxml.js](https://github.com/ExaDev/ooxml.js) — the sibling package this depends on for all docx/pptx/xlsx ⇄ JSON handling.
73
+
74
+ ## License
75
+
76
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,14 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let ooxml_js = require("ooxml.js");
3
+ Object.defineProperty(exports, "decodePackage", {
4
+ enumerable: true,
5
+ get: function() {
6
+ return ooxml_js.decodePackage;
7
+ }
8
+ });
9
+ Object.defineProperty(exports, "encodePackage", {
10
+ enumerable: true,
11
+ get: function() {
12
+ return ooxml_js.encodePackage;
13
+ }
14
+ });
@@ -0,0 +1,2 @@
1
+ import { decodePackage, encodePackage } from "ooxml.js";
2
+ export { decodePackage, encodePackage };
@@ -0,0 +1,2 @@
1
+ import { decodePackage, encodePackage } from "ooxml.js";
2
+ export { decodePackage, encodePackage };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import { decodePackage, encodePackage } from "ooxml.js";
2
+ export { decodePackage, encodePackage };
package/package.json CHANGED
@@ -1,5 +1,89 @@
1
1
  {
2
2
  "name": "documents.js",
3
- "version": "0.0.0",
4
- "private": false
3
+ "version": "1.1.0",
4
+ "description": "Bidirectional docx/pptx <-> PDF conversion and a read+write editable OOXML document model, built on ooxml.js and Zod 4 codecs.",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/ExaDev/documents.js.git"
9
+ },
10
+ "homepage": "https://github.com/ExaDev/documents.js#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/ExaDev/documents.js/issues"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "types": {
17
+ "import": "./dist/index.d.ts",
18
+ "require": "./dist/index.d.cts"
19
+ },
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs"
22
+ }
23
+ },
24
+ "main": "./dist/index.cjs",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "provenance": true,
33
+ "registry": "https://registry.npmjs.org/"
34
+ },
35
+ "sideEffects": false,
36
+ "engines": {
37
+ "node": ">=20"
38
+ },
39
+ "scripts": {
40
+ "build": "tsdown",
41
+ "prepublishOnly": "pnpm run lint && pnpm run typecheck && tsdown && publint && attw --pack",
42
+ "lint": "eslint . --max-warnings 0",
43
+ "typecheck": "tsc --noEmit",
44
+ "test": "vitest run --project unit",
45
+ "test:watch": "vitest --project unit",
46
+ "test:corpus": "vitest run --project corpus",
47
+ "test:smoke": "tsdown && vitest run --project smoke",
48
+ "prepare": "husky",
49
+ "release": "semantic-release"
50
+ },
51
+ "keywords": [
52
+ "docx",
53
+ "pptx",
54
+ "pdf",
55
+ "docx-to-pdf",
56
+ "pdf-to-docx",
57
+ "pptx-to-pdf",
58
+ "ooxml",
59
+ "document-conversion",
60
+ "zod",
61
+ "codec"
62
+ ],
63
+ "license": "MIT",
64
+ "packageManager": "pnpm@11.6.0",
65
+ "dependencies": {
66
+ "ooxml.js": "^1.3.0",
67
+ "fflate": "^0.8.3",
68
+ "zod": "^4.4.3"
69
+ },
70
+ "devDependencies": {
71
+ "@arethetypeswrong/cli": "^0.18.5",
72
+ "@commitlint/cli": "^21.2.1",
73
+ "@commitlint/config-conventional": "^21.2.0",
74
+ "@eslint/js": "^10.0.1",
75
+ "@semantic-release/changelog": "^7.0.0",
76
+ "@semantic-release/git": "^11.0.1",
77
+ "@types/node": "^26.1.1",
78
+ "eslint": "^10.8.0",
79
+ "globals": "^17.8.0",
80
+ "husky": "^9.1.7",
81
+ "lint-staged": "^17.2.0",
82
+ "publint": "^0.3.21",
83
+ "semantic-release": "^25.0.8",
84
+ "tsdown": "^0.22.13",
85
+ "typescript": "^6.0.3",
86
+ "typescript-eslint": "^8.65.0",
87
+ "vitest": "^4.1.10"
88
+ }
5
89
  }