@idomatic/parser-html 1.0.3 → 1.0.32

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 +30 -23
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,7 +8,7 @@ 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 single file components: update the <template> block.
11
+ // Process Vue SFCs: update the <template> block.
12
12
  const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
13
13
  const match = content.match(templateRegex);
14
14
 
@@ -25,18 +25,18 @@ export default async function parseHTML(filePath, config) {
25
25
  });
26
26
 
27
27
  const newTemplate = $.html();
28
- // Replace the original <template> block with the updated version.
29
28
  content = content.replace(
30
29
  templateRegex,
31
30
  `<template>${newTemplate}</template>`
32
31
  );
33
32
  }
34
- } else if (ext === ".ts") {
35
- // Process TypeScript files with inline templates.
36
- // This regex matches a template property with a backtick string literal.
37
- const templateRegex = /template\s*:\s*`([\s\S]*?)`/gm;
38
- content = content.replace(templateRegex, (match, templateContent) => {
39
- const $ = cheerio.load(templateContent, { xmlMode: false });
33
+ } else if (ext === ".html" || ext === ".ng.html") {
34
+ // Check if the content is a full HTML document or just a fragment.
35
+ const isFullDocument = content.trim().toLowerCase().startsWith("<html");
36
+
37
+ if (isFullDocument) {
38
+ // Process as a full document.
39
+ const $ = cheerio.load(content, { xmlMode: false });
40
40
 
41
41
  $("*").each((_, el) => {
42
42
  const tag = el.tagName || el.name;
@@ -46,23 +46,30 @@ export default async function parseHTML(filePath, config) {
46
46
  }
47
47
  });
48
48
 
49
- const newTemplate = $.html();
50
- // Replace the original inline template content.
51
- return match.replace(templateContent, newTemplate);
52
- });
53
- } else if (ext === ".html") {
54
- // For .html files, assume the entire file is HTML content.
55
- const $ = cheerio.load(content, { xmlMode: false });
49
+ content = $.html();
50
+ } else {
51
+ // Process as an HTML fragment.
52
+ // Wrap in a temporary container.
53
+ const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
54
+ xmlMode: false,
55
+ });
56
56
 
57
- $("*").each((_, el) => {
58
- const tag = el.tagName || el.name;
59
- if (config.excludeTags.includes(tag)) return;
60
- if (!$(el).attr(config.attributeName)) {
61
- $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
62
- }
63
- });
57
+ $("#fragment")
58
+ .find("*")
59
+ .each((_, el) => {
60
+ const tag = el.tagName || el.name;
61
+ if (config.excludeTags.includes(tag)) return;
62
+ if (!$(el).attr(config.attributeName)) {
63
+ $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
64
+ }
65
+ });
64
66
 
65
- content = $.html();
67
+ // Extract only the inner HTML of the temporary container.
68
+ content = $("#fragment").html();
69
+ }
70
+ } else {
71
+ console.log(`Skipping unsupported file: ${filePath}`);
72
+ return;
66
73
  }
67
74
 
68
75
  fs.writeFileSync(filePath, content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.3",
3
+ "version": "1.0.32",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",