@idomatic/parser-html 1.0.1 → 1.0.3

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 +33 -16
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import cheerio from "cheerio";
3
+ import * as cheerio from "cheerio";
4
4
  import { randomUUID } from "crypto";
5
5
 
6
6
  export default async function parseHTML(filePath, config) {
@@ -8,8 +8,8 @@ export default async function parseHTML(filePath, config) {
8
8
  let content = fs.readFileSync(filePath, "utf-8");
9
9
 
10
10
  if (ext === ".vue") {
11
- // Extract the <template> block in Vue files
12
- const templateRegex = /<template>([\s\S]*?)<\/template>/;
11
+ // Process Vue single file components: update the <template> block.
12
+ const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
13
13
  const match = content.match(templateRegex);
14
14
 
15
15
  if (match) {
@@ -17,31 +17,48 @@ export default async function parseHTML(filePath, config) {
17
17
  const $ = cheerio.load(originalTemplate, { xmlMode: false });
18
18
 
19
19
  $("*").each((_, el) => {
20
- const tag = el.tagName;
21
- if (config.skipTags.includes(tag)) return;
22
-
23
- if (!$(el).attr("id")) {
24
- $(el).attr("id", `${config.prefix}${randomUUID()}`);
20
+ const tag = el.tagName || el.name;
21
+ if (config.excludeTags.includes(tag)) return;
22
+ if (!$(el).attr(config.attributeName)) {
23
+ $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
25
24
  }
26
25
  });
27
26
 
28
27
  const newTemplate = $.html();
29
- // Replace the original <template> block with the updated version
28
+ // Replace the original <template> block with the updated version.
30
29
  content = content.replace(
31
30
  templateRegex,
32
31
  `<template>${newTemplate}</template>`
33
32
  );
34
33
  }
35
- } else {
36
- // For .html and .ts files, assume the entire file is HTML content
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 });
40
+
41
+ $("*").each((_, el) => {
42
+ const tag = el.tagName || el.name;
43
+ if (config.excludeTags.includes(tag)) return;
44
+ if (!$(el).attr(config.attributeName)) {
45
+ $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
46
+ }
47
+ });
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.
37
55
  const $ = cheerio.load(content, { xmlMode: false });
38
56
 
39
57
  $("*").each((_, el) => {
40
- const tag = el.tagName;
41
- if (config.skipTags.includes(tag)) return;
42
-
43
- if (!$(el).attr("id")) {
44
- $(el).attr("id", `${config.prefix}${randomUUID()}`);
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()}`);
45
62
  }
46
63
  });
47
64
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",