@idomatic/parser-html 1.0.34 → 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 +23 -61
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,29 +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 find a <template> block.
12
23
  const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
13
24
  let match = content.match(templateRegex);
25
+
14
26
  if (match) {
15
27
  const originalTemplate = match[1];
16
28
  const $ = cheerio.load(originalTemplate, { xmlMode: false });
17
- $("*").each((_, el) => {
18
- const tag = el.tagName || el.name;
19
- if (config.excludeTags.includes(tag)) return;
20
- if (!$(el).attr(config.attributeName)) {
21
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
22
- }
23
- });
29
+ addIds($);
24
30
  const newTemplate = $.html();
25
31
  content = content.replace(
26
32
  templateRegex,
27
33
  `<template>${newTemplate}</template>`
28
34
  );
29
35
  } else {
30
- // No explicit <template> block found.
31
- // Assume the template is all content before the first <script> tag.
32
36
  const scriptRegex = /<script[\s\S]*$/i;
33
37
  match = content.match(scriptRegex);
34
38
  let fragment, rest;
@@ -40,74 +44,32 @@ export default async function parseHTML(filePath, config) {
40
44
  fragment = content;
41
45
  rest = "";
42
46
  }
43
- // Wrap the fragment in a temporary container.
47
+
44
48
  const $ = cheerio.load(`<div id="fragment">${fragment}</div>`, {
45
49
  xmlMode: false,
46
50
  });
47
- $("#fragment")
48
- .find("*")
49
- .each((_, el) => {
50
- const tag = el.tagName || el.name;
51
- if (config.excludeTags.includes(tag)) return;
52
- if (!$(el).attr(config.attributeName)) {
53
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
54
- }
55
- });
51
+ addIds($);
56
52
  const newFragment = $("#fragment").html();
57
- // Reassemble file: updated fragment + rest of content (e.g., <script> block)
58
53
  content = newFragment + rest;
59
54
  }
60
- } else if (ext === ".ts") {
61
- // Process inline templates in TypeScript files for Angular or Vue.
62
- const inlineTemplateRegex = /(template\s*:\s*)`([\s\S]*?)`/gm;
63
- content = content.replace(
64
- inlineTemplateRegex,
65
- (match, prefix, templateContent) => {
66
- const $ = cheerio.load(templateContent, { xmlMode: false });
67
- $("*").each((_, el) => {
68
- const tag = el.tagName || el.name;
69
- if (config.excludeTags.includes(tag)) return;
70
- if (!$(el).attr(config.attributeName)) {
71
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
72
- }
73
- });
74
- const newTemplate = $.html();
75
- return `${prefix}\`${newTemplate}\``;
76
- }
77
- );
78
55
  } else if (ext === ".html" || ext === ".ng.html") {
79
- // Process external HTML files.
80
- // Check if the content is a full document or just an HTML fragment.
81
56
  const isFullDocument = content.trim().toLowerCase().startsWith("<html");
82
57
  if (isFullDocument) {
83
58
  const $ = cheerio.load(content, { xmlMode: false });
84
- $("*").each((_, el) => {
85
- const tag = el.tagName || el.name;
86
- if (config.excludeTags.includes(tag)) return;
87
- if (!$(el).attr(config.attributeName)) {
88
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
89
- }
90
- });
59
+ addIds($);
91
60
  content = $.html();
92
61
  } else {
93
62
  const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
94
63
  xmlMode: false,
95
64
  });
96
- $("#fragment")
97
- .find("*")
98
- .each((_, el) => {
99
- const tag = el.tagName || el.name;
100
- if (config.excludeTags.includes(tag)) return;
101
- if (!$(el).attr(config.attributeName)) {
102
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
103
- }
104
- });
65
+ addIds($);
105
66
  content = $("#fragment").html();
106
67
  }
107
- } else {
108
- console.log(`Skipping unsupported file: ${filePath}`);
109
- return;
110
68
  }
111
69
 
112
- fs.writeFileSync(filePath, content);
70
+ if (changed) {
71
+ fs.writeFileSync(filePath, content);
72
+ }
73
+
74
+ return changed;
113
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",