@iebh/reflib 2.3.2 → 2.3.3

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.
@@ -0,0 +1,37 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Run npm test:browser",
11
+ "runtimeExecutable": "/home/connor/.nvm/versions/node/v20.12.2/bin/npm",
12
+ "runtimeArgs": [
13
+ "run",
14
+ "test:browser"
15
+ ],
16
+ "console": "integratedTerminal"
17
+ },
18
+ {
19
+ "name": "Launch Chrome against localhost",
20
+ "type": "chrome",
21
+ "request": "launch",
22
+ "url": "http://localhost:3000",
23
+ "webRoot": "${workspaceFolder}/test/browser"
24
+ },
25
+ {
26
+ "name": "Attach to Vite Server",
27
+ "type": "node",
28
+ "request": "attach",
29
+ "port": 9229,
30
+ "address": "localhost",
31
+ "restart": true,
32
+ "skipFiles": [
33
+ "<node_internals>/**"
34
+ ]
35
+ }
36
+ ]
37
+ }
@@ -1,56 +1,57 @@
1
1
  import camelCase from '../../shared/camelCase.js';
2
2
  import Emitter from '../../shared/emitter.js';
3
+ import CacxParser from '@iebh/cacx';
3
4
 
4
5
  export class WritableStream {
5
- constructor(passedParserOptions) {
6
- // Stores XML in text form once read
7
- this.text = "";
8
- this.emitter = Emitter();
9
- // Add event listeners to mimic htmlparser2 behavior in the browser
10
- this.emitter.on('opentag', passedParserOptions.onopentag);
11
- this.emitter.on('closetag', passedParserOptions.onclosetag);
12
- this.emitter.on('text', passedParserOptions.ontext);
13
- this.emitter.on('end', passedParserOptions.onend);
14
- }
15
-
16
- write(data) {
17
- // CF: TODO: Parse data as it comes in chunks for better memory efficiency
18
- this.text += data;
19
- }
20
-
21
- end() {
22
- this.parseXML(this.text);
23
- // Free memory
24
- this.text = ''
25
- this.emitter.emit('end');
26
- }
27
-
28
- parseXML(xmlString) {
29
- let parser = new DOMParser();
30
- let doc = parser.parseFromString(xmlString, 'application/xml');
31
- this.traverseNode(doc.documentElement);
32
- }
33
-
34
- traverseNode(node) {
35
- if (node.nodeType === Node.ELEMENT_NODE) {
36
- let name = camelCase(node.nodeName);
37
- let attrs = Array.from(node.attributes).reduce((acc, attr) => {
38
- acc[attr.name] = attr.value;
39
- return acc;
40
- }, {});
41
-
42
- this.emitter.emit('opentag', name, attrs);
43
-
44
- for (let child of node.childNodes) {
45
- this.traverseNode(child);
46
- }
47
-
48
- this.emitter.emit('closetag', name);
49
- } else if (node.nodeType === Node.TEXT_NODE) {
50
- let text = node.nodeValue.trim();
51
- if (text) {
52
- this.emitter.emit('text', text);
53
- }
54
- }
55
- }
6
+ constructor(passedParserOptions) {
7
+ this.emitter = Emitter();
8
+ this.parser = new CacxParser({
9
+ collect: false,
10
+ onTagOpen: (node) => {
11
+ const name = camelCase(node.tag);
12
+ const attrs = node.attrs || {};
13
+ this.emitter.emit('opentag', name, attrs);
14
+ },
15
+ onTagClose: (node) => {
16
+ const name = camelCase(node.tag);
17
+ this.emitter.emit('closetag', name);
18
+ },
19
+ onAttr: (key, val/*, options*/) => {
20
+ // This method is called for each attribute, so we don't need to do anything here
21
+ return { [key]: val };
22
+ },
23
+ flattenText: true,
24
+ keyText: 'text',
25
+ keyAttrs: 'attrs',
26
+ });
27
+
28
+ // Add event listeners to mimic htmlparser2 behavior
29
+ this.emitter.on('opentag', passedParserOptions.onopentag);
30
+ this.emitter.on('closetag', passedParserOptions.onclosetag);
31
+ this.emitter.on('text', passedParserOptions.ontext);
32
+ this.emitter.on('end', passedParserOptions.onend);
33
+ }
34
+
35
+ write(data) {
36
+ this.parser.append(data).exec();
37
+
38
+ // Emit text events for any text nodes
39
+ const currentNode = this.parser.stack.at(-1);
40
+ if (currentNode && currentNode.text) {
41
+ const text = currentNode.text.trim();
42
+ if (text) {
43
+ this.emitter.emit('text', text);
44
+ }
45
+ // Clear the text to avoid emitting it multiple times
46
+ currentNode.text = '';
47
+ }
48
+ }
49
+
50
+ end() {
51
+ // Process any remaining data
52
+ this.parser.exec();
53
+
54
+ // Emit end event
55
+ this.emitter.emit('end');
56
+ }
56
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/reflib",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "description": "Reference / Citation reference library utilities",
5
5
  "scripts": {
6
6
  "lint": "eslint lib modules shared test",
@@ -53,6 +53,7 @@
53
53
  "vite-plugin-replace": "^0.1.1"
54
54
  },
55
55
  "dependencies": {
56
+ "@iebh/cacx": "^1.0.0",
56
57
  "htmlparser2": "^9.0.0",
57
58
  "JSONStream": "^1.3.5",
58
59
  "mitt": "^3.0.1"