epub-codec 0.0.0 → 1.0.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.
Files changed (133) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +184 -0
  3. package/dist/codec.cjs +28 -0
  4. package/dist/codec.d.cts +2308 -0
  5. package/dist/codec.d.ts +2308 -0
  6. package/dist/codec.js +25 -0
  7. package/dist/diagnostics-Ee4kG7mw.d.cts +60 -0
  8. package/dist/diagnostics-Ee4kG7mw.d.ts +60 -0
  9. package/dist/diagnostics.cjs +96 -0
  10. package/dist/diagnostics.d.cts +2 -0
  11. package/dist/diagnostics.d.ts +2 -0
  12. package/dist/diagnostics.js +85 -0
  13. package/dist/format.cjs +9 -0
  14. package/dist/format.d.cts +6 -0
  15. package/dist/format.d.ts +6 -0
  16. package/dist/format.js +6 -0
  17. package/dist/image/dimensions.cjs +82 -0
  18. package/dist/image/dimensions.d.cts +11 -0
  19. package/dist/image/dimensions.d.ts +11 -0
  20. package/dist/image/dimensions.js +79 -0
  21. package/dist/index.cjs +105 -0
  22. package/dist/index.d.cts +32 -0
  23. package/dist/index.d.ts +32 -0
  24. package/dist/index.js +32 -0
  25. package/dist/nav/nav3.cjs +19 -0
  26. package/dist/nav/nav3.d.cts +4 -0
  27. package/dist/nav/nav3.d.ts +4 -0
  28. package/dist/nav/nav3.js +18 -0
  29. package/dist/nav/ncx.cjs +17 -0
  30. package/dist/nav/ncx.d.cts +4 -0
  31. package/dist/nav/ncx.d.ts +4 -0
  32. package/dist/nav/ncx.js +16 -0
  33. package/dist/nav/reconcile.cjs +8 -0
  34. package/dist/nav/reconcile.d.cts +4 -0
  35. package/dist/nav/reconcile.d.ts +4 -0
  36. package/dist/nav/reconcile.js +7 -0
  37. package/dist/nav/write.cjs +39 -0
  38. package/dist/nav/write.d.cts +10 -0
  39. package/dist/nav/write.d.ts +10 -0
  40. package/dist/nav/write.js +37 -0
  41. package/dist/ocf/container.cjs +20 -0
  42. package/dist/ocf/container.d.cts +4 -0
  43. package/dist/ocf/container.d.ts +4 -0
  44. package/dist/ocf/container.js +19 -0
  45. package/dist/ocf/write.cjs +13 -0
  46. package/dist/ocf/write.d.cts +4 -0
  47. package/dist/ocf/write.d.ts +4 -0
  48. package/dist/ocf/write.js +12 -0
  49. package/dist/opf/metadata.cjs +51 -0
  50. package/dist/opf/metadata.d.cts +7 -0
  51. package/dist/opf/metadata.d.ts +7 -0
  52. package/dist/opf/metadata.js +50 -0
  53. package/dist/opf/parse.cjs +47 -0
  54. package/dist/opf/parse.d.cts +6 -0
  55. package/dist/opf/parse.d.ts +6 -0
  56. package/dist/opf/parse.js +46 -0
  57. package/dist/opf/types.cjs +0 -0
  58. package/dist/opf/types.d.cts +20 -0
  59. package/dist/opf/types.d.ts +20 -0
  60. package/dist/opf/types.js +0 -0
  61. package/dist/opf/write.cjs +55 -0
  62. package/dist/opf/write.d.cts +12 -0
  63. package/dist/opf/write.d.ts +12 -0
  64. package/dist/opf/write.js +54 -0
  65. package/dist/path.cjs +22 -0
  66. package/dist/path.d.cts +5 -0
  67. package/dist/path.d.ts +5 -0
  68. package/dist/path.js +20 -0
  69. package/dist/read.cjs +166 -0
  70. package/dist/read.d.cts +10 -0
  71. package/dist/read.d.ts +10 -0
  72. package/dist/read.js +164 -0
  73. package/dist/util/base64.cjs +51 -0
  74. package/dist/util/base64.d.cts +5 -0
  75. package/dist/util/base64.d.ts +5 -0
  76. package/dist/util/base64.js +49 -0
  77. package/dist/write.cjs +136 -0
  78. package/dist/write.d.cts +10 -0
  79. package/dist/write.d.ts +10 -0
  80. package/dist/write.js +134 -0
  81. package/dist/xhtml/context.cjs +0 -0
  82. package/dist/xhtml/context.d.cts +20 -0
  83. package/dist/xhtml/context.d.ts +20 -0
  84. package/dist/xhtml/context.js +0 -0
  85. package/dist/xhtml/footnote.cjs +32 -0
  86. package/dist/xhtml/footnote.d.cts +7 -0
  87. package/dist/xhtml/footnote.d.ts +7 -0
  88. package/dist/xhtml/footnote.js +29 -0
  89. package/dist/xhtml/inline.cjs +128 -0
  90. package/dist/xhtml/inline.d.cts +11 -0
  91. package/dist/xhtml/inline.d.ts +11 -0
  92. package/dist/xhtml/inline.js +127 -0
  93. package/dist/xhtml/list-id.cjs +22 -0
  94. package/dist/xhtml/list-id.d.cts +13 -0
  95. package/dist/xhtml/list-id.d.ts +13 -0
  96. package/dist/xhtml/list-id.js +20 -0
  97. package/dist/xhtml/read.cjs +423 -0
  98. package/dist/xhtml/read.d.cts +20 -0
  99. package/dist/xhtml/read.d.ts +20 -0
  100. package/dist/xhtml/read.js +422 -0
  101. package/dist/xhtml/style-constants.cjs +13 -0
  102. package/dist/xhtml/style-constants.d.cts +8 -0
  103. package/dist/xhtml/style-constants.d.ts +8 -0
  104. package/dist/xhtml/style-constants.js +8 -0
  105. package/dist/xhtml/write.cjs +183 -0
  106. package/dist/xhtml/write.d.cts +12 -0
  107. package/dist/xhtml/write.d.ts +12 -0
  108. package/dist/xhtml/write.js +182 -0
  109. package/dist/xml/build.cjs +46 -0
  110. package/dist/xml/build.d.cts +5 -0
  111. package/dist/xml/build.d.ts +5 -0
  112. package/dist/xml/build.js +46 -0
  113. package/dist/xml/entities.cjs +20 -0
  114. package/dist/xml/entities.d.cts +5 -0
  115. package/dist/xml/entities.d.ts +5 -0
  116. package/dist/xml/entities.js +18 -0
  117. package/dist/xml/node.cjs +67 -0
  118. package/dist/xml/node.d.cts +83 -0
  119. package/dist/xml/node.d.ts +83 -0
  120. package/dist/xml/node.js +58 -0
  121. package/dist/xml/parse.cjs +88 -0
  122. package/dist/xml/parse.d.cts +5 -0
  123. package/dist/xml/parse.d.ts +5 -0
  124. package/dist/xml/parse.js +88 -0
  125. package/dist/xml/query.cjs +44 -0
  126. package/dist/xml/query.d.cts +11 -0
  127. package/dist/xml/query.d.ts +11 -0
  128. package/dist/xml/query.js +37 -0
  129. package/dist/zip.cjs +19 -0
  130. package/dist/zip.d.cts +9 -0
  131. package/dist/zip.d.ts +9 -0
  132. package/dist/zip.js +17 -0
  133. package/package.json +95 -2
@@ -0,0 +1,182 @@
1
+ import { EpubDiagnosticCodes } from "../diagnostics.js";
2
+ import { encodeEntities } from "../xml/entities.js";
3
+ import { base64ToBytes } from "../util/base64.js";
4
+ import "./style-constants.js";
5
+ import { parseListNumId } from "./list-id.js";
6
+ import { isHeadingGroupNode, isListGroupNode, isSectionConstructGroupNode, isTreeBlockLeaf } from "document-schema.js";
7
+ import { decomposeSection } from "document-schema.js/decompose";
8
+ //#region src/xhtml/write.ts
9
+ function element(tag, attrs = {}, children = []) {
10
+ return {
11
+ type: "element",
12
+ tag,
13
+ attributes: Object.entries(attrs).map(([name, value]) => ({
14
+ name,
15
+ value
16
+ })),
17
+ children
18
+ };
19
+ }
20
+ function text(value) {
21
+ return {
22
+ type: "text",
23
+ value: encodeEntities(value)
24
+ };
25
+ }
26
+ function writeXhtmlBody(blocks, context) {
27
+ const { children } = decomposeSection({
28
+ pageSize: {
29
+ widthPt: 1,
30
+ heightPt: 1
31
+ },
32
+ margins: {
33
+ topPt: 0,
34
+ rightPt: 0,
35
+ bottomPt: 0,
36
+ leftPt: 0
37
+ },
38
+ blocks: [...blocks]
39
+ });
40
+ return element("body", {}, writeSectionChildren(children, context));
41
+ }
42
+ function writeSectionChildren(children, context) {
43
+ const out = [];
44
+ let index = 0;
45
+ while (index < children.length) {
46
+ const child = children[index];
47
+ if (child === void 0) break;
48
+ if (isListGroupNode(child)) {
49
+ let end = index;
50
+ while (end < children.length && isListGroupNode(children[end])) end += 1;
51
+ const items = children.slice(index, end);
52
+ out.push(writeList(items, context));
53
+ index = end;
54
+ continue;
55
+ }
56
+ out.push(...writeSectionChild(child, context));
57
+ index += 1;
58
+ }
59
+ return out;
60
+ }
61
+ function writeSectionChild(child, context) {
62
+ if (isHeadingGroupNode(child)) return [writeHeading(child), ...writeSectionChildren(child.children, context)];
63
+ if (isListGroupNode(child)) return [writeList([child], context)];
64
+ if (isSectionConstructGroupNode(child)) return writeSectionConstructGroup(child, context);
65
+ return writeLeafBlock(child, context);
66
+ }
67
+ function writeHeading(group) {
68
+ const level = Math.min(6, Math.max(1, group.node.headingLevel));
69
+ return element(`h${String(level)}`, {}, writeRunsToNodes(group.node.runs, group.node.constructs));
70
+ }
71
+ function writeList(items, context) {
72
+ const numId = items[0]?.node.list.numId;
73
+ const info = numId === void 0 ? void 0 : parseListNumId(numId);
74
+ return element(info?.type === "ordered" ? "ol" : "ul", info?.type === "ordered" && info.start !== void 0 ? { start: String(info.start) } : {}, items.map((item) => {
75
+ const paragraph = item.node;
76
+ const anchorNodes = writeRunsToNodes(paragraph.runs, paragraph.constructs);
77
+ const nested = writeSectionChildren(item.children, context);
78
+ return element("li", {}, [...anchorNodes, ...nested]);
79
+ }));
80
+ }
81
+ function writeSectionConstructGroup(group, context) {
82
+ const descriptor = group.node;
83
+ if (descriptor.kind === "division") return [element("blockquote", {}, writeSectionChildren(group.children, context))];
84
+ if (descriptor.kind === "anchor" && descriptor.anchorType === "footnote") return [element("aside", {
85
+ "epub:type": "footnote",
86
+ id: descriptor.name
87
+ }, writeSectionChildren(group.children, context))];
88
+ context.sink({
89
+ code: EpubDiagnosticCodes.CONSTRUCT_UNREPRESENTED,
90
+ severity: "info",
91
+ message: `a '${descriptor.kind}' construct has no XHTML spelling in this package's writer; its extent is written, the construct itself is not`,
92
+ href: context.sourceHref
93
+ });
94
+ return writeSectionChildren(group.children, context);
95
+ }
96
+ function writeLeafBlock(block, context) {
97
+ switch (block.kind) {
98
+ case "paragraph": return [writeParagraph(block)];
99
+ case "table": return [writeTable(block, context)];
100
+ case "image": return [writeImage(block, context)];
101
+ case "pageBreak": return [];
102
+ case "embeddedObject":
103
+ context.sink({
104
+ code: EpubDiagnosticCodes.ELEMENT_UNMAPPED,
105
+ severity: "warning",
106
+ message: "an embedded object has no XHTML representation in this package's writer and was dropped",
107
+ href: context.sourceHref
108
+ });
109
+ return [];
110
+ }
111
+ }
112
+ function isPreBlockParagraph(paragraph) {
113
+ if (paragraph.codeLanguage !== void 0) return true;
114
+ return paragraph.runs.length === 1 && paragraph.runs[0]?.fontFamily === "Courier New" && paragraph.runs[0].text.includes("\n");
115
+ }
116
+ function writeParagraph(paragraph) {
117
+ if (paragraph.styleId === "HorizontalRule" && paragraph.runs.length === 0) return element("hr");
118
+ if (isPreBlockParagraph(paragraph)) return element("pre", {}, [element("code", paragraph.codeLanguage === void 0 ? {} : { class: `language-${paragraph.codeLanguage}` }, [text(paragraph.runs.map((run) => run.text).join(""))])]);
119
+ return element("p", {}, writeRunsToNodes(paragraph.runs, paragraph.constructs));
120
+ }
121
+ function isFootnoteExtent(construct) {
122
+ return construct.descriptor.kind === "anchor" && construct.descriptor.anchorType === "footnote";
123
+ }
124
+ function writeRunsToNodes(runs, constructs) {
125
+ const footnoteExtents = (constructs ?? []).filter(isFootnoteExtent);
126
+ const out = [];
127
+ let index = 0;
128
+ while (index < runs.length) {
129
+ const extent = footnoteExtents.find((e) => e.startRun === index);
130
+ if (extent !== void 0) {
131
+ const inner = runs.slice(extent.startRun, extent.endRun).flatMap((run) => writeRunNodes(run));
132
+ out.push(element("a", {
133
+ "epub:type": "noteref",
134
+ href: `#${extent.descriptor.name}`
135
+ }, inner));
136
+ index = Math.max(extent.endRun, index + 1);
137
+ continue;
138
+ }
139
+ const run = runs[index];
140
+ if (run !== void 0) out.push(...writeRunNodes(run));
141
+ index += 1;
142
+ }
143
+ return out;
144
+ }
145
+ function writeRunNodes(run) {
146
+ const segments = run.text.split("\n");
147
+ let nodes = [];
148
+ segments.forEach((segment, i) => {
149
+ if (i > 0) nodes.push(element("br"));
150
+ if (segment.length > 0) nodes.push(text(segment));
151
+ });
152
+ if (nodes.length === 0) nodes = [text("")];
153
+ if (run.fontFamily === "Courier New") nodes = [element("code", {}, nodes)];
154
+ if (run.bold === true) nodes = [element("strong", {}, nodes)];
155
+ if (run.italic === true) nodes = [element("em", {}, nodes)];
156
+ if (run.underline === true) nodes = [element("u", {}, nodes)];
157
+ if (run.strike === true) nodes = [element("s", {}, nodes)];
158
+ if (run.hyperlink !== void 0) nodes = [element("a", { href: run.hyperlink }, nodes)];
159
+ return nodes;
160
+ }
161
+ function writeTable(table, context) {
162
+ return element("table", {}, table.rows.map((row) => element("tr", {}, row.cells.map((cell) => {
163
+ const attrs = {};
164
+ if (cell.colSpan !== void 0) attrs.colspan = String(cell.colSpan);
165
+ if (cell.rowSpan !== void 0) attrs.rowspan = String(cell.rowSpan);
166
+ for (const block of cell.blocks) if (!isTreeBlockLeaf(block)) context.sink({
167
+ code: EpubDiagnosticCodes.ELEMENT_UNMAPPED,
168
+ severity: "info",
169
+ message: "a construct-boundary marker inside a table cell has no XHTML representation in this package's writer and was dropped",
170
+ href: context.sourceHref
171
+ });
172
+ return element("td", attrs, cell.blocks.filter(isTreeBlockLeaf).flatMap((block) => writeLeafBlock(block, context)));
173
+ }))));
174
+ }
175
+ function writeImage(image, context) {
176
+ const bytes = base64ToBytes(image.base64);
177
+ const attrs = { src: context.registerImage(bytes, image.format) };
178
+ attrs.alt = image.altText ?? "";
179
+ return element("img", attrs);
180
+ }
181
+ //#endregion
182
+ export { writeXhtmlBody };
@@ -0,0 +1,46 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/xml/build.ts
3
+ const BUILDER = new (require("fast-xml-parser")).XMLBuilder({
4
+ preserveOrder: true,
5
+ attributeNamePrefix: "@_",
6
+ ignoreAttributes: false,
7
+ textNodeName: "#text",
8
+ cdataPropName: "__cdata",
9
+ commentPropName: "__comment",
10
+ processEntities: false,
11
+ format: false,
12
+ suppressEmptyNode: false
13
+ });
14
+ function buildXml(nodes) {
15
+ const out = BUILDER.build(toOrdered(nodes));
16
+ if (typeof out !== "string") throw new Error("XMLBuilder did not return a string");
17
+ return out;
18
+ }
19
+ function toOrdered(nodes) {
20
+ return nodes.map(toOrderedNode);
21
+ }
22
+ function attrsObject(attributes) {
23
+ const obj = {};
24
+ for (const a of attributes) obj[`@_${a.name}`] = a.value;
25
+ return obj;
26
+ }
27
+ function toOrderedNode(node) {
28
+ switch (node.type) {
29
+ case "text": return { "#text": node.value };
30
+ case "comment": return { __comment: [{ "#text": node.value }] };
31
+ case "cdata": return { __cdata: [{ "#text": node.value }] };
32
+ case "pi": return { [`?${node.target}`]: [{ "#text": node.content }] };
33
+ case "declaration": return {
34
+ "?xml": [{ "#text": "" }],
35
+ ":@": attrsObject(node.attributes)
36
+ };
37
+ case "element": {
38
+ const obj = { [node.tag]: toOrdered(node.children) };
39
+ const attrs = attrsObject(node.attributes);
40
+ if (Object.keys(attrs).length > 0) obj[":@"] = attrs;
41
+ return obj;
42
+ }
43
+ }
44
+ }
45
+ //#endregion
46
+ exports.buildXml = buildXml;
@@ -0,0 +1,5 @@
1
+ import { XmlNode } from "./node.cjs";
2
+ //#region src/xml/build.d.ts
3
+ declare function buildXml(nodes: XmlNode[]): string;
4
+ //#endregion
5
+ export { buildXml };
@@ -0,0 +1,5 @@
1
+ import { XmlNode } from "./node.js";
2
+ //#region src/xml/build.d.ts
3
+ declare function buildXml(nodes: XmlNode[]): string;
4
+ //#endregion
5
+ export { buildXml };
@@ -0,0 +1,46 @@
1
+ import { XMLBuilder } from "fast-xml-parser";
2
+ //#region src/xml/build.ts
3
+ const BUILDER = new XMLBuilder({
4
+ preserveOrder: true,
5
+ attributeNamePrefix: "@_",
6
+ ignoreAttributes: false,
7
+ textNodeName: "#text",
8
+ cdataPropName: "__cdata",
9
+ commentPropName: "__comment",
10
+ processEntities: false,
11
+ format: false,
12
+ suppressEmptyNode: false
13
+ });
14
+ function buildXml(nodes) {
15
+ const out = BUILDER.build(toOrdered(nodes));
16
+ if (typeof out !== "string") throw new Error("XMLBuilder did not return a string");
17
+ return out;
18
+ }
19
+ function toOrdered(nodes) {
20
+ return nodes.map(toOrderedNode);
21
+ }
22
+ function attrsObject(attributes) {
23
+ const obj = {};
24
+ for (const a of attributes) obj[`@_${a.name}`] = a.value;
25
+ return obj;
26
+ }
27
+ function toOrderedNode(node) {
28
+ switch (node.type) {
29
+ case "text": return { "#text": node.value };
30
+ case "comment": return { __comment: [{ "#text": node.value }] };
31
+ case "cdata": return { __cdata: [{ "#text": node.value }] };
32
+ case "pi": return { [`?${node.target}`]: [{ "#text": node.content }] };
33
+ case "declaration": return {
34
+ "?xml": [{ "#text": "" }],
35
+ ":@": attrsObject(node.attributes)
36
+ };
37
+ case "element": {
38
+ const obj = { [node.tag]: toOrdered(node.children) };
39
+ const attrs = attrsObject(node.attributes);
40
+ if (Object.keys(attrs).length > 0) obj[":@"] = attrs;
41
+ return obj;
42
+ }
43
+ }
44
+ }
45
+ //#endregion
46
+ export { buildXml };
@@ -0,0 +1,20 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/xml/entities.ts
3
+ function decodeEntities(value) {
4
+ return value.replace(/&(?:amp|lt|gt|quot|apos);/g, (entity) => {
5
+ switch (entity) {
6
+ case "&amp;": return "&";
7
+ case "&lt;": return "<";
8
+ case "&gt;": return ">";
9
+ case "&quot;": return "\"";
10
+ case "&apos;": return "'";
11
+ default: return entity;
12
+ }
13
+ });
14
+ }
15
+ function encodeEntities(value) {
16
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
17
+ }
18
+ //#endregion
19
+ exports.decodeEntities = decodeEntities;
20
+ exports.encodeEntities = encodeEntities;
@@ -0,0 +1,5 @@
1
+ //#region src/xml/entities.d.ts
2
+ declare function decodeEntities(value: string): string;
3
+ declare function encodeEntities(value: string): string;
4
+ //#endregion
5
+ export { decodeEntities, encodeEntities };
@@ -0,0 +1,5 @@
1
+ //#region src/xml/entities.d.ts
2
+ declare function decodeEntities(value: string): string;
3
+ declare function encodeEntities(value: string): string;
4
+ //#endregion
5
+ export { decodeEntities, encodeEntities };
@@ -0,0 +1,18 @@
1
+ //#region src/xml/entities.ts
2
+ function decodeEntities(value) {
3
+ return value.replace(/&(?:amp|lt|gt|quot|apos);/g, (entity) => {
4
+ switch (entity) {
5
+ case "&amp;": return "&";
6
+ case "&lt;": return "<";
7
+ case "&gt;": return ">";
8
+ case "&quot;": return "\"";
9
+ case "&apos;": return "'";
10
+ default: return entity;
11
+ }
12
+ });
13
+ }
14
+ function encodeEntities(value) {
15
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
16
+ }
17
+ //#endregion
18
+ export { decodeEntities, encodeEntities };
@@ -0,0 +1,67 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let zod = require("zod");
3
+ //#region src/xml/node.ts
4
+ const AttributeSchema = zod.z.object({
5
+ name: zod.z.string(),
6
+ value: zod.z.string()
7
+ });
8
+ const XmlTextSchema = zod.z.object({
9
+ type: zod.z.literal("text"),
10
+ value: zod.z.string()
11
+ });
12
+ const XmlCdataSchema = zod.z.object({
13
+ type: zod.z.literal("cdata"),
14
+ value: zod.z.string()
15
+ });
16
+ const XmlCommentSchema = zod.z.object({
17
+ type: zod.z.literal("comment"),
18
+ value: zod.z.string()
19
+ });
20
+ const XmlDeclarationSchema = zod.z.object({
21
+ type: zod.z.literal("declaration"),
22
+ attributes: zod.z.array(AttributeSchema)
23
+ });
24
+ const XmlPiSchema = zod.z.object({
25
+ type: zod.z.literal("pi"),
26
+ target: zod.z.string(),
27
+ content: zod.z.string()
28
+ });
29
+ function isRecord(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+ function isAttribute(value) {
33
+ return isRecord(value) && typeof value.name === "string" && typeof value.value === "string";
34
+ }
35
+ function isXmlNode(value) {
36
+ if (!isRecord(value)) return false;
37
+ const t = value.type;
38
+ if (t === "text" || t === "cdata" || t === "comment") return typeof value.value === "string";
39
+ if (t === "declaration") return Array.isArray(value.attributes) && value.attributes.every(isAttribute);
40
+ if (t === "pi") return typeof value.target === "string" && typeof value.content === "string";
41
+ if (t === "element") return typeof value.tag === "string" && Array.isArray(value.attributes) && value.attributes.every(isAttribute) && Array.isArray(value.children) && value.children.every(isXmlNode);
42
+ return false;
43
+ }
44
+ const XmlElementSchema = zod.z.object({
45
+ type: zod.z.literal("element"),
46
+ tag: zod.z.string(),
47
+ attributes: zod.z.array(AttributeSchema),
48
+ children: zod.z.array(zod.z.custom(isXmlNode))
49
+ });
50
+ const XmlNodeSchema = zod.z.discriminatedUnion("type", [
51
+ XmlTextSchema,
52
+ XmlCdataSchema,
53
+ XmlCommentSchema,
54
+ XmlDeclarationSchema,
55
+ XmlPiSchema,
56
+ XmlElementSchema
57
+ ]);
58
+ //#endregion
59
+ exports.AttributeSchema = AttributeSchema;
60
+ exports.XmlCdataSchema = XmlCdataSchema;
61
+ exports.XmlCommentSchema = XmlCommentSchema;
62
+ exports.XmlDeclarationSchema = XmlDeclarationSchema;
63
+ exports.XmlElementSchema = XmlElementSchema;
64
+ exports.XmlNodeSchema = XmlNodeSchema;
65
+ exports.XmlPiSchema = XmlPiSchema;
66
+ exports.XmlTextSchema = XmlTextSchema;
67
+ exports.isXmlNode = isXmlNode;
@@ -0,0 +1,83 @@
1
+ import { z } from "zod";
2
+ //#region src/xml/node.d.ts
3
+ declare const AttributeSchema: z.ZodObject<{
4
+ name: z.ZodString;
5
+ value: z.ZodString;
6
+ }, z.core.$strip>;
7
+ type Attribute = z.infer<typeof AttributeSchema>;
8
+ declare const XmlTextSchema: z.ZodObject<{
9
+ type: z.ZodLiteral<"text">;
10
+ value: z.ZodString;
11
+ }, z.core.$strip>;
12
+ type XmlText = z.infer<typeof XmlTextSchema>;
13
+ declare const XmlCdataSchema: z.ZodObject<{
14
+ type: z.ZodLiteral<"cdata">;
15
+ value: z.ZodString;
16
+ }, z.core.$strip>;
17
+ type XmlCdata = z.infer<typeof XmlCdataSchema>;
18
+ declare const XmlCommentSchema: z.ZodObject<{
19
+ type: z.ZodLiteral<"comment">;
20
+ value: z.ZodString;
21
+ }, z.core.$strip>;
22
+ type XmlComment = z.infer<typeof XmlCommentSchema>;
23
+ declare const XmlDeclarationSchema: z.ZodObject<{
24
+ type: z.ZodLiteral<"declaration">;
25
+ attributes: z.ZodArray<z.ZodObject<{
26
+ name: z.ZodString;
27
+ value: z.ZodString;
28
+ }, z.core.$strip>>;
29
+ }, z.core.$strip>;
30
+ type XmlDeclaration = z.infer<typeof XmlDeclarationSchema>;
31
+ declare const XmlPiSchema: z.ZodObject<{
32
+ type: z.ZodLiteral<"pi">;
33
+ target: z.ZodString;
34
+ content: z.ZodString;
35
+ }, z.core.$strip>;
36
+ type XmlPi = z.infer<typeof XmlPiSchema>;
37
+ interface XmlElement {
38
+ type: "element";
39
+ tag: string;
40
+ attributes: Attribute[];
41
+ children: XmlNode[];
42
+ }
43
+ type XmlNode = XmlText | XmlCdata | XmlComment | XmlDeclaration | XmlPi | XmlElement;
44
+ declare function isXmlNode(value: unknown): value is XmlNode;
45
+ declare const XmlElementSchema: z.ZodObject<{
46
+ type: z.ZodLiteral<"element">;
47
+ tag: z.ZodString;
48
+ attributes: z.ZodArray<z.ZodObject<{
49
+ name: z.ZodString;
50
+ value: z.ZodString;
51
+ }, z.core.$strip>>;
52
+ children: z.ZodArray<z.ZodCustom<XmlNode, XmlNode>>;
53
+ }, z.core.$strip>;
54
+ declare const XmlNodeSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
55
+ type: z.ZodLiteral<"text">;
56
+ value: z.ZodString;
57
+ }, z.core.$strip>, z.ZodObject<{
58
+ type: z.ZodLiteral<"cdata">;
59
+ value: z.ZodString;
60
+ }, z.core.$strip>, z.ZodObject<{
61
+ type: z.ZodLiteral<"comment">;
62
+ value: z.ZodString;
63
+ }, z.core.$strip>, z.ZodObject<{
64
+ type: z.ZodLiteral<"declaration">;
65
+ attributes: z.ZodArray<z.ZodObject<{
66
+ name: z.ZodString;
67
+ value: z.ZodString;
68
+ }, z.core.$strip>>;
69
+ }, z.core.$strip>, z.ZodObject<{
70
+ type: z.ZodLiteral<"pi">;
71
+ target: z.ZodString;
72
+ content: z.ZodString;
73
+ }, z.core.$strip>, z.ZodObject<{
74
+ type: z.ZodLiteral<"element">;
75
+ tag: z.ZodString;
76
+ attributes: z.ZodArray<z.ZodObject<{
77
+ name: z.ZodString;
78
+ value: z.ZodString;
79
+ }, z.core.$strip>>;
80
+ children: z.ZodArray<z.ZodCustom<XmlNode, XmlNode>>;
81
+ }, z.core.$strip>], "type">;
82
+ //#endregion
83
+ export { Attribute, AttributeSchema, XmlCdata, XmlCdataSchema, XmlComment, XmlCommentSchema, XmlDeclaration, XmlDeclarationSchema, XmlElement, XmlElementSchema, XmlNode, XmlNodeSchema, XmlPi, XmlPiSchema, XmlText, XmlTextSchema, isXmlNode };
@@ -0,0 +1,83 @@
1
+ import { z } from "zod";
2
+ //#region src/xml/node.d.ts
3
+ declare const AttributeSchema: z.ZodObject<{
4
+ name: z.ZodString;
5
+ value: z.ZodString;
6
+ }, z.core.$strip>;
7
+ type Attribute = z.infer<typeof AttributeSchema>;
8
+ declare const XmlTextSchema: z.ZodObject<{
9
+ type: z.ZodLiteral<"text">;
10
+ value: z.ZodString;
11
+ }, z.core.$strip>;
12
+ type XmlText = z.infer<typeof XmlTextSchema>;
13
+ declare const XmlCdataSchema: z.ZodObject<{
14
+ type: z.ZodLiteral<"cdata">;
15
+ value: z.ZodString;
16
+ }, z.core.$strip>;
17
+ type XmlCdata = z.infer<typeof XmlCdataSchema>;
18
+ declare const XmlCommentSchema: z.ZodObject<{
19
+ type: z.ZodLiteral<"comment">;
20
+ value: z.ZodString;
21
+ }, z.core.$strip>;
22
+ type XmlComment = z.infer<typeof XmlCommentSchema>;
23
+ declare const XmlDeclarationSchema: z.ZodObject<{
24
+ type: z.ZodLiteral<"declaration">;
25
+ attributes: z.ZodArray<z.ZodObject<{
26
+ name: z.ZodString;
27
+ value: z.ZodString;
28
+ }, z.core.$strip>>;
29
+ }, z.core.$strip>;
30
+ type XmlDeclaration = z.infer<typeof XmlDeclarationSchema>;
31
+ declare const XmlPiSchema: z.ZodObject<{
32
+ type: z.ZodLiteral<"pi">;
33
+ target: z.ZodString;
34
+ content: z.ZodString;
35
+ }, z.core.$strip>;
36
+ type XmlPi = z.infer<typeof XmlPiSchema>;
37
+ interface XmlElement {
38
+ type: "element";
39
+ tag: string;
40
+ attributes: Attribute[];
41
+ children: XmlNode[];
42
+ }
43
+ type XmlNode = XmlText | XmlCdata | XmlComment | XmlDeclaration | XmlPi | XmlElement;
44
+ declare function isXmlNode(value: unknown): value is XmlNode;
45
+ declare const XmlElementSchema: z.ZodObject<{
46
+ type: z.ZodLiteral<"element">;
47
+ tag: z.ZodString;
48
+ attributes: z.ZodArray<z.ZodObject<{
49
+ name: z.ZodString;
50
+ value: z.ZodString;
51
+ }, z.core.$strip>>;
52
+ children: z.ZodArray<z.ZodCustom<XmlNode, XmlNode>>;
53
+ }, z.core.$strip>;
54
+ declare const XmlNodeSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
55
+ type: z.ZodLiteral<"text">;
56
+ value: z.ZodString;
57
+ }, z.core.$strip>, z.ZodObject<{
58
+ type: z.ZodLiteral<"cdata">;
59
+ value: z.ZodString;
60
+ }, z.core.$strip>, z.ZodObject<{
61
+ type: z.ZodLiteral<"comment">;
62
+ value: z.ZodString;
63
+ }, z.core.$strip>, z.ZodObject<{
64
+ type: z.ZodLiteral<"declaration">;
65
+ attributes: z.ZodArray<z.ZodObject<{
66
+ name: z.ZodString;
67
+ value: z.ZodString;
68
+ }, z.core.$strip>>;
69
+ }, z.core.$strip>, z.ZodObject<{
70
+ type: z.ZodLiteral<"pi">;
71
+ target: z.ZodString;
72
+ content: z.ZodString;
73
+ }, z.core.$strip>, z.ZodObject<{
74
+ type: z.ZodLiteral<"element">;
75
+ tag: z.ZodString;
76
+ attributes: z.ZodArray<z.ZodObject<{
77
+ name: z.ZodString;
78
+ value: z.ZodString;
79
+ }, z.core.$strip>>;
80
+ children: z.ZodArray<z.ZodCustom<XmlNode, XmlNode>>;
81
+ }, z.core.$strip>], "type">;
82
+ //#endregion
83
+ export { Attribute, AttributeSchema, XmlCdata, XmlCdataSchema, XmlComment, XmlCommentSchema, XmlDeclaration, XmlDeclarationSchema, XmlElement, XmlElementSchema, XmlNode, XmlNodeSchema, XmlPi, XmlPiSchema, XmlText, XmlTextSchema, isXmlNode };
@@ -0,0 +1,58 @@
1
+ import { z } from "zod";
2
+ //#region src/xml/node.ts
3
+ const AttributeSchema = z.object({
4
+ name: z.string(),
5
+ value: z.string()
6
+ });
7
+ const XmlTextSchema = z.object({
8
+ type: z.literal("text"),
9
+ value: z.string()
10
+ });
11
+ const XmlCdataSchema = z.object({
12
+ type: z.literal("cdata"),
13
+ value: z.string()
14
+ });
15
+ const XmlCommentSchema = z.object({
16
+ type: z.literal("comment"),
17
+ value: z.string()
18
+ });
19
+ const XmlDeclarationSchema = z.object({
20
+ type: z.literal("declaration"),
21
+ attributes: z.array(AttributeSchema)
22
+ });
23
+ const XmlPiSchema = z.object({
24
+ type: z.literal("pi"),
25
+ target: z.string(),
26
+ content: z.string()
27
+ });
28
+ function isRecord(value) {
29
+ return typeof value === "object" && value !== null && !Array.isArray(value);
30
+ }
31
+ function isAttribute(value) {
32
+ return isRecord(value) && typeof value.name === "string" && typeof value.value === "string";
33
+ }
34
+ function isXmlNode(value) {
35
+ if (!isRecord(value)) return false;
36
+ const t = value.type;
37
+ if (t === "text" || t === "cdata" || t === "comment") return typeof value.value === "string";
38
+ if (t === "declaration") return Array.isArray(value.attributes) && value.attributes.every(isAttribute);
39
+ if (t === "pi") return typeof value.target === "string" && typeof value.content === "string";
40
+ if (t === "element") return typeof value.tag === "string" && Array.isArray(value.attributes) && value.attributes.every(isAttribute) && Array.isArray(value.children) && value.children.every(isXmlNode);
41
+ return false;
42
+ }
43
+ const XmlElementSchema = z.object({
44
+ type: z.literal("element"),
45
+ tag: z.string(),
46
+ attributes: z.array(AttributeSchema),
47
+ children: z.array(z.custom(isXmlNode))
48
+ });
49
+ const XmlNodeSchema = z.discriminatedUnion("type", [
50
+ XmlTextSchema,
51
+ XmlCdataSchema,
52
+ XmlCommentSchema,
53
+ XmlDeclarationSchema,
54
+ XmlPiSchema,
55
+ XmlElementSchema
56
+ ]);
57
+ //#endregion
58
+ export { AttributeSchema, XmlCdataSchema, XmlCommentSchema, XmlDeclarationSchema, XmlElementSchema, XmlNodeSchema, XmlPiSchema, XmlTextSchema, isXmlNode };