@jxsuite/parser 0.6.2 → 0.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/package.json +3 -2
- package/src/html-to-jx.js +84 -0
- package/src/transpile.js +4 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jxsuite/parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Jx markdown parser and external class integration",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"glob": "^13.0.6",
|
|
30
|
+
"hast-util-from-html": "^2.0.3",
|
|
30
31
|
"mdast-util-to-string": "^4.0.0",
|
|
31
32
|
"rehype-stringify": "^10.0.1",
|
|
32
33
|
"remark-directive": "^4.0.0",
|
|
@@ -40,6 +41,6 @@
|
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"@happy-dom/global-registrator": "^20.9.0",
|
|
43
|
-
"@jxsuite/runtime": "^0.
|
|
44
|
+
"@jxsuite/runtime": "^0.6.2"
|
|
44
45
|
}
|
|
45
46
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { fromHtml } from "hast-util-from-html";
|
|
2
|
+
import { whitespace } from "hast-util-whitespace";
|
|
3
|
+
import { find, html as htmlInfo } from "property-information";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Convert an HTML string into an array of Jx tree nodes.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} htmlString
|
|
9
|
+
* @returns {any[]}
|
|
10
|
+
*/
|
|
11
|
+
export function htmlToJx(htmlString) {
|
|
12
|
+
const hast = fromHtml(htmlString, { fragment: true });
|
|
13
|
+
return convertHastChildren(hast.children);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {any[]} children
|
|
18
|
+
* @returns {any[]}
|
|
19
|
+
*/
|
|
20
|
+
function convertHastChildren(children) {
|
|
21
|
+
/** @type {any[]} */
|
|
22
|
+
const result = [];
|
|
23
|
+
for (const child of children) {
|
|
24
|
+
const converted = convertHastNode(child);
|
|
25
|
+
if (converted != null) result.push(converted);
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {any} node
|
|
32
|
+
* @returns {any}
|
|
33
|
+
*/
|
|
34
|
+
function convertHastNode(node) {
|
|
35
|
+
if (node.type === "text") {
|
|
36
|
+
if (whitespace(node)) return null;
|
|
37
|
+
return node.value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (node.type === "element") {
|
|
41
|
+
/** @type {Record<string, any>} */
|
|
42
|
+
const el = { tagName: node.tagName };
|
|
43
|
+
|
|
44
|
+
if (node.properties && Object.keys(node.properties).length > 0) {
|
|
45
|
+
el.attributes = hastPropsToAttributes(node.properties);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const kids = node.children ? convertHastChildren(node.children) : [];
|
|
49
|
+
|
|
50
|
+
if (kids.length === 1 && typeof kids[0] === "string") {
|
|
51
|
+
el.textContent = kids[0];
|
|
52
|
+
} else if (kids.length > 0) {
|
|
53
|
+
el.children = kids;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return el;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {Record<string, any>} properties
|
|
64
|
+
* @returns {Record<string, string>}
|
|
65
|
+
*/
|
|
66
|
+
function hastPropsToAttributes(properties) {
|
|
67
|
+
/** @type {Record<string, string>} */
|
|
68
|
+
const attrs = {};
|
|
69
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
70
|
+
if (value === false || value === undefined || value === null) continue;
|
|
71
|
+
|
|
72
|
+
const info = find(htmlInfo, key);
|
|
73
|
+
const name = info.attribute;
|
|
74
|
+
|
|
75
|
+
if (value === true) {
|
|
76
|
+
attrs[name] = "";
|
|
77
|
+
} else if (Array.isArray(value)) {
|
|
78
|
+
attrs[name] = value.join(info.commaSeparated ? ", " : " ");
|
|
79
|
+
} else {
|
|
80
|
+
attrs[name] = String(value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return attrs;
|
|
84
|
+
}
|
package/src/transpile.js
CHANGED
|
@@ -17,6 +17,8 @@ import remarkFrontmatter from "remark-frontmatter";
|
|
|
17
17
|
import remarkParseFrontmatter from "remark-parse-frontmatter";
|
|
18
18
|
import remarkGfm from "remark-gfm";
|
|
19
19
|
import remarkDirective from "remark-directive";
|
|
20
|
+
import { htmlToJx } from "./html-to-jx.js";
|
|
21
|
+
export { htmlToJx };
|
|
20
22
|
|
|
21
23
|
// ─── Dot-path expansion ─────────────────────────────────────────────────────
|
|
22
24
|
|
|
@@ -333,7 +335,7 @@ export function mdastNodeToJx(node) {
|
|
|
333
335
|
}
|
|
334
336
|
|
|
335
337
|
if (node.type === "html") {
|
|
336
|
-
if (node.value) return
|
|
338
|
+
if (node.value) return htmlToJx(node.value);
|
|
337
339
|
return null;
|
|
338
340
|
}
|
|
339
341
|
|
|
@@ -542,7 +544,7 @@ function directiveToJx(node) {
|
|
|
542
544
|
*/
|
|
543
545
|
export function convertChildren(children) {
|
|
544
546
|
if (!children) return [];
|
|
545
|
-
return children.
|
|
547
|
+
return children.flatMap(mdastNodeToJx).filter((c) => c != null);
|
|
546
548
|
}
|
|
547
549
|
|
|
548
550
|
/**
|