@idomatic/parser-html 1.0.36 → 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 +20 -36
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,71 +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
|
-
|
|
9
|
-
let
|
|
8
|
+
const originalContent = fs.readFileSync(filePath, "utf-8");
|
|
9
|
+
let updatedContent = originalContent; // fallback if nothing changes
|
|
10
10
|
|
|
11
|
+
const tagsToExclude = config.excludeTags || [];
|
|
11
12
|
const addIds = ($) => {
|
|
12
13
|
$("*").each((_, el) => {
|
|
13
|
-
const tag = el.
|
|
14
|
-
if (
|
|
14
|
+
const tag = el.name;
|
|
15
|
+
if (tagsToExclude.includes(tag)) return;
|
|
15
16
|
if (!$(el).attr(config.attributeName)) {
|
|
16
17
|
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
17
|
-
changed = true;
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
if (ext === ".vue") {
|
|
23
23
|
const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
|
|
24
|
-
|
|
24
|
+
const match = originalContent.match(templateRegex);
|
|
25
25
|
|
|
26
26
|
if (match) {
|
|
27
|
-
const
|
|
28
|
-
const $ = cheerio.load(originalTemplate, { xmlMode: false });
|
|
27
|
+
const $ = cheerio.load(match[1], { xmlMode: false });
|
|
29
28
|
addIds($);
|
|
30
29
|
const newTemplate = $.html();
|
|
31
|
-
|
|
30
|
+
updatedContent = originalContent.replace(
|
|
32
31
|
templateRegex,
|
|
33
32
|
`<template>${newTemplate}</template>`
|
|
34
33
|
);
|
|
35
|
-
} else {
|
|
36
|
-
const scriptRegex = /<script[\s\S]*$/i;
|
|
37
|
-
match = content.match(scriptRegex);
|
|
38
|
-
let fragment, rest;
|
|
39
|
-
if (match) {
|
|
40
|
-
const index = match.index;
|
|
41
|
-
fragment = content.substring(0, index);
|
|
42
|
-
rest = content.substring(index);
|
|
43
|
-
} else {
|
|
44
|
-
fragment = content;
|
|
45
|
-
rest = "";
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const $ = cheerio.load(`<div id="fragment">${fragment}</div>`, {
|
|
49
|
-
xmlMode: false,
|
|
50
|
-
});
|
|
51
|
-
addIds($);
|
|
52
|
-
const newFragment = $("#fragment").html();
|
|
53
|
-
content = newFragment + rest;
|
|
54
34
|
}
|
|
55
35
|
} else if (ext === ".html" || ext === ".ng.html") {
|
|
56
|
-
const isFullDocument =
|
|
36
|
+
const isFullDocument = originalContent
|
|
37
|
+
.trim()
|
|
38
|
+
.toLowerCase()
|
|
39
|
+
.startsWith("<html");
|
|
57
40
|
if (isFullDocument) {
|
|
58
|
-
const $ = cheerio.load(
|
|
41
|
+
const $ = cheerio.load(originalContent, { xmlMode: false });
|
|
59
42
|
addIds($);
|
|
60
|
-
|
|
43
|
+
updatedContent = $.html();
|
|
61
44
|
} else {
|
|
62
|
-
const $ = cheerio.load(`<div id="fragment">${
|
|
45
|
+
const $ = cheerio.load(`<div id="fragment">${originalContent}</div>`, {
|
|
63
46
|
xmlMode: false,
|
|
64
47
|
});
|
|
65
48
|
addIds($);
|
|
66
|
-
|
|
49
|
+
updatedContent = $("#fragment").html();
|
|
67
50
|
}
|
|
68
51
|
}
|
|
69
52
|
|
|
70
|
-
if (
|
|
71
|
-
fs.writeFileSync(filePath,
|
|
53
|
+
if (updatedContent !== originalContent) {
|
|
54
|
+
fs.writeFileSync(filePath, updatedContent);
|
|
55
|
+
return true;
|
|
72
56
|
}
|
|
73
57
|
|
|
74
|
-
return
|
|
58
|
+
return false;
|
|
75
59
|
}
|