@idomatic/parser-html 1.0.36 → 1.0.37

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 +20 -36
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -5,71 +5,55 @@ import { randomUUID } from "crypto";
5
5
 
6
6
  export default async function parseHTML(filePath, config) {
7
7
  const ext = path.extname(filePath).toLowerCase();
8
- let content = fs.readFileSync(filePath, "utf-8");
9
- let changed = false;
8
+ const originalContent = fs.readFileSync(filePath, "utf-8");
9
+ let updatedContent = originalContent; // fallback if nothing changes
10
10
 
11
+ const tagsToExclude = config.excludeTags || [];
11
12
  const addIds = ($) => {
12
13
  $("*").each((_, el) => {
13
- const tag = el.tagName || el.name;
14
- if (config.excludeTags.includes(tag)) return;
14
+ const tag = el.name;
15
+ if (tagsToExclude.includes(tag)) return;
15
16
  if (!$(el).attr(config.attributeName)) {
16
17
  $(el).attr(config.attributeName, `${config.prefix}${randomUUID()}`);
17
- changed = true;
18
18
  }
19
19
  });
20
20
  };
21
21
 
22
22
  if (ext === ".vue") {
23
23
  const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
24
- let match = content.match(templateRegex);
24
+ const match = originalContent.match(templateRegex);
25
25
 
26
26
  if (match) {
27
- const originalTemplate = match[1];
28
- const $ = cheerio.load(originalTemplate, { xmlMode: false });
27
+ const $ = cheerio.load(match[1], { xmlMode: false });
29
28
  addIds($);
30
29
  const newTemplate = $.html();
31
- content = content.replace(
30
+ updatedContent = originalContent.replace(
32
31
  templateRegex,
33
32
  `<template>${newTemplate}</template>`
34
33
  );
35
- } else {
36
- const scriptRegex = /<script[\s\S]*$/i;
37
- match = content.match(scriptRegex);
38
- let fragment, rest;
39
- if (match) {
40
- const index = match.index;
41
- fragment = content.substring(0, index);
42
- rest = content.substring(index);
43
- } else {
44
- fragment = content;
45
- rest = "";
46
- }
47
-
48
- const $ = cheerio.load(`<div id="fragment">${fragment}</div>`, {
49
- xmlMode: false,
50
- });
51
- addIds($);
52
- const newFragment = $("#fragment").html();
53
- content = newFragment + rest;
54
34
  }
55
35
  } else if (ext === ".html" || ext === ".ng.html") {
56
- const isFullDocument = content.trim().toLowerCase().startsWith("<html");
36
+ const isFullDocument = originalContent
37
+ .trim()
38
+ .toLowerCase()
39
+ .startsWith("<html");
57
40
  if (isFullDocument) {
58
- const $ = cheerio.load(content, { xmlMode: false });
41
+ const $ = cheerio.load(originalContent, { xmlMode: false });
59
42
  addIds($);
60
- content = $.html();
43
+ updatedContent = $.html();
61
44
  } else {
62
- const $ = cheerio.load(`<div id="fragment">${content}</div>`, {
45
+ const $ = cheerio.load(`<div id="fragment">${originalContent}</div>`, {
63
46
  xmlMode: false,
64
47
  });
65
48
  addIds($);
66
- content = $("#fragment").html();
49
+ updatedContent = $("#fragment").html();
67
50
  }
68
51
  }
69
52
 
70
- if (changed) {
71
- fs.writeFileSync(filePath, content);
53
+ if (updatedContent !== originalContent) {
54
+ fs.writeFileSync(filePath, updatedContent);
55
+ return true;
72
56
  }
73
57
 
74
- return changed;
58
+ return false;
75
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",