@idomatic/parser-html 1.0.38 → 1.0.41
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/consts/index.js +1 -0
- package/helpers/index.js +22 -0
- package/index.js +10 -40
- package/package.json +1 -1
- package/utils/index.js +41 -0
package/consts/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
|
package/helpers/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Adds unique attributes to all elements that don't have the specified attribute.
|
|
5
|
+
* It skips elements whose tag names appear in the config's excludeTags array.
|
|
6
|
+
*
|
|
7
|
+
* @param {CheerioStatic} $ - The Cheerio instance containing the parsed HTML.
|
|
8
|
+
* @param {object} config - The configuration object.
|
|
9
|
+
* @param {string} config.attributeName - The attribute name to add (for example, "data-id").
|
|
10
|
+
* @param {string} config.prefix - The prefix for generated IDs.
|
|
11
|
+
* @param {string[]} config.excludeTags - An array of tag names to exclude.
|
|
12
|
+
*/
|
|
13
|
+
export function addIds($, config) {
|
|
14
|
+
const { attributeName, prefix, excludeTags } = config;
|
|
15
|
+
$("*").each((_, el) => {
|
|
16
|
+
const tag = el.name;
|
|
17
|
+
if (excludeTags && excludeTags.includes(tag)) return;
|
|
18
|
+
if (!$(el).attr(attributeName)) {
|
|
19
|
+
$(el).attr(attributeName, `${prefix}${randomUUID()}`);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
package/index.js
CHANGED
|
@@ -1,61 +1,32 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
3
|
+
import { templateRegex } from "./consts/index.js";
|
|
4
|
+
import { processHtmlContent, processVueTemplate } from "./utils/index.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @param {
|
|
10
|
-
* @
|
|
7
|
+
* Parses an HTML/Vue file and updates its content with unique IDs.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} filePath - The path to the file.
|
|
10
|
+
* @param {object} config - The configuration object.
|
|
11
|
+
* @param {boolean} dry - If true, does not write changes back to disk.
|
|
12
|
+
* @returns {boolean} - Returns true if changes were made.
|
|
11
13
|
*/
|
|
12
14
|
export default async function parseHTML(filePath, config, dry = false) {
|
|
13
15
|
const ext = path.extname(filePath).toLowerCase();
|
|
14
16
|
const originalContent = fs.readFileSync(filePath, "utf-8");
|
|
15
17
|
let updatedContent = originalContent;
|
|
16
18
|
|
|
17
|
-
const tagsToExclude = config.excludeTags || [];
|
|
18
|
-
|
|
19
|
-
const addIds = ($) => {
|
|
20
|
-
$("*").each((_, el) => {
|
|
21
|
-
const tag = el.name;
|
|
22
|
-
if (tagsToExclude.includes(tag)) return;
|
|
23
|
-
if (!$(el).attr(config.attributeName)) {
|
|
24
|
-
$(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
19
|
if (ext === ".vue") {
|
|
30
|
-
const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
|
|
31
20
|
const match = originalContent.match(templateRegex);
|
|
32
|
-
|
|
33
21
|
if (match) {
|
|
34
|
-
const
|
|
35
|
-
addIds($);
|
|
36
|
-
const newTemplate = $.html();
|
|
22
|
+
const newTemplate = processVueTemplate(match[1], config);
|
|
37
23
|
updatedContent = originalContent.replace(
|
|
38
24
|
templateRegex,
|
|
39
25
|
`<template>${newTemplate}</template>`
|
|
40
26
|
);
|
|
41
27
|
}
|
|
42
28
|
} else if (ext === ".html" || ext === ".ng.html") {
|
|
43
|
-
|
|
44
|
-
.trim()
|
|
45
|
-
.toLowerCase()
|
|
46
|
-
.startsWith("<html");
|
|
47
|
-
|
|
48
|
-
if (isFullDocument) {
|
|
49
|
-
const $ = cheerio.load(originalContent, { xmlMode: false });
|
|
50
|
-
addIds($);
|
|
51
|
-
updatedContent = $.html();
|
|
52
|
-
} else {
|
|
53
|
-
const $ = cheerio.load(`<div id="fragment">${originalContent}</div>`, {
|
|
54
|
-
xmlMode: false,
|
|
55
|
-
});
|
|
56
|
-
addIds($);
|
|
57
|
-
updatedContent = $("#fragment").html();
|
|
58
|
-
}
|
|
29
|
+
updatedContent = processHtmlContent(originalContent, config);
|
|
59
30
|
}
|
|
60
31
|
|
|
61
32
|
if (updatedContent !== originalContent) {
|
|
@@ -64,6 +35,5 @@ export default async function parseHTML(filePath, config, dry = false) {
|
|
|
64
35
|
}
|
|
65
36
|
return true;
|
|
66
37
|
}
|
|
67
|
-
|
|
68
38
|
return false;
|
|
69
39
|
}
|
package/package.json
CHANGED
package/utils/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
import { addIds } from "../helpers/index.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Processes a Vue template content.
|
|
6
|
+
* @param {string} templateContent - The content inside the Vue <template> tag.
|
|
7
|
+
* @param {object} config - The configuration object.
|
|
8
|
+
* @returns {string} - The updated template content.
|
|
9
|
+
*/
|
|
10
|
+
export function processVueTemplate(templateContent, config) {
|
|
11
|
+
const $ = cheerio.load(templateContent, { xmlMode: false });
|
|
12
|
+
// For Vue files, addIds might not need the config if handled differently;
|
|
13
|
+
// adjust as needed.
|
|
14
|
+
addIds($, config);
|
|
15
|
+
return $.html();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Processes HTML content (full document or fragment) by adding IDs.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} htmlContent - The original HTML content.
|
|
22
|
+
* @param {object} config - The configuration object.
|
|
23
|
+
* @returns {string} - The updated HTML content.
|
|
24
|
+
*/
|
|
25
|
+
export function processHtmlContent(htmlContent, config) {
|
|
26
|
+
// Determine if we have a full document
|
|
27
|
+
const isFullDocument = htmlContent.trim().toLowerCase().startsWith("<html");
|
|
28
|
+
|
|
29
|
+
if (isFullDocument) {
|
|
30
|
+
const $ = cheerio.load(htmlContent, { xmlMode: false });
|
|
31
|
+
addIds($, config);
|
|
32
|
+
return $.html();
|
|
33
|
+
} else {
|
|
34
|
+
// Wrap the fragment to ensure Cheerio parses it properly.
|
|
35
|
+
const $ = cheerio.load(`<div id="fragment">${htmlContent}</div>`, {
|
|
36
|
+
xmlMode: false,
|
|
37
|
+
});
|
|
38
|
+
addIds($, config);
|
|
39
|
+
return $("#fragment").html();
|
|
40
|
+
}
|
|
41
|
+
}
|