@idomatic/parser-html 1.0.31 → 1.0.32
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/index.js +36 -12
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -31,18 +31,42 @@ export default async function parseHTML(filePath, config) {
|
|
|
31
31
|
);
|
|
32
32
|
}
|
|
33
33
|
} else if (ext === ".html" || ext === ".ng.html") {
|
|
34
|
-
//
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
// Check if the content is a full HTML document or just a fragment.
|
|
35
|
+
const isFullDocument = content.trim().toLowerCase().startsWith("<html");
|
|
36
|
+
|
|
37
|
+
if (isFullDocument) {
|
|
38
|
+
// Process as a full document.
|
|
39
|
+
const $ = cheerio.load(content, { xmlMode: false });
|
|
40
|
+
|
|
41
|
+
$("*").each((_, el) => {
|
|
42
|
+
const tag = el.tagName || el.name;
|
|
43
|
+
if (config.excludeTags.includes(tag)) return;
|
|
44
|
+
if (!$(el).attr(config.attributeName)) {
|
|
45
|
+
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
content = $.html();
|
|
50
|
+
} else {
|
|
51
|
+
// Process as an HTML fragment.
|
|
52
|
+
// Wrap in a temporary container.
|
|
53
|
+
const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
|
|
54
|
+
xmlMode: false,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
$("#fragment")
|
|
58
|
+
.find("*")
|
|
59
|
+
.each((_, el) => {
|
|
60
|
+
const tag = el.tagName || el.name;
|
|
61
|
+
if (config.excludeTags.includes(tag)) return;
|
|
62
|
+
if (!$(el).attr(config.attributeName)) {
|
|
63
|
+
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Extract only the inner HTML of the temporary container.
|
|
68
|
+
content = $("#fragment").html();
|
|
69
|
+
}
|
|
46
70
|
} else {
|
|
47
71
|
console.log(`Skipping unsupported file: ${filePath}`);
|
|
48
72
|
return;
|