@idomatic/parser-html 1.0.31 → 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 +65 -17
  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,27 +26,74 @@ 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
  );
32
- }
33
- } else if (ext === ".html" || ext === ".ng.html") {
34
- // For .html files (including Angular's .ng.html), assume the entire file is HTML.
35
- const $ = cheerio.load(content, { xmlMode: false });
36
-
37
- $("*").each((_, el) => {
38
- const tag = el.tagName || el.name;
39
- if (config.excludeTags.includes(tag)) return;
40
- if (!$(el).attr(config.attributeName)) {
41
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
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 = "";
42
48
  }
43
- });
44
49
 
45
- content = $.html();
46
- } else {
47
- console.log(`Skipping unsupported file: ${filePath}`);
48
- return;
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
+ }
68
+ } 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");
71
+ 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();
81
+ } else {
82
+ // Treat as an HTML fragment.
83
+ const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
84
+ xmlMode: false,
85
+ });
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();
96
+ }
49
97
  }
50
98
 
51
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.31",
3
+ "version": "1.0.33",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",