@idomatic/parser-html 1.0.33 → 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 +28 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,15 +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
- // Try to match a <template> block first.
11
+ // Try to find a <template> block.
12
12
  const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
13
13
  let match = content.match(templateRegex);
14
-
15
14
  if (match) {
16
- // Process the content inside <template>
17
15
  const originalTemplate = match[1];
18
16
  const $ = cheerio.load(originalTemplate, { xmlMode: false });
19
-
20
17
  $("*").each((_, el) => {
21
18
  const tag = el.tagName || el.name;
22
19
  if (config.excludeTags.includes(tag)) return;
@@ -24,16 +21,14 @@ export default async function parseHTML(filePath, config) {
24
21
  $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
25
22
  }
26
23
  });
27
-
28
24
  const newTemplate = $.html();
29
- // Replace the original <template> block with the updated version.
30
25
  content = content.replace(
31
26
  templateRegex,
32
27
  `<template>${newTemplate}</template>`
33
28
  );
34
29
  } else {
35
- // No <template> block found.
36
- // Assume that the template is the content before the first <script> tag.
30
+ // No explicit <template> block found.
31
+ // Assume the template is all content before the first <script> tag.
37
32
  const scriptRegex = /<script[\s\S]*$/i;
38
33
  match = content.match(scriptRegex);
39
34
  let fragment, rest;
@@ -42,12 +37,10 @@ export default async function parseHTML(filePath, config) {
42
37
  fragment = content.substring(0, index);
43
38
  rest = content.substring(index);
44
39
  } else {
45
- // If there's no <script> block, treat the entire file as a fragment.
46
40
  fragment = content;
47
41
  rest = "";
48
42
  }
49
-
50
- // Wrap the fragment in a temporary container so we don't lose the structure.
43
+ // Wrap the fragment in a temporary container.
51
44
  const $ = cheerio.load(`<div id="fragment">${fragment}</div>`, {
52
45
  xmlMode: false,
53
46
  });
@@ -60,13 +53,31 @@ export default async function parseHTML(filePath, config) {
60
53
  $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
61
54
  }
62
55
  });
63
-
64
56
  const newFragment = $("#fragment").html();
65
- // Reassemble the file: updated fragment + the rest (e.g., <script> block).
57
+ // Reassemble file: updated fragment + rest of content (e.g., <script> block)
66
58
  content = newFragment + rest;
67
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
+ );
68
78
  } else if (ext === ".html" || ext === ".ng.html") {
69
- // For HTML files, check if it's a full document or a fragment.
79
+ // Process external HTML files.
80
+ // Check if the content is a full document or just an HTML fragment.
70
81
  const isFullDocument = content.trim().toLowerCase().startsWith("<html");
71
82
  if (isFullDocument) {
72
83
  const $ = cheerio.load(content, { xmlMode: false });
@@ -79,7 +90,6 @@ export default async function parseHTML(filePath, config) {
79
90
  });
80
91
  content = $.html();
81
92
  } else {
82
- // Treat as an HTML fragment.
83
93
  const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
84
94
  xmlMode: false,
85
95
  });
@@ -94,6 +104,9 @@ export default async function parseHTML(filePath, config) {
94
104
  });
95
105
  content = $("#fragment").html();
96
106
  }
107
+ } else {
108
+ console.log(`Skipping unsupported file: ${filePath}`);
109
+ return;
97
110
  }
98
111
 
99
112
  fs.writeFileSync(filePath, content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",