@idomatic/parser-html 1.0.39 → 1.0.42

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.
package/helpers/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { randomUUID } from "crypto";
2
+
1
3
  /**
2
4
  * Adds unique attributes to all elements that don't have the specified attribute.
3
5
  * It skips elements whose tag names appear in the config's excludeTags array.
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import { processVueTemplate, processHtmlContent } from "./utils.js";
4
3
  import { templateRegex } from "./consts/index.js";
4
+ import { processHtmlContent, processVueTemplate } from "./utils/index.js";
5
5
 
6
6
  /**
7
7
  * Parses an HTML/Vue file and updates its content with unique IDs.
@@ -11,12 +11,14 @@ import { templateRegex } from "./consts/index.js";
11
11
  * @param {boolean} dry - If true, does not write changes back to disk.
12
12
  * @returns {boolean} - Returns true if changes were made.
13
13
  */
14
+
14
15
  export default async function parseHTML(filePath, config, dry = false) {
15
16
  const ext = path.extname(filePath).toLowerCase();
16
17
  const originalContent = fs.readFileSync(filePath, "utf-8");
17
18
  let updatedContent = originalContent;
18
19
 
19
20
  if (ext === ".vue") {
21
+ // Process Vue <template> tag contents using MagicString-based approach
20
22
  const match = originalContent.match(templateRegex);
21
23
  if (match) {
22
24
  const newTemplate = processVueTemplate(match[1], config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.39",
3
+ "version": "1.0.42",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -17,6 +17,7 @@
17
17
  "author": "Amar Smajlovic",
18
18
  "license": "MIT",
19
19
  "dependencies": {
20
- "cheerio": "^1.0.0-rc.12"
20
+ "cheerio": "^1.0.0-rc.12",
21
+ "magic-string": "^0.30.17"
21
22
  }
22
23
  }
package/utils/index.js CHANGED
@@ -1,41 +1,77 @@
1
- import * as cheerio from "cheerio";
2
- import { addIds } from "./helpers/index.js";
1
+ import MagicString from "magic-string";
2
+ import { randomUUID } from "crypto";
3
3
 
4
4
  /**
5
- * Processes a Vue template content.
5
+ * Processes a Vue template content by inserting unique IDs while preserving formatting.
6
+ *
6
7
  * @param {string} templateContent - The content inside the Vue <template> tag.
7
8
  * @param {object} config - The configuration object.
9
+ * @param {string} config.attributeName - The attribute name to add (e.g. "id" or "data-id").
10
+ * @param {string} config.prefix - The prefix for generated IDs.
11
+ * @param {string[]} [config.excludeTags] - An array of tag names to exclude (optional).
8
12
  * @returns {string} - The updated template content.
9
13
  */
10
14
  export function processVueTemplate(templateContent, config) {
11
- const $ = cheerio.load(templateContent, { xmlMode: false });
12
- // For Vue files, addIds might not need the config if handled differently;
13
- // adjust as needed.
14
- addIds($, config);
15
- return $.html();
15
+ const { attributeName, prefix, excludeTags = [] } = config;
16
+ const ms = new MagicString(templateContent);
17
+
18
+ // A generic regex to match opening tags:
19
+ // - It captures the tag name and its attributes.
20
+ // - This regex is simplified, so you might need additional adjustments for complex cases.
21
+ const tagRegex = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
22
+
23
+ let match;
24
+ while ((match = tagRegex.exec(templateContent)) !== null) {
25
+ const fullMatch = match[0]; // entire opening tag text
26
+ const tagName = match[1]; // tag name, e.g., v-btn, i, etc.
27
+ const attributesStr = match[2]; // all attributes as a string
28
+ const selfClosing = match[3] || ""; // self-closing slash if present
29
+
30
+ // If the tag is in the exclude list, skip processing.
31
+ if (excludeTags.includes(tagName)) continue;
32
+
33
+ // Check if this tag already has the attribute
34
+ const idRegex = new RegExp(`\\s${attributeName}\\s*=`);
35
+ if (idRegex.test(attributesStr)) continue;
36
+
37
+ // Insert the attribute just before the closing of the tag.
38
+ // We subtract the length of any trailing self-closing slash and the ">" character.
39
+ const insertPosition =
40
+ match.index + fullMatch.length - (selfClosing.length + 1);
41
+ ms.appendLeft(
42
+ insertPosition,
43
+ ` ${attributeName}="${prefix}${randomUUID()}"`
44
+ );
45
+ }
46
+
47
+ return ms.toString();
16
48
  }
17
49
 
18
- /**
19
- * Processes HTML content (full document or fragment) by adding IDs.
20
- *
21
- * @param {string} htmlContent - The original HTML content.
22
- * @param {object} config - The configuration object.
23
- * @returns {string} - The updated HTML content.
24
- */
25
50
  export function processHtmlContent(htmlContent, config) {
26
- // Determine if we have a full document
27
- const isFullDocument = htmlContent.trim().toLowerCase().startsWith("<html");
28
-
29
- if (isFullDocument) {
30
- const $ = cheerio.load(htmlContent, { xmlMode: false });
31
- addIds($, config);
32
- return $.html();
33
- } else {
34
- // Wrap the fragment to ensure Cheerio parses it properly.
35
- const $ = cheerio.load(`<div id="fragment">${htmlContent}</div>`, {
36
- xmlMode: false,
37
- });
38
- addIds($, config);
39
- return $("#fragment").html();
51
+ const { attributeName, prefix, excludeTags = [] } = config;
52
+ const ms = new MagicString(htmlContent);
53
+
54
+ // This regex is similar to the Vue one.
55
+ const tagRegex = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
56
+ let match;
57
+ while ((match = tagRegex.exec(htmlContent)) !== null) {
58
+ const fullMatch = match[0];
59
+ const tagName = match[1];
60
+ const attributesStr = match[2];
61
+ const selfClosing = match[3] || "";
62
+
63
+ if (excludeTags.includes(tagName)) continue;
64
+
65
+ const idRegex = new RegExp(`\\s${attributeName}\\s*=`);
66
+ if (idRegex.test(attributesStr)) continue;
67
+
68
+ const insertPosition =
69
+ match.index + fullMatch.length - (selfClosing.length + 1);
70
+ ms.appendLeft(
71
+ insertPosition,
72
+ ` ${attributeName}="${prefix}${randomUUID()}"`
73
+ );
40
74
  }
75
+
76
+ return ms.toString();
41
77
  }