@idomatic/parser-html 1.0.3 → 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 +30 -23
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -8,7 +8,7 @@ export default async function parseHTML(filePath, config) {
|
|
|
8
8
|
let content = fs.readFileSync(filePath, "utf-8");
|
|
9
9
|
|
|
10
10
|
if (ext === ".vue") {
|
|
11
|
-
// Process Vue
|
|
11
|
+
// Process Vue SFCs: update the <template> block.
|
|
12
12
|
const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
|
|
13
13
|
const match = content.match(templateRegex);
|
|
14
14
|
|
|
@@ -25,18 +25,18 @@ export default async function parseHTML(filePath, config) {
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
const newTemplate = $.html();
|
|
28
|
-
// Replace the original <template> block with the updated version.
|
|
29
28
|
content = content.replace(
|
|
30
29
|
templateRegex,
|
|
31
30
|
`<template>${newTemplate}</template>`
|
|
32
31
|
);
|
|
33
32
|
}
|
|
34
|
-
} else if (ext === ".
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
} else if (ext === ".html" || ext === ".ng.html") {
|
|
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
40
|
|
|
41
41
|
$("*").each((_, el) => {
|
|
42
42
|
const tag = el.tagName || el.name;
|
|
@@ -46,23 +46,30 @@ export default async function parseHTML(filePath, config) {
|
|
|
46
46
|
}
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
});
|
|
64
66
|
|
|
65
|
-
|
|
67
|
+
// Extract only the inner HTML of the temporary container.
|
|
68
|
+
content = $("#fragment").html();
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
console.log(`Skipping unsupported file: ${filePath}`);
|
|
72
|
+
return;
|
|
66
73
|
}
|
|
67
74
|
|
|
68
75
|
fs.writeFileSync(filePath, content);
|