@idomatic/parser-html 1.0.32 → 1.0.33

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 +39 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,11 +8,12 @@ export default async function parseHTML(filePath, config) {
8
8
  let content = fs.readFileSync(filePath, "utf-8");
9
9
 
10
10
  if (ext === ".vue") {
11
- // Process Vue SFCs: update the <template> block.
11
+ // Try to match a <template> block first.
12
12
  const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
13
- const match = content.match(templateRegex);
13
+ let match = content.match(templateRegex);
14
14
 
15
15
  if (match) {
16
+ // Process the content inside <template>
16
17
  const originalTemplate = match[1];
17
18
  const $ = cheerio.load(originalTemplate, { xmlMode: false });
18
19
 
@@ -25,19 +26,50 @@ export default async function parseHTML(filePath, config) {
25
26
  });
26
27
 
27
28
  const newTemplate = $.html();
29
+ // Replace the original <template> block with the updated version.
28
30
  content = content.replace(
29
31
  templateRegex,
30
32
  `<template>${newTemplate}</template>`
31
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;
32
67
  }
33
68
  } else if (ext === ".html" || ext === ".ng.html") {
34
- // Check if the content is a full HTML document or just a fragment.
69
+ // For HTML files, check if it's a full document or a fragment.
35
70
  const isFullDocument = content.trim().toLowerCase().startsWith("<html");
36
-
37
71
  if (isFullDocument) {
38
- // Process as a full document.
39
72
  const $ = cheerio.load(content, { xmlMode: false });
40
-
41
73
  $("*").each((_, el) => {
42
74
  const tag = el.tagName || el.name;
43
75
  if (config.excludeTags.includes(tag)) return;
@@ -45,15 +77,12 @@ export default async function parseHTML(filePath, config) {
45
77
  $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
46
78
  }
47
79
  });
48
-
49
80
  content = $.html();
50
81
  } else {
51
- // Process as an HTML fragment.
52
- // Wrap in a temporary container.
82
+ // Treat as an HTML fragment.
53
83
  const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
54
84
  xmlMode: false,
55
85
  });
56
-
57
86
  $("#fragment")
58
87
  .find("*")
59
88
  .each((_, el) => {
@@ -63,13 +92,8 @@ export default async function parseHTML(filePath, config) {
63
92
  $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
64
93
  }
65
94
  });
66
-
67
- // Extract only the inner HTML of the temporary container.
68
95
  content = $("#fragment").html();
69
96
  }
70
- } else {
71
- console.log(`Skipping unsupported file: ${filePath}`);
72
- return;
73
97
  }
74
98
 
75
99
  fs.writeFileSync(filePath, content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",