document-schema.js 1.6.0 → 1.7.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/README.md +30 -1
- package/dist/index.cjs +90 -7
- package/dist/index.d.cts +46 -13
- package/dist/index.d.ts +46 -13
- package/dist/index.js +83 -8
- package/package.json +1 -1
- package/schemas/content-document.schema.json +3 -3
- package/schemas/document-package.schema.json +3 -3
- package/schemas/layout-document.schema.json +1 -1
package/README.md
CHANGED
|
@@ -40,7 +40,36 @@ node_modules/document-schema.js/schemas/content-document.schema.json
|
|
|
40
40
|
node_modules/document-schema.js/schemas/layout-document.schema.json
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
Each file's `$id` is a
|
|
43
|
+
Each file's `$id` is a `https://cdn.jsdelivr.net/npm/document-schema.js@<version>/schemas/<file>` URL, pinned to the exact npm version that generated it -- immutable (jsdelivr serves each version's own published tarball contents forever, with an immutable cache header) and genuinely live the moment that version is published, unlike a commit-SHA-pinned raw GitHub URL would be: a gitignored, generated file can never actually be committed at the commit whose SHA it would need to embed, since committing it changes the tree and thus the hash. The three files are cross-referenced via real `$ref`s (e.g. `document-package.schema.json`'s `content`/`layout` properties `$ref` the other two files directly, at that same version), so a JSON Schema validator that resolves `$ref`s over HTTP (or against local copies of all three files) can validate a whole `DocumentPackage` value. `content-document.schema.json` additionally carries a `$defs` block for the recursive paragraph/table/embedded-object block model, which Zod's own converter can't express directly (see that script's own top-of-file comment for why).
|
|
44
|
+
|
|
45
|
+
### Self-describing JSON
|
|
46
|
+
|
|
47
|
+
`documentPackageWithSchema`/`contentDocumentWithSchema`/`layoutDocumentWithSchema` each take a real `DocumentPackage`/`ContentDocument`/`LayoutDocument` value and return the same value with a `$schema` property added, pointing at the `.schema.json` file above for the *currently installed* package version:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { documentPackageWithSchema } from 'document-schema.js';
|
|
51
|
+
|
|
52
|
+
const tagged = documentPackageWithSchema(pkg);
|
|
53
|
+
// { $schema: 'https://cdn.jsdelivr.net/npm/document-schema.js@1.6.1/schemas/document-package.schema.json', formatVersion: 1, content: {...}, layout: {...} }
|
|
54
|
+
writeFileSync('package.json.doc', JSON.stringify(tagged, null, 2));
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
A caller who already knows the kind can keep ingesting with the existing schemas directly -- `DocumentPackageSchema.parse(value)` (etc.) already tolerates and silently strips an incoming `$schema` property, since none of these schemas are `.strict()`. `documentFromJson` exists for the "don't yet know the kind" case: it reads `$schema` to decide which of the three schemas to run, then that schema does the real structural validation:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { documentFromJson, UnrecognizedDocumentSchemaError } from 'document-schema.js';
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
const { kind, value } = documentFromJson(JSON.parse(readFileSync('some-file.json', 'utf8')));
|
|
64
|
+
// kind: 'DocumentPackage' | 'ContentDocument' | 'LayoutDocument'
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error instanceof UnrecognizedDocumentSchemaError) {
|
|
67
|
+
console.error('not a document-schema.js value:', error.schema);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`documentSchemaKindOf(value)` is the lower-level building block `documentFromJson` uses internally -- exported on its own for a caller that only wants to know which kind a value claims to be (version-agnostically: a `$schema` from an older or newer installed version still resolves), without also parsing it. `schemaUriFor(kind)` is the URL builder itself, also exported directly. Deliberately not added: a JSON-Schema-validator dependency (e.g. `ajv`) for ingest -- the generated `.schema.json` files are already a strictly *weaker* approximation of the real Zod schemas (see the two hand-authored `$defs` fragments above), so re-validating against them on ingest would be a fidelity regression, not an improvement.
|
|
44
73
|
|
|
45
74
|
## Used by
|
|
46
75
|
|
package/dist/index.cjs
CHANGED
|
@@ -141,29 +141,29 @@ const ContentPageBreakSchema = zod.z.object({
|
|
|
141
141
|
kind: zod.z.literal("pageBreak"),
|
|
142
142
|
sourcePath: zod.z.string().optional()
|
|
143
143
|
});
|
|
144
|
-
function isRecord(value) {
|
|
144
|
+
function isRecord$1(value) {
|
|
145
145
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
146
146
|
}
|
|
147
147
|
function isContentRun(value) {
|
|
148
|
-
return isRecord(value) && typeof value.text === "string";
|
|
148
|
+
return isRecord$1(value) && typeof value.text === "string";
|
|
149
149
|
}
|
|
150
150
|
function isContentTableCell(value) {
|
|
151
|
-
return isRecord(value) && Array.isArray(value.blocks) && value.blocks.every(isContentBlock);
|
|
151
|
+
return isRecord$1(value) && Array.isArray(value.blocks) && value.blocks.every(isContentBlock);
|
|
152
152
|
}
|
|
153
153
|
function isContentTableRow(value) {
|
|
154
|
-
return isRecord(value) && Array.isArray(value.cells) && value.cells.every(isContentTableCell) && (value.heightPt === void 0 || typeof value.heightPt === "number");
|
|
154
|
+
return isRecord$1(value) && Array.isArray(value.cells) && value.cells.every(isContentTableCell) && (value.heightPt === void 0 || typeof value.heightPt === "number");
|
|
155
155
|
}
|
|
156
156
|
function isContentEmbeddedObjectKind(value) {
|
|
157
157
|
return value === "formula" || value === "wordprocessing" || value === "presentation" || value === "spreadsheet" || value === "drawing";
|
|
158
158
|
}
|
|
159
159
|
function isContentEmbeddedObject(value) {
|
|
160
|
-
return isRecord(value) && isContentEmbeddedObjectKind(value.objectKind) && BoxSchema.safeParse(value.frame).success && ContentDocumentSchema.safeParse(value.document).success;
|
|
160
|
+
return isRecord$1(value) && isContentEmbeddedObjectKind(value.objectKind) && BoxSchema.safeParse(value.frame).success && ContentDocumentSchema.safeParse(value.document).success;
|
|
161
161
|
}
|
|
162
162
|
function isContentEmbeddedObjectBlock(value) {
|
|
163
|
-
return isRecord(value) && value.kind === "embeddedObject" && isContentEmbeddedObject(value);
|
|
163
|
+
return isRecord$1(value) && value.kind === "embeddedObject" && isContentEmbeddedObject(value);
|
|
164
164
|
}
|
|
165
165
|
function isContentBlock(value) {
|
|
166
|
-
if (!isRecord(value)) return false;
|
|
166
|
+
if (!isRecord$1(value)) return false;
|
|
167
167
|
const kind = value.kind;
|
|
168
168
|
if (kind === "paragraph") return Array.isArray(value.runs) && value.runs.every(isContentRun);
|
|
169
169
|
if (kind === "image") return (value.format === "png" || value.format === "jpeg") && typeof value.base64 === "string" && typeof value.widthPt === "number" && typeof value.heightPt === "number";
|
|
@@ -537,6 +537,81 @@ const DocumentPackageSchema = zod.z.object({
|
|
|
537
537
|
layout: LayoutDocumentSchema.optional()
|
|
538
538
|
});
|
|
539
539
|
//#endregion
|
|
540
|
+
//#region src/schema-io.ts
|
|
541
|
+
const SCHEMA_FILE_NAMES = {
|
|
542
|
+
DocumentPackage: "document-package.schema.json",
|
|
543
|
+
ContentDocument: "content-document.schema.json",
|
|
544
|
+
LayoutDocument: "layout-document.schema.json"
|
|
545
|
+
};
|
|
546
|
+
function schemaUriFor(kind) {
|
|
547
|
+
return `https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/${SCHEMA_FILE_NAMES[kind]}`;
|
|
548
|
+
}
|
|
549
|
+
const SCHEMA_URI_PATTERN = /^https:\/\/cdn\.jsdelivr\.net\/npm\/document-schema\.js@[^/]+\/schemas\/(document-package|content-document|layout-document)\.schema\.json$/;
|
|
550
|
+
function kindForFileStem(stem) {
|
|
551
|
+
switch (stem) {
|
|
552
|
+
case "document-package": return "DocumentPackage";
|
|
553
|
+
case "content-document": return "ContentDocument";
|
|
554
|
+
case "layout-document": return "LayoutDocument";
|
|
555
|
+
default: return;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function isRecord(value) {
|
|
559
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
560
|
+
}
|
|
561
|
+
function documentSchemaKindOf(value) {
|
|
562
|
+
if (!isRecord(value)) return void 0;
|
|
563
|
+
if (!("$schema" in value) || typeof value.$schema !== "string") return void 0;
|
|
564
|
+
const match = SCHEMA_URI_PATTERN.exec(value.$schema);
|
|
565
|
+
if (match === null) return void 0;
|
|
566
|
+
const stem = match[1];
|
|
567
|
+
if (stem === void 0) return void 0;
|
|
568
|
+
return kindForFileStem(stem);
|
|
569
|
+
}
|
|
570
|
+
function documentPackageWithSchema(value) {
|
|
571
|
+
return {
|
|
572
|
+
$schema: schemaUriFor("DocumentPackage"),
|
|
573
|
+
...value
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function contentDocumentWithSchema(value) {
|
|
577
|
+
return {
|
|
578
|
+
$schema: schemaUriFor("ContentDocument"),
|
|
579
|
+
...value
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
function layoutDocumentWithSchema(value) {
|
|
583
|
+
return {
|
|
584
|
+
$schema: schemaUriFor("LayoutDocument"),
|
|
585
|
+
...value
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
var UnrecognizedDocumentSchemaError = class extends Error {
|
|
589
|
+
schema;
|
|
590
|
+
constructor(schema) {
|
|
591
|
+
super(`documentFromJson: value has no recognized "$schema" property (expected one of the three document-schema.js .schema.json URIs; found: ${JSON.stringify(schema)}).`);
|
|
592
|
+
this.name = "UnrecognizedDocumentSchemaError";
|
|
593
|
+
this.schema = schema;
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
function documentFromJson(value) {
|
|
597
|
+
const kind = documentSchemaKindOf(value);
|
|
598
|
+
if (kind === void 0) throw new UnrecognizedDocumentSchemaError(isRecord(value) ? value.$schema : void 0);
|
|
599
|
+
switch (kind) {
|
|
600
|
+
case "DocumentPackage": return {
|
|
601
|
+
kind,
|
|
602
|
+
value: DocumentPackageSchema.parse(value)
|
|
603
|
+
};
|
|
604
|
+
case "ContentDocument": return {
|
|
605
|
+
kind,
|
|
606
|
+
value: ContentDocumentSchema.parse(value)
|
|
607
|
+
};
|
|
608
|
+
case "LayoutDocument": return {
|
|
609
|
+
kind,
|
|
610
|
+
value: LayoutDocumentSchema.parse(value)
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
//#endregion
|
|
540
615
|
exports.AlignmentSchema = AlignmentSchema;
|
|
541
616
|
exports.BoxSchema = BoxSchema;
|
|
542
617
|
exports.COLOR_BLACK = COLOR_BLACK;
|
|
@@ -595,8 +670,16 @@ exports.MarginsSchema = MarginsSchema;
|
|
|
595
670
|
exports.PAGE_SIZE_A4 = PAGE_SIZE_A4;
|
|
596
671
|
exports.PAGE_SIZE_LETTER = PAGE_SIZE_LETTER;
|
|
597
672
|
exports.PageSizeSchema = PageSizeSchema;
|
|
673
|
+
exports.SCHEMA_FILE_NAMES = SCHEMA_FILE_NAMES;
|
|
598
674
|
exports.SLIDE_SIZE_STANDARD = SLIDE_SIZE_STANDARD;
|
|
599
675
|
exports.SLIDE_SIZE_WIDESCREEN = SLIDE_SIZE_WIDESCREEN;
|
|
676
|
+
exports.UnrecognizedDocumentSchemaError = UnrecognizedDocumentSchemaError;
|
|
600
677
|
exports.colorToRgbHex = colorToRgbHex;
|
|
678
|
+
exports.contentDocumentWithSchema = contentDocumentWithSchema;
|
|
679
|
+
exports.documentFromJson = documentFromJson;
|
|
680
|
+
exports.documentPackageWithSchema = documentPackageWithSchema;
|
|
681
|
+
exports.documentSchemaKindOf = documentSchemaKindOf;
|
|
601
682
|
exports.isContentBlock = isContentBlock;
|
|
683
|
+
exports.layoutDocumentWithSchema = layoutDocumentWithSchema;
|
|
602
684
|
exports.rgbHexToColor = rgbHexToColor;
|
|
685
|
+
exports.schemaUriFor = schemaUriFor;
|
package/dist/index.d.cts
CHANGED
|
@@ -46,12 +46,12 @@ type Alignment = z.infer<typeof AlignmentSchema>;
|
|
|
46
46
|
declare const LayoutFontSchema: z.ZodObject<{
|
|
47
47
|
family: z.ZodString;
|
|
48
48
|
weight: z.ZodEnum<{
|
|
49
|
-
bold: "bold";
|
|
50
49
|
normal: "normal";
|
|
50
|
+
bold: "bold";
|
|
51
51
|
}>;
|
|
52
52
|
style: z.ZodEnum<{
|
|
53
|
-
italic: "italic";
|
|
54
53
|
normal: "normal";
|
|
54
|
+
italic: "italic";
|
|
55
55
|
}>;
|
|
56
56
|
}, z.core.$strip>;
|
|
57
57
|
type LayoutFont = z.infer<typeof LayoutFontSchema>;
|
|
@@ -1264,12 +1264,12 @@ declare const LayoutTextSchema: z.ZodObject<{
|
|
|
1264
1264
|
font: z.ZodObject<{
|
|
1265
1265
|
family: z.ZodString;
|
|
1266
1266
|
weight: z.ZodEnum<{
|
|
1267
|
-
bold: "bold";
|
|
1268
1267
|
normal: "normal";
|
|
1268
|
+
bold: "bold";
|
|
1269
1269
|
}>;
|
|
1270
1270
|
style: z.ZodEnum<{
|
|
1271
|
-
italic: "italic";
|
|
1272
1271
|
normal: "normal";
|
|
1272
|
+
italic: "italic";
|
|
1273
1273
|
}>;
|
|
1274
1274
|
}, z.core.$strip>;
|
|
1275
1275
|
sizePt: z.ZodNumber;
|
|
@@ -1445,12 +1445,12 @@ declare const LayoutItemSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
1445
1445
|
font: z.ZodObject<{
|
|
1446
1446
|
family: z.ZodString;
|
|
1447
1447
|
weight: z.ZodEnum<{
|
|
1448
|
-
bold: "bold";
|
|
1449
1448
|
normal: "normal";
|
|
1449
|
+
bold: "bold";
|
|
1450
1450
|
}>;
|
|
1451
1451
|
style: z.ZodEnum<{
|
|
1452
|
-
italic: "italic";
|
|
1453
1452
|
normal: "normal";
|
|
1453
|
+
italic: "italic";
|
|
1454
1454
|
}>;
|
|
1455
1455
|
}, z.core.$strip>;
|
|
1456
1456
|
sizePt: z.ZodNumber;
|
|
@@ -1584,12 +1584,12 @@ declare const LayoutPageSchema: z.ZodObject<{
|
|
|
1584
1584
|
font: z.ZodObject<{
|
|
1585
1585
|
family: z.ZodString;
|
|
1586
1586
|
weight: z.ZodEnum<{
|
|
1587
|
-
bold: "bold";
|
|
1588
1587
|
normal: "normal";
|
|
1588
|
+
bold: "bold";
|
|
1589
1589
|
}>;
|
|
1590
1590
|
style: z.ZodEnum<{
|
|
1591
|
-
italic: "italic";
|
|
1592
1591
|
normal: "normal";
|
|
1592
|
+
italic: "italic";
|
|
1593
1593
|
}>;
|
|
1594
1594
|
}, z.core.$strip>;
|
|
1595
1595
|
sizePt: z.ZodNumber;
|
|
@@ -1747,12 +1747,12 @@ declare const LayoutDocumentSchema: z.ZodObject<{
|
|
|
1747
1747
|
font: z.ZodObject<{
|
|
1748
1748
|
family: z.ZodString;
|
|
1749
1749
|
weight: z.ZodEnum<{
|
|
1750
|
-
bold: "bold";
|
|
1751
1750
|
normal: "normal";
|
|
1751
|
+
bold: "bold";
|
|
1752
1752
|
}>;
|
|
1753
1753
|
style: z.ZodEnum<{
|
|
1754
|
-
italic: "italic";
|
|
1755
1754
|
normal: "normal";
|
|
1755
|
+
italic: "italic";
|
|
1756
1756
|
}>;
|
|
1757
1757
|
}, z.core.$strip>;
|
|
1758
1758
|
sizePt: z.ZodNumber;
|
|
@@ -2271,12 +2271,12 @@ declare const DocumentPackageSchema: z.ZodObject<{
|
|
|
2271
2271
|
font: z.ZodObject<{
|
|
2272
2272
|
family: z.ZodString;
|
|
2273
2273
|
weight: z.ZodEnum<{
|
|
2274
|
-
bold: "bold";
|
|
2275
2274
|
normal: "normal";
|
|
2275
|
+
bold: "bold";
|
|
2276
2276
|
}>;
|
|
2277
2277
|
style: z.ZodEnum<{
|
|
2278
|
-
italic: "italic";
|
|
2279
2278
|
normal: "normal";
|
|
2279
|
+
italic: "italic";
|
|
2280
2280
|
}>;
|
|
2281
2281
|
}, z.core.$strip>;
|
|
2282
2282
|
sizePt: z.ZodNumber;
|
|
@@ -2413,4 +2413,37 @@ declare const DocumentPackageSchema: z.ZodObject<{
|
|
|
2413
2413
|
}, z.core.$strip>;
|
|
2414
2414
|
type DocumentPackage = z.infer<typeof DocumentPackageSchema>;
|
|
2415
2415
|
//#endregion
|
|
2416
|
-
|
|
2416
|
+
//#region src/schema-io.d.ts
|
|
2417
|
+
type DocumentSchemaKind = 'DocumentPackage' | 'ContentDocument' | 'LayoutDocument';
|
|
2418
|
+
declare const SCHEMA_FILE_NAMES: Record<DocumentSchemaKind, string>;
|
|
2419
|
+
declare function schemaUriFor(kind: DocumentSchemaKind): string;
|
|
2420
|
+
declare function documentSchemaKindOf(value: unknown): DocumentSchemaKind | undefined;
|
|
2421
|
+
type DocumentPackageJson = DocumentPackage & {
|
|
2422
|
+
readonly $schema: string;
|
|
2423
|
+
};
|
|
2424
|
+
type ContentDocumentJson = ContentDocument & {
|
|
2425
|
+
readonly $schema: string;
|
|
2426
|
+
};
|
|
2427
|
+
type LayoutDocumentJson = LayoutDocument & {
|
|
2428
|
+
readonly $schema: string;
|
|
2429
|
+
};
|
|
2430
|
+
declare function documentPackageWithSchema(value: DocumentPackage): DocumentPackageJson;
|
|
2431
|
+
declare function contentDocumentWithSchema(value: ContentDocument): ContentDocumentJson;
|
|
2432
|
+
declare function layoutDocumentWithSchema(value: LayoutDocument): LayoutDocumentJson;
|
|
2433
|
+
declare class UnrecognizedDocumentSchemaError extends Error {
|
|
2434
|
+
readonly schema: unknown;
|
|
2435
|
+
constructor(schema: unknown);
|
|
2436
|
+
}
|
|
2437
|
+
type DocumentJsonResult = {
|
|
2438
|
+
kind: 'DocumentPackage';
|
|
2439
|
+
value: DocumentPackage;
|
|
2440
|
+
} | {
|
|
2441
|
+
kind: 'ContentDocument';
|
|
2442
|
+
value: ContentDocument;
|
|
2443
|
+
} | {
|
|
2444
|
+
kind: 'LayoutDocument';
|
|
2445
|
+
value: LayoutDocument;
|
|
2446
|
+
};
|
|
2447
|
+
declare function documentFromJson(value: unknown): DocumentJsonResult;
|
|
2448
|
+
//#endregion
|
|
2449
|
+
export { Alignment, AlignmentSchema, Box, BoxSchema, COLOR_BLACK, CONTENT_FORMAT_VERSION, Color, ColorSchema, ContentBlock, ContentBlockSchema, ContentCellValue, ContentCellValueSchema, ContentDocument, ContentDocumentJson, ContentDocumentSchema, ContentDrawPage, ContentDrawPageSchema, ContentEmbeddedObject, ContentEmbeddedObjectBlock, ContentEmbeddedObjectBlockSchema, ContentEmbeddedObjectKind, ContentEmbeddedObjectSchema, ContentImageBlock, ContentImageBlockSchema, ContentListMembership, ContentListMembershipSchema, ContentPageBreak, ContentPageBreakSchema, ContentParagraph, ContentParagraphSchema, ContentPathPoint, ContentPathPointSchema, ContentPathSegment, ContentPathSegmentSchema, ContentRun, ContentRunSchema, ContentSection, ContentSectionSchema, ContentShape, ContentShapeSchema, ContentSheet, ContentSheetCell, ContentSheetCellSchema, ContentSheetColumn, ContentSheetColumnSchema, ContentSheetImage, ContentSheetImageSchema, ContentSheetPrintRange, ContentSheetPrintRangeSchema, ContentSheetPrintSettings, ContentSheetPrintSettingsSchema, ContentSheetRepeatRange, ContentSheetRepeatRangeSchema, ContentSheetRow, ContentSheetRowSchema, ContentSheetSchema, ContentSlide, ContentSlideSchema, ContentStroke, ContentStrokeSchema, ContentSubpath, ContentSubpathSchema, ContentTable, ContentTableCell, ContentTableCellSchema, ContentTableRow, ContentTableRowSchema, ContentTableSchema, ContentVector, ContentVectorSchema, DEFAULT_LAYOUT_FONT, DOCUMENT_PACKAGE_FORMAT_VERSION, DocumentJsonResult, DocumentPackage, DocumentPackageJson, DocumentPackageSchema, DocumentSchemaKind, LAYOUT_FORMAT_VERSION, LayoutDocument, LayoutDocumentJson, LayoutDocumentSchema, LayoutEllipse, LayoutEllipseSchema, LayoutFont, LayoutFontSchema, LayoutImage, LayoutImageAsset, LayoutImageAssetSchema, LayoutImageSchema, LayoutItem, LayoutItemSchema, LayoutLine, LayoutLineSchema, LayoutLink, LayoutLinkSchema, LayoutMetadata, LayoutMetadataSchema, LayoutPage, LayoutPageSchema, LayoutPath, LayoutPathSchema, LayoutPathSegment, LayoutPathSegmentSchema, LayoutRect, LayoutRectSchema, LayoutSubpath, LayoutSubpathSchema, LayoutText, LayoutTextSchema, Margins, MarginsSchema, PAGE_SIZE_A4, PAGE_SIZE_LETTER, PageSize, PageSizeSchema, SCHEMA_FILE_NAMES, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, UnrecognizedDocumentSchemaError, colorToRgbHex, contentDocumentWithSchema, documentFromJson, documentPackageWithSchema, documentSchemaKindOf, isContentBlock, layoutDocumentWithSchema, rgbHexToColor, schemaUriFor };
|
package/dist/index.d.ts
CHANGED
|
@@ -46,12 +46,12 @@ type Alignment = z.infer<typeof AlignmentSchema>;
|
|
|
46
46
|
declare const LayoutFontSchema: z.ZodObject<{
|
|
47
47
|
family: z.ZodString;
|
|
48
48
|
weight: z.ZodEnum<{
|
|
49
|
-
bold: "bold";
|
|
50
49
|
normal: "normal";
|
|
50
|
+
bold: "bold";
|
|
51
51
|
}>;
|
|
52
52
|
style: z.ZodEnum<{
|
|
53
|
-
italic: "italic";
|
|
54
53
|
normal: "normal";
|
|
54
|
+
italic: "italic";
|
|
55
55
|
}>;
|
|
56
56
|
}, z.core.$strip>;
|
|
57
57
|
type LayoutFont = z.infer<typeof LayoutFontSchema>;
|
|
@@ -1264,12 +1264,12 @@ declare const LayoutTextSchema: z.ZodObject<{
|
|
|
1264
1264
|
font: z.ZodObject<{
|
|
1265
1265
|
family: z.ZodString;
|
|
1266
1266
|
weight: z.ZodEnum<{
|
|
1267
|
-
bold: "bold";
|
|
1268
1267
|
normal: "normal";
|
|
1268
|
+
bold: "bold";
|
|
1269
1269
|
}>;
|
|
1270
1270
|
style: z.ZodEnum<{
|
|
1271
|
-
italic: "italic";
|
|
1272
1271
|
normal: "normal";
|
|
1272
|
+
italic: "italic";
|
|
1273
1273
|
}>;
|
|
1274
1274
|
}, z.core.$strip>;
|
|
1275
1275
|
sizePt: z.ZodNumber;
|
|
@@ -1445,12 +1445,12 @@ declare const LayoutItemSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
1445
1445
|
font: z.ZodObject<{
|
|
1446
1446
|
family: z.ZodString;
|
|
1447
1447
|
weight: z.ZodEnum<{
|
|
1448
|
-
bold: "bold";
|
|
1449
1448
|
normal: "normal";
|
|
1449
|
+
bold: "bold";
|
|
1450
1450
|
}>;
|
|
1451
1451
|
style: z.ZodEnum<{
|
|
1452
|
-
italic: "italic";
|
|
1453
1452
|
normal: "normal";
|
|
1453
|
+
italic: "italic";
|
|
1454
1454
|
}>;
|
|
1455
1455
|
}, z.core.$strip>;
|
|
1456
1456
|
sizePt: z.ZodNumber;
|
|
@@ -1584,12 +1584,12 @@ declare const LayoutPageSchema: z.ZodObject<{
|
|
|
1584
1584
|
font: z.ZodObject<{
|
|
1585
1585
|
family: z.ZodString;
|
|
1586
1586
|
weight: z.ZodEnum<{
|
|
1587
|
-
bold: "bold";
|
|
1588
1587
|
normal: "normal";
|
|
1588
|
+
bold: "bold";
|
|
1589
1589
|
}>;
|
|
1590
1590
|
style: z.ZodEnum<{
|
|
1591
|
-
italic: "italic";
|
|
1592
1591
|
normal: "normal";
|
|
1592
|
+
italic: "italic";
|
|
1593
1593
|
}>;
|
|
1594
1594
|
}, z.core.$strip>;
|
|
1595
1595
|
sizePt: z.ZodNumber;
|
|
@@ -1747,12 +1747,12 @@ declare const LayoutDocumentSchema: z.ZodObject<{
|
|
|
1747
1747
|
font: z.ZodObject<{
|
|
1748
1748
|
family: z.ZodString;
|
|
1749
1749
|
weight: z.ZodEnum<{
|
|
1750
|
-
bold: "bold";
|
|
1751
1750
|
normal: "normal";
|
|
1751
|
+
bold: "bold";
|
|
1752
1752
|
}>;
|
|
1753
1753
|
style: z.ZodEnum<{
|
|
1754
|
-
italic: "italic";
|
|
1755
1754
|
normal: "normal";
|
|
1755
|
+
italic: "italic";
|
|
1756
1756
|
}>;
|
|
1757
1757
|
}, z.core.$strip>;
|
|
1758
1758
|
sizePt: z.ZodNumber;
|
|
@@ -2271,12 +2271,12 @@ declare const DocumentPackageSchema: z.ZodObject<{
|
|
|
2271
2271
|
font: z.ZodObject<{
|
|
2272
2272
|
family: z.ZodString;
|
|
2273
2273
|
weight: z.ZodEnum<{
|
|
2274
|
-
bold: "bold";
|
|
2275
2274
|
normal: "normal";
|
|
2275
|
+
bold: "bold";
|
|
2276
2276
|
}>;
|
|
2277
2277
|
style: z.ZodEnum<{
|
|
2278
|
-
italic: "italic";
|
|
2279
2278
|
normal: "normal";
|
|
2279
|
+
italic: "italic";
|
|
2280
2280
|
}>;
|
|
2281
2281
|
}, z.core.$strip>;
|
|
2282
2282
|
sizePt: z.ZodNumber;
|
|
@@ -2413,4 +2413,37 @@ declare const DocumentPackageSchema: z.ZodObject<{
|
|
|
2413
2413
|
}, z.core.$strip>;
|
|
2414
2414
|
type DocumentPackage = z.infer<typeof DocumentPackageSchema>;
|
|
2415
2415
|
//#endregion
|
|
2416
|
-
|
|
2416
|
+
//#region src/schema-io.d.ts
|
|
2417
|
+
type DocumentSchemaKind = 'DocumentPackage' | 'ContentDocument' | 'LayoutDocument';
|
|
2418
|
+
declare const SCHEMA_FILE_NAMES: Record<DocumentSchemaKind, string>;
|
|
2419
|
+
declare function schemaUriFor(kind: DocumentSchemaKind): string;
|
|
2420
|
+
declare function documentSchemaKindOf(value: unknown): DocumentSchemaKind | undefined;
|
|
2421
|
+
type DocumentPackageJson = DocumentPackage & {
|
|
2422
|
+
readonly $schema: string;
|
|
2423
|
+
};
|
|
2424
|
+
type ContentDocumentJson = ContentDocument & {
|
|
2425
|
+
readonly $schema: string;
|
|
2426
|
+
};
|
|
2427
|
+
type LayoutDocumentJson = LayoutDocument & {
|
|
2428
|
+
readonly $schema: string;
|
|
2429
|
+
};
|
|
2430
|
+
declare function documentPackageWithSchema(value: DocumentPackage): DocumentPackageJson;
|
|
2431
|
+
declare function contentDocumentWithSchema(value: ContentDocument): ContentDocumentJson;
|
|
2432
|
+
declare function layoutDocumentWithSchema(value: LayoutDocument): LayoutDocumentJson;
|
|
2433
|
+
declare class UnrecognizedDocumentSchemaError extends Error {
|
|
2434
|
+
readonly schema: unknown;
|
|
2435
|
+
constructor(schema: unknown);
|
|
2436
|
+
}
|
|
2437
|
+
type DocumentJsonResult = {
|
|
2438
|
+
kind: 'DocumentPackage';
|
|
2439
|
+
value: DocumentPackage;
|
|
2440
|
+
} | {
|
|
2441
|
+
kind: 'ContentDocument';
|
|
2442
|
+
value: ContentDocument;
|
|
2443
|
+
} | {
|
|
2444
|
+
kind: 'LayoutDocument';
|
|
2445
|
+
value: LayoutDocument;
|
|
2446
|
+
};
|
|
2447
|
+
declare function documentFromJson(value: unknown): DocumentJsonResult;
|
|
2448
|
+
//#endregion
|
|
2449
|
+
export { Alignment, AlignmentSchema, Box, BoxSchema, COLOR_BLACK, CONTENT_FORMAT_VERSION, Color, ColorSchema, ContentBlock, ContentBlockSchema, ContentCellValue, ContentCellValueSchema, ContentDocument, ContentDocumentJson, ContentDocumentSchema, ContentDrawPage, ContentDrawPageSchema, ContentEmbeddedObject, ContentEmbeddedObjectBlock, ContentEmbeddedObjectBlockSchema, ContentEmbeddedObjectKind, ContentEmbeddedObjectSchema, ContentImageBlock, ContentImageBlockSchema, ContentListMembership, ContentListMembershipSchema, ContentPageBreak, ContentPageBreakSchema, ContentParagraph, ContentParagraphSchema, ContentPathPoint, ContentPathPointSchema, ContentPathSegment, ContentPathSegmentSchema, ContentRun, ContentRunSchema, ContentSection, ContentSectionSchema, ContentShape, ContentShapeSchema, ContentSheet, ContentSheetCell, ContentSheetCellSchema, ContentSheetColumn, ContentSheetColumnSchema, ContentSheetImage, ContentSheetImageSchema, ContentSheetPrintRange, ContentSheetPrintRangeSchema, ContentSheetPrintSettings, ContentSheetPrintSettingsSchema, ContentSheetRepeatRange, ContentSheetRepeatRangeSchema, ContentSheetRow, ContentSheetRowSchema, ContentSheetSchema, ContentSlide, ContentSlideSchema, ContentStroke, ContentStrokeSchema, ContentSubpath, ContentSubpathSchema, ContentTable, ContentTableCell, ContentTableCellSchema, ContentTableRow, ContentTableRowSchema, ContentTableSchema, ContentVector, ContentVectorSchema, DEFAULT_LAYOUT_FONT, DOCUMENT_PACKAGE_FORMAT_VERSION, DocumentJsonResult, DocumentPackage, DocumentPackageJson, DocumentPackageSchema, DocumentSchemaKind, LAYOUT_FORMAT_VERSION, LayoutDocument, LayoutDocumentJson, LayoutDocumentSchema, LayoutEllipse, LayoutEllipseSchema, LayoutFont, LayoutFontSchema, LayoutImage, LayoutImageAsset, LayoutImageAssetSchema, LayoutImageSchema, LayoutItem, LayoutItemSchema, LayoutLine, LayoutLineSchema, LayoutLink, LayoutLinkSchema, LayoutMetadata, LayoutMetadataSchema, LayoutPage, LayoutPageSchema, LayoutPath, LayoutPathSchema, LayoutPathSegment, LayoutPathSegmentSchema, LayoutRect, LayoutRectSchema, LayoutSubpath, LayoutSubpathSchema, LayoutText, LayoutTextSchema, Margins, MarginsSchema, PAGE_SIZE_A4, PAGE_SIZE_LETTER, PageSize, PageSizeSchema, SCHEMA_FILE_NAMES, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, UnrecognizedDocumentSchemaError, colorToRgbHex, contentDocumentWithSchema, documentFromJson, documentPackageWithSchema, documentSchemaKindOf, isContentBlock, layoutDocumentWithSchema, rgbHexToColor, schemaUriFor };
|
package/dist/index.js
CHANGED
|
@@ -140,29 +140,29 @@ const ContentPageBreakSchema = z.object({
|
|
|
140
140
|
kind: z.literal("pageBreak"),
|
|
141
141
|
sourcePath: z.string().optional()
|
|
142
142
|
});
|
|
143
|
-
function isRecord(value) {
|
|
143
|
+
function isRecord$1(value) {
|
|
144
144
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
145
145
|
}
|
|
146
146
|
function isContentRun(value) {
|
|
147
|
-
return isRecord(value) && typeof value.text === "string";
|
|
147
|
+
return isRecord$1(value) && typeof value.text === "string";
|
|
148
148
|
}
|
|
149
149
|
function isContentTableCell(value) {
|
|
150
|
-
return isRecord(value) && Array.isArray(value.blocks) && value.blocks.every(isContentBlock);
|
|
150
|
+
return isRecord$1(value) && Array.isArray(value.blocks) && value.blocks.every(isContentBlock);
|
|
151
151
|
}
|
|
152
152
|
function isContentTableRow(value) {
|
|
153
|
-
return isRecord(value) && Array.isArray(value.cells) && value.cells.every(isContentTableCell) && (value.heightPt === void 0 || typeof value.heightPt === "number");
|
|
153
|
+
return isRecord$1(value) && Array.isArray(value.cells) && value.cells.every(isContentTableCell) && (value.heightPt === void 0 || typeof value.heightPt === "number");
|
|
154
154
|
}
|
|
155
155
|
function isContentEmbeddedObjectKind(value) {
|
|
156
156
|
return value === "formula" || value === "wordprocessing" || value === "presentation" || value === "spreadsheet" || value === "drawing";
|
|
157
157
|
}
|
|
158
158
|
function isContentEmbeddedObject(value) {
|
|
159
|
-
return isRecord(value) && isContentEmbeddedObjectKind(value.objectKind) && BoxSchema.safeParse(value.frame).success && ContentDocumentSchema.safeParse(value.document).success;
|
|
159
|
+
return isRecord$1(value) && isContentEmbeddedObjectKind(value.objectKind) && BoxSchema.safeParse(value.frame).success && ContentDocumentSchema.safeParse(value.document).success;
|
|
160
160
|
}
|
|
161
161
|
function isContentEmbeddedObjectBlock(value) {
|
|
162
|
-
return isRecord(value) && value.kind === "embeddedObject" && isContentEmbeddedObject(value);
|
|
162
|
+
return isRecord$1(value) && value.kind === "embeddedObject" && isContentEmbeddedObject(value);
|
|
163
163
|
}
|
|
164
164
|
function isContentBlock(value) {
|
|
165
|
-
if (!isRecord(value)) return false;
|
|
165
|
+
if (!isRecord$1(value)) return false;
|
|
166
166
|
const kind = value.kind;
|
|
167
167
|
if (kind === "paragraph") return Array.isArray(value.runs) && value.runs.every(isContentRun);
|
|
168
168
|
if (kind === "image") return (value.format === "png" || value.format === "jpeg") && typeof value.base64 === "string" && typeof value.widthPt === "number" && typeof value.heightPt === "number";
|
|
@@ -536,4 +536,79 @@ const DocumentPackageSchema = z.object({
|
|
|
536
536
|
layout: LayoutDocumentSchema.optional()
|
|
537
537
|
});
|
|
538
538
|
//#endregion
|
|
539
|
-
|
|
539
|
+
//#region src/schema-io.ts
|
|
540
|
+
const SCHEMA_FILE_NAMES = {
|
|
541
|
+
DocumentPackage: "document-package.schema.json",
|
|
542
|
+
ContentDocument: "content-document.schema.json",
|
|
543
|
+
LayoutDocument: "layout-document.schema.json"
|
|
544
|
+
};
|
|
545
|
+
function schemaUriFor(kind) {
|
|
546
|
+
return `https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/${SCHEMA_FILE_NAMES[kind]}`;
|
|
547
|
+
}
|
|
548
|
+
const SCHEMA_URI_PATTERN = /^https:\/\/cdn\.jsdelivr\.net\/npm\/document-schema\.js@[^/]+\/schemas\/(document-package|content-document|layout-document)\.schema\.json$/;
|
|
549
|
+
function kindForFileStem(stem) {
|
|
550
|
+
switch (stem) {
|
|
551
|
+
case "document-package": return "DocumentPackage";
|
|
552
|
+
case "content-document": return "ContentDocument";
|
|
553
|
+
case "layout-document": return "LayoutDocument";
|
|
554
|
+
default: return;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
function isRecord(value) {
|
|
558
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
559
|
+
}
|
|
560
|
+
function documentSchemaKindOf(value) {
|
|
561
|
+
if (!isRecord(value)) return void 0;
|
|
562
|
+
if (!("$schema" in value) || typeof value.$schema !== "string") return void 0;
|
|
563
|
+
const match = SCHEMA_URI_PATTERN.exec(value.$schema);
|
|
564
|
+
if (match === null) return void 0;
|
|
565
|
+
const stem = match[1];
|
|
566
|
+
if (stem === void 0) return void 0;
|
|
567
|
+
return kindForFileStem(stem);
|
|
568
|
+
}
|
|
569
|
+
function documentPackageWithSchema(value) {
|
|
570
|
+
return {
|
|
571
|
+
$schema: schemaUriFor("DocumentPackage"),
|
|
572
|
+
...value
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
function contentDocumentWithSchema(value) {
|
|
576
|
+
return {
|
|
577
|
+
$schema: schemaUriFor("ContentDocument"),
|
|
578
|
+
...value
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function layoutDocumentWithSchema(value) {
|
|
582
|
+
return {
|
|
583
|
+
$schema: schemaUriFor("LayoutDocument"),
|
|
584
|
+
...value
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
var UnrecognizedDocumentSchemaError = class extends Error {
|
|
588
|
+
schema;
|
|
589
|
+
constructor(schema) {
|
|
590
|
+
super(`documentFromJson: value has no recognized "$schema" property (expected one of the three document-schema.js .schema.json URIs; found: ${JSON.stringify(schema)}).`);
|
|
591
|
+
this.name = "UnrecognizedDocumentSchemaError";
|
|
592
|
+
this.schema = schema;
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
function documentFromJson(value) {
|
|
596
|
+
const kind = documentSchemaKindOf(value);
|
|
597
|
+
if (kind === void 0) throw new UnrecognizedDocumentSchemaError(isRecord(value) ? value.$schema : void 0);
|
|
598
|
+
switch (kind) {
|
|
599
|
+
case "DocumentPackage": return {
|
|
600
|
+
kind,
|
|
601
|
+
value: DocumentPackageSchema.parse(value)
|
|
602
|
+
};
|
|
603
|
+
case "ContentDocument": return {
|
|
604
|
+
kind,
|
|
605
|
+
value: ContentDocumentSchema.parse(value)
|
|
606
|
+
};
|
|
607
|
+
case "LayoutDocument": return {
|
|
608
|
+
kind,
|
|
609
|
+
value: LayoutDocumentSchema.parse(value)
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
//#endregion
|
|
614
|
+
export { AlignmentSchema, BoxSchema, COLOR_BLACK, CONTENT_FORMAT_VERSION, ColorSchema, ContentBlockSchema, ContentCellValueSchema, ContentDocumentSchema, ContentDrawPageSchema, ContentEmbeddedObjectBlockSchema, ContentEmbeddedObjectSchema, ContentImageBlockSchema, ContentListMembershipSchema, ContentPageBreakSchema, ContentParagraphSchema, ContentPathPointSchema, ContentPathSegmentSchema, ContentRunSchema, ContentSectionSchema, ContentShapeSchema, ContentSheetCellSchema, ContentSheetColumnSchema, ContentSheetImageSchema, ContentSheetPrintRangeSchema, ContentSheetPrintSettingsSchema, ContentSheetRepeatRangeSchema, ContentSheetRowSchema, ContentSheetSchema, ContentSlideSchema, ContentStrokeSchema, ContentSubpathSchema, ContentTableCellSchema, ContentTableRowSchema, ContentTableSchema, ContentVectorSchema, DEFAULT_LAYOUT_FONT, DOCUMENT_PACKAGE_FORMAT_VERSION, DocumentPackageSchema, LAYOUT_FORMAT_VERSION, LayoutDocumentSchema, LayoutEllipseSchema, LayoutFontSchema, LayoutImageAssetSchema, LayoutImageSchema, LayoutItemSchema, LayoutLineSchema, LayoutLinkSchema, LayoutMetadataSchema, LayoutPageSchema, LayoutPathSchema, LayoutPathSegmentSchema, LayoutRectSchema, LayoutSubpathSchema, LayoutTextSchema, MarginsSchema, PAGE_SIZE_A4, PAGE_SIZE_LETTER, PageSizeSchema, SCHEMA_FILE_NAMES, SLIDE_SIZE_STANDARD, SLIDE_SIZE_WIDESCREEN, UnrecognizedDocumentSchemaError, colorToRgbHex, contentDocumentWithSchema, documentFromJson, documentPackageWithSchema, documentSchemaKindOf, isContentBlock, layoutDocumentWithSchema, rgbHexToColor, schemaUriFor };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "document-schema.js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
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
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
-
"$id": "https://
|
|
3
|
+
"$id": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/content-document.schema.json",
|
|
4
4
|
"oneOf": [
|
|
5
5
|
{
|
|
6
6
|
"type": "object",
|
|
@@ -930,7 +930,7 @@
|
|
|
930
930
|
]
|
|
931
931
|
},
|
|
932
932
|
"document": {
|
|
933
|
-
"$ref": "https://
|
|
933
|
+
"$ref": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/content-document.schema.json"
|
|
934
934
|
},
|
|
935
935
|
"frame": {
|
|
936
936
|
"$ref": "#/$defs/Box"
|
|
@@ -2021,7 +2021,7 @@
|
|
|
2021
2021
|
]
|
|
2022
2022
|
},
|
|
2023
2023
|
"document": {
|
|
2024
|
-
"$ref": "https://
|
|
2024
|
+
"$ref": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/content-document.schema.json"
|
|
2025
2025
|
},
|
|
2026
2026
|
"frame": {
|
|
2027
2027
|
"$ref": "#/$defs/Box"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
-
"$id": "https://
|
|
3
|
+
"$id": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/document-package.schema.json",
|
|
4
4
|
"type": "object",
|
|
5
5
|
"properties": {
|
|
6
6
|
"formatVersion": {
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
"const": 1
|
|
9
9
|
},
|
|
10
10
|
"content": {
|
|
11
|
-
"$ref": "https://
|
|
11
|
+
"$ref": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/content-document.schema.json"
|
|
12
12
|
},
|
|
13
13
|
"layout": {
|
|
14
|
-
"$ref": "https://
|
|
14
|
+
"$ref": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/layout-document.schema.json"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"required": [
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
-
"$id": "https://
|
|
3
|
+
"$id": "https://cdn.jsdelivr.net/npm/document-schema.js@1.7.0/schemas/layout-document.schema.json",
|
|
4
4
|
"type": "object",
|
|
5
5
|
"properties": {
|
|
6
6
|
"formatVersion": {
|