@idomatic/parser-html 1.0.35 → 1.0.36

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.
Files changed (2) hide show
  1. package/index.js +21 -46
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,34 +6,33 @@ import { randomUUID } from "crypto";
6
6
  export default async function parseHTML(filePath, config) {
7
7
  const ext = path.extname(filePath).toLowerCase();
8
8
  let content = fs.readFileSync(filePath, "utf-8");
9
+ let changed = false;
10
+
11
+ const addIds = ($) => {
12
+ $("*").each((_, el) => {
13
+ const tag = el.tagName || el.name;
14
+ if (config.excludeTags.includes(tag)) return;
15
+ if (!$(el).attr(config.attributeName)) {
16
+ $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
17
+ changed = true;
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
  let match = content.match(templateRegex);
14
25
 
15
26
  if (match) {
16
- // Process the content inside <template>
17
27
  const originalTemplate = match[1];
18
28
  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
-
29
+ addIds($);
28
30
  const newTemplate = $.html();
29
- // Replace the original <template> block with the updated version.
30
31
  content = content.replace(
31
32
  templateRegex,
32
33
  `<template>${newTemplate}</template>`
33
34
  );
34
35
  } else {
35
- // No <template> block found.
36
- // Assume that the template is the content before the first <script> tag.
37
36
  const scriptRegex = /<script[\s\S]*$/i;
38
37
  match = content.match(scriptRegex);
39
38
  let fragment, rest;
@@ -42,59 +41,35 @@ export default async function parseHTML(filePath, config) {
42
41
  fragment = content.substring(0, index);
43
42
  rest = content.substring(index);
44
43
  } else {
45
- // If there's no <script> block, treat the entire file as a fragment.
46
44
  fragment = content;
47
45
  rest = "";
48
46
  }
49
47
 
50
- // Wrap the fragment in a temporary container so we don't lose the structure.
51
48
  const $ = cheerio.load(`<div id="fragment">${fragment}</div>`, {
52
49
  xmlMode: false,
53
50
  });
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
-
51
+ addIds($);
64
52
  const newFragment = $("#fragment").html();
65
- // Reassemble the file: updated fragment + the rest (e.g., <script> block).
66
53
  content = newFragment + rest;
67
54
  }
68
55
  } else if (ext === ".html" || ext === ".ng.html") {
69
- // For HTML files, check if it's a full document or a fragment.
70
56
  const isFullDocument = content.trim().toLowerCase().startsWith("<html");
71
57
  if (isFullDocument) {
72
58
  const $ = cheerio.load(content, { xmlMode: false });
73
- $("*").each((_, el) => {
74
- const tag = el.tagName || el.name;
75
- if (config.excludeTags.includes(tag)) return;
76
- if (!$(el).attr(config.attributeName)) {
77
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
78
- }
79
- });
59
+ addIds($);
80
60
  content = $.html();
81
61
  } else {
82
- // Treat as an HTML fragment.
83
62
  const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
84
63
  xmlMode: false,
85
64
  });
86
- $("#fragment")
87
- .find("*")
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
- });
65
+ addIds($);
95
66
  content = $("#fragment").html();
96
67
  }
97
68
  }
98
69
 
99
- fs.writeFileSync(filePath, content);
70
+ if (changed) {
71
+ fs.writeFileSync(filePath, content);
72
+ }
73
+
74
+ return changed;
100
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.35",
3
+ "version": "1.0.36",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",