@idomatic/parser-html 1.0.35 → 1.0.37
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 +33 -74
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,96 +5,55 @@ import { randomUUID } from "crypto";
|
|
|
5
5
|
|
|
6
6
|
export default async function parseHTML(filePath, config) {
|
|
7
7
|
const ext = path.extname(filePath).toLowerCase();
|
|
8
|
-
|
|
8
|
+
const originalContent = fs.readFileSync(filePath, "utf-8");
|
|
9
|
+
let updatedContent = originalContent; // fallback if nothing changes
|
|
10
|
+
|
|
11
|
+
const tagsToExclude = config.excludeTags || [];
|
|
12
|
+
const addIds = ($) => {
|
|
13
|
+
$("*").each((_, el) => {
|
|
14
|
+
const tag = el.name;
|
|
15
|
+
if (tagsToExclude.includes(tag)) return;
|
|
16
|
+
if (!$(el).attr(config.attributeName)) {
|
|
17
|
+
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
};
|
|
9
21
|
|
|
10
22
|
if (ext === ".vue") {
|
|
11
|
-
// Try to match a <template> block first.
|
|
12
23
|
const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
|
|
13
|
-
|
|
24
|
+
const match = originalContent.match(templateRegex);
|
|
14
25
|
|
|
15
26
|
if (match) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const $ = cheerio.load(originalTemplate, { xmlMode: false });
|
|
19
|
-
|
|
20
|
-
$("*").each((_, el) => {
|
|
21
|
-
const tag = el.tagName || el.name;
|
|
22
|
-
if (config.excludeTags.includes(tag)) return;
|
|
23
|
-
if (!$(el).attr(config.attributeName)) {
|
|
24
|
-
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
27
|
+
const $ = cheerio.load(match[1], { xmlMode: false });
|
|
28
|
+
addIds($);
|
|
28
29
|
const newTemplate = $.html();
|
|
29
|
-
|
|
30
|
-
content = content.replace(
|
|
30
|
+
updatedContent = originalContent.replace(
|
|
31
31
|
templateRegex,
|
|
32
32
|
`<template>${newTemplate}</template>`
|
|
33
33
|
);
|
|
34
|
-
} else {
|
|
35
|
-
// No <template> block found.
|
|
36
|
-
// Assume that the template is the content before the first <script> tag.
|
|
37
|
-
const scriptRegex = /<script[\s\S]*$/i;
|
|
38
|
-
match = content.match(scriptRegex);
|
|
39
|
-
let fragment, rest;
|
|
40
|
-
if (match) {
|
|
41
|
-
const index = match.index;
|
|
42
|
-
fragment = content.substring(0, index);
|
|
43
|
-
rest = content.substring(index);
|
|
44
|
-
} else {
|
|
45
|
-
// If there's no <script> block, treat the entire file as a fragment.
|
|
46
|
-
fragment = content;
|
|
47
|
-
rest = "";
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Wrap the fragment in a temporary container so we don't lose the structure.
|
|
51
|
-
const $ = cheerio.load(`<div id="fragment">${fragment}</div>`, {
|
|
52
|
-
xmlMode: false,
|
|
53
|
-
});
|
|
54
|
-
$("#fragment")
|
|
55
|
-
.find("*")
|
|
56
|
-
.each((_, el) => {
|
|
57
|
-
const tag = el.tagName || el.name;
|
|
58
|
-
if (config.excludeTags.includes(tag)) return;
|
|
59
|
-
if (!$(el).attr(config.attributeName)) {
|
|
60
|
-
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const newFragment = $("#fragment").html();
|
|
65
|
-
// Reassemble the file: updated fragment + the rest (e.g., <script> block).
|
|
66
|
-
content = newFragment + rest;
|
|
67
34
|
}
|
|
68
35
|
} else if (ext === ".html" || ext === ".ng.html") {
|
|
69
|
-
|
|
70
|
-
|
|
36
|
+
const isFullDocument = originalContent
|
|
37
|
+
.trim()
|
|
38
|
+
.toLowerCase()
|
|
39
|
+
.startsWith("<html");
|
|
71
40
|
if (isFullDocument) {
|
|
72
|
-
const $ = cheerio.load(
|
|
73
|
-
$
|
|
74
|
-
|
|
75
|
-
if (config.excludeTags.includes(tag)) return;
|
|
76
|
-
if (!$(el).attr(config.attributeName)) {
|
|
77
|
-
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
content = $.html();
|
|
41
|
+
const $ = cheerio.load(originalContent, { xmlMode: false });
|
|
42
|
+
addIds($);
|
|
43
|
+
updatedContent = $.html();
|
|
81
44
|
} else {
|
|
82
|
-
|
|
83
|
-
const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
|
|
45
|
+
const $ = cheerio.load(`<div id="fragment">${originalContent}</div>`, {
|
|
84
46
|
xmlMode: false,
|
|
85
47
|
});
|
|
86
|
-
$
|
|
87
|
-
|
|
88
|
-
.each((_, el) => {
|
|
89
|
-
const tag = el.tagName || el.name;
|
|
90
|
-
if (config.excludeTags.includes(tag)) return;
|
|
91
|
-
if (!$(el).attr(config.attributeName)) {
|
|
92
|
-
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
content = $("#fragment").html();
|
|
48
|
+
addIds($);
|
|
49
|
+
updatedContent = $("#fragment").html();
|
|
96
50
|
}
|
|
97
51
|
}
|
|
98
52
|
|
|
99
|
-
|
|
53
|
+
if (updatedContent !== originalContent) {
|
|
54
|
+
fs.writeFileSync(filePath, updatedContent);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
100
59
|
}
|