@thi.ng/hiccup-html-parse 0.3.40 → 0.3.42
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/CHANGELOG.md +1 -1
- package/index.js +13 -26
- package/package.json +11 -12
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -34,8 +34,7 @@ const parseRaw = (src, opts) => {
|
|
|
34
34
|
return { result: lang.rules.main(ctx), ctx };
|
|
35
35
|
};
|
|
36
36
|
const parseHtml = (src, opts) => {
|
|
37
|
-
if (!src)
|
|
38
|
-
return { type: "success", result: [] };
|
|
37
|
+
if (!src) return { type: "success", result: [] };
|
|
39
38
|
opts = {
|
|
40
39
|
debug: false,
|
|
41
40
|
collapse: true,
|
|
@@ -77,28 +76,24 @@ const transformScope = defmulti(
|
|
|
77
76
|
},
|
|
78
77
|
// root node of the parse tree
|
|
79
78
|
root: ({ children }, opts, acc) => {
|
|
80
|
-
if (!children)
|
|
81
|
-
return;
|
|
79
|
+
if (!children) return;
|
|
82
80
|
children = children[0].children;
|
|
83
81
|
if (opts.doctype && children?.[0]) {
|
|
84
82
|
acc.push(["!DOCTYPE", children[0].result]);
|
|
85
83
|
}
|
|
86
|
-
for (let x of children[1].children)
|
|
87
|
-
transformScope(x, opts, acc);
|
|
84
|
+
for (let x of children[1].children) transformScope(x, opts, acc);
|
|
88
85
|
},
|
|
89
86
|
node: ({ children }, opts, acc) => {
|
|
90
87
|
transformScope(children[0], opts, acc);
|
|
91
88
|
},
|
|
92
89
|
comment: ({ result }, opts, acc) => {
|
|
93
|
-
if (opts.comments)
|
|
94
|
-
acc.push(["__COMMENT__", result.trim()]);
|
|
90
|
+
if (opts.comments) acc.push(["__COMMENT__", result.trim()]);
|
|
95
91
|
},
|
|
96
92
|
// element node transformer, collects & filters attributes/children
|
|
97
93
|
// adds resulting hiccup element to accumulator array
|
|
98
94
|
el: ({ children }, opts, acc) => {
|
|
99
95
|
const [name, { children: $attribs }, body] = children;
|
|
100
|
-
if (opts.ignoreElements?.includes(name.result))
|
|
101
|
-
return;
|
|
96
|
+
if (opts.ignoreElements?.includes(name.result)) return;
|
|
102
97
|
const attribs = {};
|
|
103
98
|
const el = [name.result, attribs];
|
|
104
99
|
if ($attribs) {
|
|
@@ -106,12 +101,10 @@ const transformScope = defmulti(
|
|
|
106
101
|
const name2 = a.children[0].result;
|
|
107
102
|
if (opts.dataAttribs === false && name2.startsWith("data-"))
|
|
108
103
|
continue;
|
|
109
|
-
if (opts.ignoreAttribs?.includes(name2))
|
|
110
|
-
continue;
|
|
104
|
+
if (opts.ignoreAttribs?.includes(name2)) continue;
|
|
111
105
|
if (a.children[1].children) {
|
|
112
106
|
const val = a.children[1].children[0].result;
|
|
113
|
-
if (val != null)
|
|
114
|
-
attribs[name2] = unescapeEntities(val);
|
|
107
|
+
if (val != null) attribs[name2] = unescapeEntities(val);
|
|
115
108
|
} else {
|
|
116
109
|
attribs[name2] = true;
|
|
117
110
|
}
|
|
@@ -121,25 +114,19 @@ const transformScope = defmulti(
|
|
|
121
114
|
if (body.result) {
|
|
122
115
|
el.push(body.result.trim());
|
|
123
116
|
} else if (body.children) {
|
|
124
|
-
for (let x of body.children)
|
|
125
|
-
transformScope(x, opts, el);
|
|
117
|
+
for (let x of body.children) transformScope(x, opts, el);
|
|
126
118
|
}
|
|
127
119
|
}
|
|
128
120
|
const result = opts.tx ? opts.tx(el) : el;
|
|
129
|
-
if (result != null)
|
|
130
|
-
acc.push(result);
|
|
121
|
+
if (result != null) acc.push(result);
|
|
131
122
|
},
|
|
132
123
|
// plain text transform (by default only resolves HTML entities)
|
|
133
124
|
body: ({ result }, opts, acc) => {
|
|
134
|
-
if (!opts.whitespace && /^\s+$/.test(result))
|
|
135
|
-
|
|
136
|
-
if (opts.
|
|
137
|
-
result = result.replace(/\s+/gm, " ");
|
|
138
|
-
if (opts.unescape)
|
|
139
|
-
result = unescapeEntities(result);
|
|
125
|
+
if (!opts.whitespace && /^\s+$/.test(result)) return;
|
|
126
|
+
if (opts.collapse) result = result.replace(/\s+/gm, " ");
|
|
127
|
+
if (opts.unescape) result = unescapeEntities(result);
|
|
140
128
|
result = opts.txBody ? opts.txBody(result) : result;
|
|
141
|
-
if (result != null)
|
|
142
|
-
acc.push(result);
|
|
129
|
+
if (result != null) acc.push(result);
|
|
143
130
|
}
|
|
144
131
|
}
|
|
145
132
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/hiccup-html-parse",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.42",
|
|
4
4
|
"description": "Well-formed HTML parsing and customizable transformation to nested JS arrays in @thi.ng/hiccup format",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
28
|
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
29
|
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
30
|
-
"clean": "
|
|
30
|
+
"clean": "bun ../../tools/src/clean-package.ts",
|
|
31
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
32
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
33
33
|
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
|
|
@@ -36,17 +36,16 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.11.
|
|
40
|
-
"@thi.ng/defmulti": "^3.0.
|
|
41
|
-
"@thi.ng/parse": "^2.4.
|
|
42
|
-
"@thi.ng/strings": "^3.7.
|
|
39
|
+
"@thi.ng/api": "^8.11.2",
|
|
40
|
+
"@thi.ng/defmulti": "^3.0.39",
|
|
41
|
+
"@thi.ng/parse": "^2.4.42",
|
|
42
|
+
"@thi.ng/strings": "^3.7.33"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@microsoft/api-extractor": "^7.43.
|
|
46
|
-
"esbuild": "^0.
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"typescript": "^5.4.3"
|
|
45
|
+
"@microsoft/api-extractor": "^7.43.2",
|
|
46
|
+
"esbuild": "^0.21.1",
|
|
47
|
+
"typedoc": "^0.25.13",
|
|
48
|
+
"typescript": "^5.4.5"
|
|
50
49
|
},
|
|
51
50
|
"keywords": [
|
|
52
51
|
"ast",
|
|
@@ -86,5 +85,5 @@
|
|
|
86
85
|
"status": "alpha",
|
|
87
86
|
"year": 2023
|
|
88
87
|
},
|
|
89
|
-
"gitHead": "
|
|
88
|
+
"gitHead": "df34b4a9e650cc7323575356de207d78933bdcf3\n"
|
|
90
89
|
}
|