@mak-98/xml-dirvi-app 0.11.0-beta.7 → 0.11.0-beta.7.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.
@@ -77,6 +77,22 @@ export declare const XmlElementValueSchema: z.ZodObject<{
77
77
  value: z.ZodString;
78
78
  }, z.core.$strip>>>;
79
79
  }, z.core.$strip>;
80
+ export declare const XmlDeclarationValueSchema: z.ZodObject<{
81
+ kind: z.ZodLiteral<"declaration">;
82
+ version: z.ZodString;
83
+ encoding: z.ZodNullable<z.ZodString>;
84
+ standalone: z.ZodNullable<z.ZodEnum<{
85
+ yes: "yes";
86
+ no: "no";
87
+ }>>;
88
+ }, z.core.$strip>;
89
+ export declare const XmlDoctypeValueSchema: z.ZodObject<{
90
+ kind: z.ZodLiteral<"doctype">;
91
+ name: z.ZodString;
92
+ publicId: z.ZodNullable<z.ZodString>;
93
+ systemId: z.ZodNullable<z.ZodString>;
94
+ internalSubset: z.ZodNullable<z.ZodString>;
95
+ }, z.core.$strip>;
80
96
  export declare const XmlTextValueSchema: z.ZodObject<{
81
97
  kind: z.ZodLiteral<"text">;
82
98
  text: z.ZodString;
@@ -1 +1 @@
1
- {"version":3,"file":"xml-tree.d.ts","sourceRoot":"","sources":["../../src/domain/xml-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAmB,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAcxE;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,aAavB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC;AAMhD,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK,CAE7D;AAED,wBAAgB,SAAS,CAAC,EAAE,EAAE,KAAK,GAAG,MAAM,CAE3C;AAED,w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AAG,IAAI,YACb,MAAM,GAAG,IAAI,kBACP,MAAM,GAAG,IAAI,KAC5B,WAAW;2BAWR,KAAK,QACH,MAAM,cACA,SAAS,YAAY,EAAE,YACzB,QAAQ,CAAC,WAAW,CAAC,KAC9B,WAAW,GAAG,SAAS;wBAYjB,KAAK,QAAQ,MAAM,KAAG,WAAW;2BAO9B,KAAK,QAAQ,MAAM,KAAG,WAAW;yBAOnC,KAAK,QAAQ,MAAM,KAAG,WAAW;yCAQrC,KAAK,UACD,MAAM,QACR,MAAM,KACX,WAAW;EAQjB"}
@@ -41,7 +41,19 @@ export const XmlElementValueSchema = z.object({
41
41
  name: z.string().min(1),
42
42
  attributes: z.array(XmlAttributeSchema).readonly(),
43
43
  });
44
- // TODO: add declaration and doctype when schema is needed
44
+ export const XmlDeclarationValueSchema = z.object({
45
+ kind: z.literal('declaration'),
46
+ version: z.string(),
47
+ encoding: z.string().nullable(),
48
+ standalone: z.enum(['yes', 'no']).nullable(),
49
+ });
50
+ export const XmlDoctypeValueSchema = z.object({
51
+ kind: z.literal('doctype'),
52
+ name: z.string(),
53
+ publicId: z.string().nullable(),
54
+ systemId: z.string().nullable(),
55
+ internalSubset: z.string().nullable(),
56
+ });
45
57
  export const XmlTextValueSchema = z.object({
46
58
  kind: z.literal('text'),
47
59
  text: z.string(),
@@ -1,20 +1,9 @@
1
- import type { XmlNode } from '@rgrove/parse-xml';
2
- import type { TreeMethods } from 'dirvi-lib';
3
- import type { XmlNodeValue, XmlTreeNode } from '../domain/xml-tree.js';
4
- import type { XmlId } from '../domain/xml-tree.js';
1
+ import type { AppChild, AppRoot } from 'dirvi-apis';
2
+ import type { XmlId, XmlNodeValue } from '../domain/xml-tree.js';
3
+ export declare function createXmlRoot(filePath: string): AppRoot<XmlId, XmlNodeValue>;
5
4
  /**
6
- * Allocates an XML tree ID containing the node's name and a unique UUID.
7
- *
8
- * IDs identify nodes and cursor paths. They do not represent sibling order:
9
- * source child order is preserved by the TreeNode child sequence.
5
+ * Returns direct children in parser order. Paths are relative to the document
6
+ * root; an empty path selects the document itself.
10
7
  */
11
- export type CreateXmlId = (name: string) => XmlId;
12
- /**
13
- * Converts XML parser nodes into ordered Dirvi XML tree nodes.
14
- *
15
- * The returned array preserves parser child order exactly. In particular,
16
- * mixed content such as text, elements, comments, and CDATA remains in its
17
- * source-document order.
18
- */
19
- export declare function getXmlEntries(treeNodeApi: TreeMethods<XmlId, XmlNodeValue>, xmlNodes: readonly XmlNode[], createXmlId: CreateXmlId): XmlTreeNode[];
8
+ export declare function getXmlEntries(filePath: string, source: string, path: readonly XmlId[]): readonly AppChild<XmlId, XmlNodeValue>[];
20
9
  //# sourceMappingURL=get-xml-entries.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-xml-entries.d.ts","sourceRoot":"","sources":["../../src/infrastructure/get-xml-entries.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACZ,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,KAAK,CAAC;AAElD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,WAAW,EAAE,WAAW,CAAC,KAAK,EAAE,YAAY,CAAC,EAC7C,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,WAAW,EAAE,WAAW,GACvB,WAAW,EAAE,CAwEf"}
1
+ {"version":3,"file":"get-xml-entries.d.ts","sourceRoot":"","sources":["../../src/infrastructure/get-xml-entries.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAiIjE,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,YAAY,CAAC,CAK5E;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,KAAK,EAAE,GACrB,SAAS,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC,EAAE,CA2B1C"}
@@ -1,70 +1,134 @@
1
- import { XmlCdata, XmlComment, XmlDeclaration, XmlDocumentType, XmlElement, XmlProcessingInstruction, XmlText, } from '@rgrove/parse-xml';
2
- import { createConstructors } from '../domain/xml-tree.js';
3
- /**
4
- * Converts XML parser nodes into ordered Dirvi XML tree nodes.
5
- *
6
- * The returned array preserves parser child order exactly. In particular,
7
- * mixed content such as text, elements, comments, and CDATA remains in its
8
- * source-document order.
9
- */
10
- export function getXmlEntries(treeNodeApi, xmlNodes, createXmlId) {
11
- return xmlNodes.map((xmlNode) => {
12
- const id = createXmlId(getXmlNodeName(xmlNode));
13
- const constructors = createConstructors(treeNodeApi);
14
- if (xmlNode instanceof XmlElement) {
15
- const children = getXmlEntries(treeNodeApi, xmlNode.children, createXmlId);
16
- const node = constructors.element(id, xmlNode.name, Object.entries(xmlNode.attributes).map(([name, value]) => ({
17
- name,
18
- value,
19
- })), children);
20
- if (node === undefined) {
21
- throw new Error(`Cannot create XML element "${xmlNode.name}": duplicate child IDs.`);
22
- }
23
- return node;
24
- }
25
- if (xmlNode instanceof XmlText) {
26
- return constructors.text(id, xmlNode.text);
27
- }
28
- if (xmlNode instanceof XmlCdata) {
29
- return constructors.cdata(id, xmlNode.text);
30
- }
31
- if (xmlNode instanceof XmlComment) {
32
- return constructors.comment(id, xmlNode.content);
33
- }
34
- if (xmlNode instanceof XmlProcessingInstruction) {
35
- return constructors.processingInstruction(id, xmlNode.name, xmlNode.content);
36
- }
37
- if (xmlNode instanceof XmlDeclaration) {
38
- return constructors.declaration(id, xmlNode.version, xmlNode.encoding, xmlNode.standalone);
39
- }
40
- if (xmlNode instanceof XmlDocumentType) {
41
- return constructors.doctype(id, xmlNode.name, xmlNode.publicId, xmlNode.systemId, xmlNode.internalSubset);
42
- }
43
- throw new Error(`Unsupported XML node type "${xmlNode.type}".`);
44
- });
1
+ import { createHash } from 'node:crypto';
2
+ import { parseXml, XmlCdata, XmlComment, XmlDeclaration, XmlDocumentType, XmlElement, XmlProcessingInstruction, XmlText, } from '@rgrove/parse-xml';
3
+ import { createXmlId } from '../domain/xml-tree.js';
4
+ // RFC 4122 URL namespace, used to derive deterministic version-5 UUIDs.
5
+ const uuidNamespace = Buffer.from('6ba7b8119dad11d180b400c04fd430c8', 'hex');
6
+ function nodeId(filePath, position, name) {
7
+ const hash = createHash('sha1')
8
+ .update(uuidNamespace)
9
+ .update(JSON.stringify([filePath, position, name]), 'utf8')
10
+ .digest();
11
+ const bytes = Buffer.from(hash.subarray(0, 16));
12
+ bytes[6] = (bytes[6] & 0x0f) | 0x50; // UUID version 5
13
+ bytes[8] = (bytes[8] & 0x3f) | 0x80; // RFC variant
14
+ const hex = bytes.toString('hex');
15
+ const uuid = [
16
+ hex.slice(0, 8),
17
+ hex.slice(8, 12),
18
+ hex.slice(12, 16),
19
+ hex.slice(16, 20),
20
+ hex.slice(20, 32),
21
+ ].join('-');
22
+ return createXmlId(name, uuid);
45
23
  }
46
- /**
47
- * Returns the XML name where one exists, otherwise a synthetic node label.
48
- *
49
- * Namespace prefixes are preserved, for example "svg:path".
50
- */
51
- function getXmlNodeName(xmlNode) {
52
- if (xmlNode instanceof XmlElement ||
53
- xmlNode instanceof XmlProcessingInstruction ||
54
- xmlNode instanceof XmlDocumentType) {
55
- return xmlNode.name;
24
+ function nodeName(node) {
25
+ if (node instanceof XmlElement ||
26
+ node instanceof XmlProcessingInstruction ||
27
+ node instanceof XmlDocumentType) {
28
+ return node.name;
56
29
  }
57
- if (xmlNode instanceof XmlText) {
30
+ if (node instanceof XmlText)
58
31
  return '#text';
59
- }
60
- if (xmlNode instanceof XmlCdata) {
32
+ if (node instanceof XmlCdata)
61
33
  return '#cdata';
62
- }
63
- if (xmlNode instanceof XmlComment) {
34
+ if (node instanceof XmlComment)
64
35
  return '#comment';
65
- }
66
- if (xmlNode instanceof XmlDeclaration) {
36
+ if (node instanceof XmlDeclaration)
67
37
  return '#declaration';
38
+ throw new Error(`Unsupported XML node type "${node.type}".`);
39
+ }
40
+ function describeChild(filePath, position, node) {
41
+ const id = nodeId(filePath, position, nodeName(node));
42
+ if (node instanceof XmlElement) {
43
+ return {
44
+ id,
45
+ kind: 'unloaded-branch',
46
+ value: {
47
+ kind: 'element',
48
+ name: node.name,
49
+ attributes: Object.entries(node.attributes).map(([name, value]) => ({
50
+ name,
51
+ value,
52
+ })),
53
+ },
54
+ };
55
+ }
56
+ if (node instanceof XmlText) {
57
+ return { id, kind: 'leaf', value: { kind: 'text', text: node.text } };
58
+ }
59
+ if (node instanceof XmlCdata) {
60
+ return { id, kind: 'leaf', value: { kind: 'cdata', text: node.text } };
61
+ }
62
+ if (node instanceof XmlComment) {
63
+ return {
64
+ id,
65
+ kind: 'leaf',
66
+ value: { kind: 'comment', text: node.content },
67
+ };
68
+ }
69
+ if (node instanceof XmlProcessingInstruction) {
70
+ return {
71
+ id,
72
+ kind: 'leaf',
73
+ value: {
74
+ kind: 'processing-instruction',
75
+ target: node.name,
76
+ data: node.content,
77
+ },
78
+ };
79
+ }
80
+ if (node instanceof XmlDeclaration) {
81
+ return {
82
+ id,
83
+ kind: 'leaf',
84
+ value: {
85
+ kind: 'declaration',
86
+ version: node.version,
87
+ encoding: node.encoding,
88
+ standalone: node.standalone,
89
+ },
90
+ };
91
+ }
92
+ if (node instanceof XmlDocumentType) {
93
+ return {
94
+ id,
95
+ kind: 'leaf',
96
+ value: {
97
+ kind: 'doctype',
98
+ name: node.name,
99
+ publicId: node.publicId,
100
+ systemId: node.systemId,
101
+ internalSubset: node.internalSubset,
102
+ },
103
+ };
104
+ }
105
+ throw new Error(`Unsupported XML node type "${node.type}".`);
106
+ }
107
+ export function createXmlRoot(filePath) {
108
+ return {
109
+ id: nodeId(filePath, [], '#document'),
110
+ value: { kind: 'document' },
111
+ };
112
+ }
113
+ /**
114
+ * Returns direct children in parser order. Paths are relative to the document
115
+ * root; an empty path selects the document itself.
116
+ */
117
+ export function getXmlEntries(filePath, source, path) {
118
+ const document = parseXml(source);
119
+ let children = document.children;
120
+ let position = [];
121
+ for (const segment of path) {
122
+ const index = children.findIndex((node, i) => nodeId(filePath, [...position, i], nodeName(node)) === segment);
123
+ if (index === -1) {
124
+ throw new Error(`XML path not found: ${path.join(' / ')}`);
125
+ }
126
+ const node = children[index];
127
+ if (!(node instanceof XmlElement)) {
128
+ throw new Error(`XML path addresses a leaf: ${path.join(' / ')}`);
129
+ }
130
+ position = [...position, index];
131
+ children = node.children;
68
132
  }
69
- throw new Error(`Unsupported XML node type "${xmlNode.type}".`);
133
+ return children.map((node, index) => describeChild(filePath, [...position, index], node));
70
134
  }
@@ -1,12 +1,3 @@
1
1
  import type { AppPlugin } from 'dirvi-apis/plugin';
2
- /**
3
- * Creates applicator of `AppDefinition<Position, XmlValue>`.
4
- *
5
- * This style is used, instead of just creating the `AppDefinition`, for the
6
- * user of the package to be able to apply the AppDefinition without specifying
7
- * the generics.
8
- *
9
- * @param input - the path of the XML file to open.
10
- */
11
2
  export declare const xmlAppPlugin: AppPlugin;
12
3
  //# sourceMappingURL=xml-dirvi-app.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"xml-dirvi-app.d.ts","sourceRoot":"","sources":["../src/xml-dirvi-app.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAQnD;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,EAAE,SAwC1B,CAAC"}
1
+ {"version":3,"file":"xml-dirvi-app.d.ts","sourceRoot":"","sources":["../src/xml-dirvi-app.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAUnD,eAAO,MAAM,YAAY,EAAE,SA8B1B,CAAC"}
@@ -2,16 +2,7 @@ import { readFile } from 'node:fs/promises';
2
2
  import { resolve } from 'node:path';
3
3
  import { xmlIdSchema, xmlValueSchema } from './domain/xml-tree.js';
4
4
  import { createXmlFileResyncSubscription } from './infrastructure/create-xml-file-resync.js';
5
- import { createXmlId, parseXmlDocument } from './infrastructure/parse-xml-document.js';
6
- /**
7
- * Creates applicator of `AppDefinition<Position, XmlValue>`.
8
- *
9
- * This style is used, instead of just creating the `AppDefinition`, for the
10
- * user of the package to be able to apply the AppDefinition without specifying
11
- * the generics.
12
- *
13
- * @param input - the path of the XML file to open.
14
- */
5
+ import { createXmlRoot, getXmlEntries, } from './infrastructure/get-xml-entries.js';
15
6
  export const xmlAppPlugin = {
16
7
  createAppDefinitionApplicator: (input) => {
17
8
  if (input === undefined) {
@@ -20,28 +11,20 @@ export const xmlAppPlugin = {
20
11
  const xmlAbsolutePath = resolve(input);
21
12
  const appDefinition = {
22
13
  displayName: `XML: ${input}`,
23
- rootId: createXmlId('document'),
24
14
  semanticOrder: true,
25
15
  idSchema: xmlIdSchema,
26
16
  valueSchema: xmlValueSchema,
27
- createRoot: async (treeNodeApi) => {
28
- const source = await readFile(input, 'utf8');
29
- return parseXmlDocument(treeNodeApi, source);
30
- },
31
- loadChildren: async (treeNodeApi, path) => {
32
- // TODO Later: load from the file
33
- throw new Error('Cannot load XML branches: parsed XML documents have no unloaded branches.');
17
+ loadChildren: async (path) => {
18
+ const source = await readFile(xmlAbsolutePath, 'utf8');
19
+ return getXmlEntries(xmlAbsolutePath, source, path);
34
20
  },
21
+ loadRoot: async () => createXmlRoot(xmlAbsolutePath),
35
22
  presentNode: (id, isBranch) => ({
36
- // Split at the first colon only, preserving names such as "svg:path".
37
23
  content: id.slice(id.indexOf(':') + 1),
38
24
  highlight: isBranch ? 'branch' : 'leaf',
39
25
  }),
40
26
  subscribeToResync: createXmlFileResyncSubscription(xmlAbsolutePath),
41
- createNavNodeApiOptions: {},
42
- };
43
- return (appDefinitionConsumer) => {
44
- return appDefinitionConsumer(appDefinition);
45
27
  };
28
+ return (appDefinitionConsumer) => appDefinitionConsumer(appDefinition);
46
29
  },
47
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mak-98/xml-dirvi-app",
3
- "version": "0.11.0-beta.7",
3
+ "version": "0.11.0-beta.7.1",
4
4
  "description": "XML file app for dirvi.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "type": "module",
@@ -20,12 +20,12 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@rgrove/parse-xml": "^5.0.0",
23
- "dirvi-apis": "0.11.0-beta.6",
23
+ "dirvi-apis": "0.11.0-beta.7",
24
24
  "env-paths": "^4.0.0",
25
25
  "zod": "^4.4.3"
26
26
  },
27
27
  "devDependencies": {
28
- "dirvi-lib": "0.11.0-beta.6",
28
+ "dirvi-lib": "0.11.0-beta.7",
29
29
  "@types/node": "^22.0.0",
30
30
  "typescript": "^5.0.0"
31
31
  },