@idomatic/parser-html 1.0.3 → 1.0.4

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.
@@ -0,0 +1 @@
1
+ export const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Adds unique attributes to all elements that don't have the specified attribute.
3
+ * It skips elements whose tag names appear in the config's excludeTags array.
4
+ *
5
+ * @param {CheerioStatic} $ - The Cheerio instance containing the parsed HTML.
6
+ * @param {object} config - The configuration object.
7
+ * @param {string} config.attributeName - The attribute name to add (for example, "data-id").
8
+ * @param {string} config.prefix - The prefix for generated IDs.
9
+ * @param {string[]} config.excludeTags - An array of tag names to exclude.
10
+ */
11
+ export function addIds($, config) {
12
+ const { attributeName, prefix, excludeTags } = config;
13
+ $("*").each((_, el) => {
14
+ const tag = el.name;
15
+ if (excludeTags && excludeTags.includes(tag)) return;
16
+ if (!$(el).attr(attributeName)) {
17
+ $(el).attr(attributeName, `${prefix}${randomUUID()}`);
18
+ }
19
+ });
20
+ }
package/index.js CHANGED
@@ -1,69 +1,39 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import * as cheerio from "cheerio";
4
- import { randomUUID } from "crypto";
5
-
6
- export default async function parseHTML(filePath, config) {
3
+ import { processVueTemplate, processHtmlContent } from "./utils/index.js";
4
+ import { templateRegex } from "./consts/index.js";
5
+
6
+ /**
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.
13
+ */
14
+ export default async function parseHTML(filePath, config, dry = false) {
7
15
  const ext = path.extname(filePath).toLowerCase();
8
- let content = fs.readFileSync(filePath, "utf-8");
16
+ const originalContent = fs.readFileSync(filePath, "utf-8");
17
+ let updatedContent = originalContent;
9
18
 
10
19
  if (ext === ".vue") {
11
- // Process Vue single file components: update the <template> block.
12
- const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
13
- const match = content.match(templateRegex);
14
-
20
+ const match = originalContent.match(templateRegex);
15
21
  if (match) {
16
- const originalTemplate = match[1];
17
- const $ = cheerio.load(originalTemplate, { xmlMode: false });
18
-
19
- $("*").each((_, el) => {
20
- const tag = el.tagName || el.name;
21
- if (config.excludeTags.includes(tag)) return;
22
- if (!$(el).attr(config.attributeName)) {
23
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
24
- }
25
- });
26
-
27
- const newTemplate = $.html();
28
- // Replace the original <template> block with the updated version.
29
- content = content.replace(
22
+ const newTemplate = processVueTemplate(match[1], config);
23
+ updatedContent = originalContent.replace(
30
24
  templateRegex,
31
25
  `<template>${newTemplate}</template>`
32
26
  );
33
27
  }
34
- } else if (ext === ".ts") {
35
- // Process TypeScript files with inline templates.
36
- // This regex matches a template property with a backtick string literal.
37
- const templateRegex = /template\s*:\s*`([\s\S]*?)`/gm;
38
- content = content.replace(templateRegex, (match, templateContent) => {
39
- const $ = cheerio.load(templateContent, { 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
- const newTemplate = $.html();
50
- // Replace the original inline template content.
51
- return match.replace(templateContent, newTemplate);
52
- });
53
- } else if (ext === ".html") {
54
- // For .html files, assume the entire file is HTML content.
55
- const $ = cheerio.load(content, { xmlMode: false });
56
-
57
- $("*").each((_, el) => {
58
- const tag = el.tagName || el.name;
59
- if (config.excludeTags.includes(tag)) return;
60
- if (!$(el).attr(config.attributeName)) {
61
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
62
- }
63
- });
64
-
65
- content = $.html();
28
+ } else if (ext === ".html" || ext === ".ng.html") {
29
+ updatedContent = processHtmlContent(originalContent, config);
66
30
  }
67
31
 
68
- fs.writeFileSync(filePath, content);
32
+ if (updatedContent !== originalContent) {
33
+ if (!dry) {
34
+ fs.writeFileSync(filePath, updatedContent);
35
+ }
36
+ return true;
37
+ }
38
+ return false;
69
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",
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
+ }