document-content-model 0.0.0 → 1.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/dist/index.js ADDED
@@ -0,0 +1,300 @@
1
+ import { z } from "zod";
2
+ //#region src/color.ts
3
+ const ColorSchema = z.object({
4
+ r: z.number().min(0).max(1),
5
+ g: z.number().min(0).max(1),
6
+ b: z.number().min(0).max(1)
7
+ });
8
+ const COLOR_BLACK = {
9
+ r: 0,
10
+ g: 0,
11
+ b: 0
12
+ };
13
+ const HEX_COLOR_PATTERN = /^#?([0-9a-fA-F]{6})$/;
14
+ const HEX_BYTE_MAX = 255;
15
+ function rgbHexToColor(hex) {
16
+ const match = HEX_COLOR_PATTERN.exec(hex);
17
+ if (match === null) throw new Error(`not a 6-digit hex colour: ${hex}`);
18
+ const digits = match[1];
19
+ if (digits === void 0) throw new Error(`not a 6-digit hex colour: ${hex}`);
20
+ const r = Number.parseInt(digits.slice(0, 2), 16);
21
+ const g = Number.parseInt(digits.slice(2, 4), 16);
22
+ const b = Number.parseInt(digits.slice(4, 6), 16);
23
+ return {
24
+ r: r / HEX_BYTE_MAX,
25
+ g: g / HEX_BYTE_MAX,
26
+ b: b / HEX_BYTE_MAX
27
+ };
28
+ }
29
+ function toHexByte(component) {
30
+ return Math.round(component * HEX_BYTE_MAX).toString(16).padStart(2, "0");
31
+ }
32
+ function colorToRgbHex(color) {
33
+ return `${toHexByte(color.r)}${toHexByte(color.g)}${toHexByte(color.b)}`;
34
+ }
35
+ //#endregion
36
+ //#region src/geometry.ts
37
+ const BoxSchema = z.object({
38
+ xPt: z.number(),
39
+ yPt: z.number(),
40
+ widthPt: z.number().nonnegative(),
41
+ heightPt: z.number().nonnegative()
42
+ });
43
+ const PageSizeSchema = z.object({
44
+ widthPt: z.number().positive(),
45
+ heightPt: z.number().positive()
46
+ });
47
+ const MarginsSchema = z.object({
48
+ topPt: z.number().nonnegative(),
49
+ rightPt: z.number().nonnegative(),
50
+ bottomPt: z.number().nonnegative(),
51
+ leftPt: z.number().nonnegative()
52
+ });
53
+ const PAGE_SIZE_LETTER = {
54
+ widthPt: 612,
55
+ heightPt: 792
56
+ };
57
+ const PAGE_SIZE_A4 = {
58
+ widthPt: 595.28,
59
+ heightPt: 841.89
60
+ };
61
+ const SLIDE_SIZE_WIDESCREEN = {
62
+ widthPt: 960,
63
+ heightPt: 540
64
+ };
65
+ const SLIDE_SIZE_STANDARD = {
66
+ widthPt: 720,
67
+ heightPt: 540
68
+ };
69
+ //#endregion
70
+ //#region src/style.ts
71
+ const AlignmentSchema = z.enum([
72
+ "left",
73
+ "center",
74
+ "right",
75
+ "justify"
76
+ ]);
77
+ const LayoutFontSchema = z.object({
78
+ family: z.string(),
79
+ weight: z.enum(["normal", "bold"]),
80
+ style: z.enum(["normal", "italic"])
81
+ });
82
+ const DEFAULT_LAYOUT_FONT = {
83
+ family: "Helvetica",
84
+ weight: "normal",
85
+ style: "normal"
86
+ };
87
+ //#endregion
88
+ //#region src/metadata.ts
89
+ const LayoutMetadataSchema = z.object({
90
+ title: z.string().optional(),
91
+ author: z.string().optional(),
92
+ subject: z.string().optional(),
93
+ keywords: z.array(z.string()).optional(),
94
+ creator: z.string().optional(),
95
+ producer: z.string().optional(),
96
+ createdIso: z.string().optional(),
97
+ modifiedIso: z.string().optional()
98
+ });
99
+ //#endregion
100
+ //#region src/content.ts
101
+ const ContentRunSchema = z.object({
102
+ text: z.string(),
103
+ bold: z.boolean().optional(),
104
+ italic: z.boolean().optional(),
105
+ underline: z.boolean().optional(),
106
+ strike: z.boolean().optional(),
107
+ fontFamily: z.string().optional(),
108
+ sizePt: z.number().positive().optional(),
109
+ color: ColorSchema.optional(),
110
+ hyperlink: z.string().optional()
111
+ });
112
+ const ContentListMembershipSchema = z.object({
113
+ numId: z.string(),
114
+ level: z.number().int().nonnegative()
115
+ });
116
+ const ContentParagraphSchema = z.object({
117
+ kind: z.literal("paragraph"),
118
+ runs: z.array(ContentRunSchema),
119
+ styleId: z.string().optional(),
120
+ alignment: AlignmentSchema.optional(),
121
+ list: ContentListMembershipSchema.optional(),
122
+ spacingBeforePt: z.number().optional(),
123
+ spacingAfterPt: z.number().optional(),
124
+ lineSpacing: z.number().positive().optional(),
125
+ indentLeftPt: z.number().optional(),
126
+ indentFirstLinePt: z.number().optional()
127
+ });
128
+ const ContentImageBlockSchema = z.object({
129
+ kind: z.literal("image"),
130
+ format: z.enum(["png", "jpeg"]),
131
+ base64: z.string(),
132
+ widthPt: z.number().positive(),
133
+ heightPt: z.number().positive(),
134
+ altText: z.string().optional()
135
+ });
136
+ const ContentPageBreakSchema = z.object({ kind: z.literal("pageBreak") });
137
+ function isRecord(value) {
138
+ return typeof value === "object" && value !== null && !Array.isArray(value);
139
+ }
140
+ function isContentRun(value) {
141
+ return isRecord(value) && typeof value.text === "string";
142
+ }
143
+ function isContentTableCell(value) {
144
+ return isRecord(value) && Array.isArray(value.blocks) && value.blocks.every(isContentBlock);
145
+ }
146
+ function isContentTableRow(value) {
147
+ return isRecord(value) && Array.isArray(value.cells) && value.cells.every(isContentTableCell) && (value.heightPt === void 0 || typeof value.heightPt === "number");
148
+ }
149
+ function isContentBlock(value) {
150
+ if (!isRecord(value)) return false;
151
+ const kind = value.kind;
152
+ if (kind === "paragraph") return Array.isArray(value.runs) && value.runs.every(isContentRun);
153
+ if (kind === "image") return (value.format === "png" || value.format === "jpeg") && typeof value.base64 === "string" && typeof value.widthPt === "number" && typeof value.heightPt === "number";
154
+ if (kind === "pageBreak") return true;
155
+ if (kind === "table") return Array.isArray(value.rows) && value.rows.every(isContentTableRow) && Array.isArray(value.columnWidthsPt) && value.columnWidthsPt.every((w) => typeof w === "number");
156
+ return false;
157
+ }
158
+ const ContentBlockSchema = z.custom(isContentBlock);
159
+ const ContentTableCellSchema = z.object({
160
+ blocks: z.array(ContentBlockSchema),
161
+ colSpan: z.number().int().positive().optional(),
162
+ rowSpan: z.number().int().positive().optional(),
163
+ background: ColorSchema.optional()
164
+ });
165
+ const ContentTableRowSchema = z.object({
166
+ cells: z.array(ContentTableCellSchema),
167
+ heightPt: z.number().positive().optional()
168
+ });
169
+ const ContentTableSchema = z.object({
170
+ kind: z.literal("table"),
171
+ rows: z.array(ContentTableRowSchema),
172
+ columnWidthsPt: z.array(z.number().positive())
173
+ });
174
+ const ContentSectionSchema = z.object({
175
+ pageSize: PageSizeSchema,
176
+ margins: MarginsSchema,
177
+ blocks: z.array(ContentBlockSchema)
178
+ });
179
+ const ContentShapeSchema = z.object({
180
+ name: z.string().optional(),
181
+ frame: BoxSchema,
182
+ rotationDeg: z.number().optional(),
183
+ insetLeftPt: z.number().nonnegative(),
184
+ insetTopPt: z.number().nonnegative(),
185
+ insetRightPt: z.number().nonnegative(),
186
+ insetBottomPt: z.number().nonnegative(),
187
+ fontScale: z.number().positive().optional(),
188
+ lineSpacingReduction: z.number().nonnegative().optional(),
189
+ blocks: z.array(ContentBlockSchema)
190
+ });
191
+ const ContentSlideSchema = z.object({
192
+ size: PageSizeSchema,
193
+ shapes: z.array(ContentShapeSchema),
194
+ notes: z.string()
195
+ });
196
+ const CONTENT_FORMAT_VERSION = 1;
197
+ const ContentDocumentSchema = z.discriminatedUnion("kind", [z.object({
198
+ kind: z.literal("wordprocessing"),
199
+ formatVersion: z.literal(1),
200
+ metadata: LayoutMetadataSchema,
201
+ sections: z.array(ContentSectionSchema)
202
+ }), z.object({
203
+ kind: z.literal("presentation"),
204
+ formatVersion: z.literal(1),
205
+ metadata: LayoutMetadataSchema,
206
+ slides: z.array(ContentSlideSchema)
207
+ })]);
208
+ //#endregion
209
+ //#region src/layout.ts
210
+ const LAYOUT_FORMAT_VERSION = 1;
211
+ const LayoutTextSchema = z.object({
212
+ kind: z.literal("text"),
213
+ text: z.string(),
214
+ xPt: z.number(),
215
+ yPt: z.number(),
216
+ font: LayoutFontSchema,
217
+ sizePt: z.number().positive(),
218
+ color: ColorSchema,
219
+ widthPt: z.number().nonnegative().optional(),
220
+ rotationDeg: z.number().optional(),
221
+ underline: z.boolean().optional()
222
+ });
223
+ const LayoutImageSchema = z.object({
224
+ kind: z.literal("image"),
225
+ imageId: z.string(),
226
+ xPt: z.number(),
227
+ yPt: z.number(),
228
+ widthPt: z.number().positive(),
229
+ heightPt: z.number().positive(),
230
+ rotationDeg: z.number().optional()
231
+ });
232
+ const LayoutRectSchema = z.object({
233
+ kind: z.literal("rect"),
234
+ xPt: z.number(),
235
+ yPt: z.number(),
236
+ widthPt: z.number().nonnegative(),
237
+ heightPt: z.number().nonnegative(),
238
+ fill: ColorSchema.optional(),
239
+ stroke: z.object({
240
+ color: ColorSchema,
241
+ widthPt: z.number().positive()
242
+ }).optional()
243
+ });
244
+ const LayoutLineSchema = z.object({
245
+ kind: z.literal("line"),
246
+ x1Pt: z.number(),
247
+ y1Pt: z.number(),
248
+ x2Pt: z.number(),
249
+ y2Pt: z.number(),
250
+ color: ColorSchema,
251
+ widthPt: z.number().positive()
252
+ });
253
+ const LayoutEllipseSchema = z.object({
254
+ kind: z.literal("ellipse"),
255
+ xPt: z.number(),
256
+ yPt: z.number(),
257
+ widthPt: z.number().positive(),
258
+ heightPt: z.number().positive(),
259
+ fill: ColorSchema.optional(),
260
+ stroke: z.object({
261
+ color: ColorSchema,
262
+ widthPt: z.number().positive()
263
+ }).optional()
264
+ });
265
+ const LayoutLinkSchema = z.object({
266
+ kind: z.literal("link"),
267
+ uri: z.string(),
268
+ xPt: z.number(),
269
+ yPt: z.number(),
270
+ widthPt: z.number().nonnegative(),
271
+ heightPt: z.number().nonnegative()
272
+ });
273
+ const LayoutItemSchema = z.discriminatedUnion("kind", [
274
+ LayoutTextSchema,
275
+ LayoutImageSchema,
276
+ LayoutRectSchema,
277
+ LayoutLineSchema,
278
+ LayoutEllipseSchema,
279
+ LayoutLinkSchema
280
+ ]);
281
+ const LayoutPageSchema = z.object({
282
+ widthPt: z.number().positive(),
283
+ heightPt: z.number().positive(),
284
+ items: z.array(LayoutItemSchema),
285
+ notes: z.string().optional()
286
+ });
287
+ const LayoutImageAssetSchema = z.object({
288
+ format: z.enum(["png", "jpeg"]),
289
+ base64: z.string(),
290
+ widthPx: z.number().int().positive(),
291
+ heightPx: z.number().int().positive()
292
+ });
293
+ const LayoutDocumentSchema = z.object({
294
+ formatVersion: z.literal(1),
295
+ metadata: LayoutMetadataSchema,
296
+ pages: z.array(LayoutPageSchema),
297
+ images: z.record(z.string(), LayoutImageAssetSchema)
298
+ });
299
+ //#endregion
300
+ export { AlignmentSchema, BoxSchema, COLOR_BLACK, CONTENT_FORMAT_VERSION, ColorSchema, ContentBlockSchema, ContentDocumentSchema, ContentImageBlockSchema, ContentListMembershipSchema, ContentPageBreakSchema, ContentParagraphSchema, ContentRunSchema, ContentSectionSchema, ContentShapeSchema, ContentSlideSchema, ContentTableCellSchema, ContentTableRowSchema, ContentTableSchema, DEFAULT_LAYOUT_FONT, LAYOUT_FORMAT_VERSION, LayoutDocumentSchema, LayoutEllipseSchema, LayoutFontSchema, LayoutImageAssetSchema, LayoutImageSchema, LayoutItemSchema, LayoutLineSchema, LayoutLinkSchema, LayoutMetadataSchema, LayoutPageSchema, LayoutRectSchema, LayoutTextSchema, MarginsSchema, PAGE_SIZE_A4, PAGE_SIZE_LETTER, PageSizeSchema, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, colorToRgbHex, isContentBlock, rgbHexToColor };
package/package.json CHANGED
@@ -1,5 +1,88 @@
1
1
  {
2
2
  "name": "document-content-model",
3
- "version": "0.0.0",
4
- "private": false
3
+ "version": "1.0.1",
4
+ "description": "The canonical, format-agnostic content and layout schemas shared by ooxml.js, odf.js, and documents.js -- pure Zod schemas, no behaviour.",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/ExaDev/document-content-model.git"
9
+ },
10
+ "homepage": "https://github.com/ExaDev/document-content-model#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/ExaDev/document-content-model/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:coverage": "vitest run --project unit --coverage",
47
+ "test:smoke": "tsdown && vitest run --project smoke",
48
+ "prepare": "husky",
49
+ "release": "semantic-release"
50
+ },
51
+ "keywords": [
52
+ "document",
53
+ "content-model",
54
+ "zod",
55
+ "schema",
56
+ "docx",
57
+ "pptx",
58
+ "xlsx",
59
+ "odt",
60
+ "odp",
61
+ "ods"
62
+ ],
63
+ "license": "MIT",
64
+ "packageManager": "pnpm@11.6.0",
65
+ "dependencies": {
66
+ "zod": "^4.4.3"
67
+ },
68
+ "devDependencies": {
69
+ "@arethetypeswrong/cli": "^0.18.5",
70
+ "@commitlint/cli": "^21.2.1",
71
+ "@commitlint/config-conventional": "^21.2.0",
72
+ "@eslint/js": "^10.0.1",
73
+ "@semantic-release/changelog": "^7.0.0",
74
+ "@semantic-release/git": "^11.0.1",
75
+ "@types/node": "^26.1.1",
76
+ "@vitest/coverage-v8": "^4.1.10",
77
+ "eslint": "^10.8.0",
78
+ "globals": "^17.8.0",
79
+ "husky": "^9.1.7",
80
+ "lint-staged": "^17.2.0",
81
+ "publint": "^0.3.21",
82
+ "semantic-release": "^25.0.8",
83
+ "tsdown": "^0.22.13",
84
+ "typescript": "^6.0.3",
85
+ "typescript-eslint": "^8.65.0",
86
+ "vitest": "^4.1.10"
87
+ }
5
88
  }