@idomatic/parser-html 1.0.32 → 1.0.34

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