@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.
Files changed (2) hide show
  1. package/index.js +33 -74
  2. 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
- let content = fs.readFileSync(filePath, "utf-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
- let match = content.match(templateRegex);
24
+ const match = originalContent.match(templateRegex);
14
25
 
15
26
  if (match) {
16
- // Process the content inside <template>
17
- const originalTemplate = match[1];
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
- // Replace the original <template> block with the updated version.
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
- // For HTML files, check if it's a full document or a fragment.
70
- const isFullDocument = content.trim().toLowerCase().startsWith("<html");
36
+ const isFullDocument = originalContent
37
+ .trim()
38
+ .toLowerCase()
39
+ .startsWith("<html");
71
40
  if (isFullDocument) {
72
- 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
- });
80
- content = $.html();
41
+ const $ = cheerio.load(originalContent, { xmlMode: false });
42
+ addIds($);
43
+ updatedContent = $.html();
81
44
  } else {
82
- // Treat as an HTML fragment.
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
- $("#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
- });
95
- content = $("#fragment").html();
48
+ addIds($);
49
+ updatedContent = $("#fragment").html();
96
50
  }
97
51
  }
98
52
 
99
- fs.writeFileSync(filePath, content);
53
+ if (updatedContent !== originalContent) {
54
+ fs.writeFileSync(filePath, updatedContent);
55
+ return true;
56
+ }
57
+
58
+ return false;
100
59
  }
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.37",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",