@idomatic/parser-html 1.0.41 → 1.0.43

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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## [1.0.43] - 2024-12-22
4
+
5
+ ### Fixed
6
+ - Fixed HTML parser import/export bug where `templateRegex` was not properly exported from consts/index.js
7
+ - Resolved module loading issues that prevented proper functionality
8
+
9
+ ### Changed
10
+ - Updated import statement to use correct export name (`TEMPLATE_REGEX as templateRegex`)
11
+
12
+ ## [1.0.42] - Previous releases
13
+ - Previous functionality and features
package/consts/index.js CHANGED
@@ -1 +1,2 @@
1
- export const templateRegex = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
1
+ export const TEMPLATE_REGEX = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
2
+ export const TAG_REGEX = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import { templateRegex } from "./consts/index.js";
3
+ import { TEMPLATE_REGEX as templateRegex } from "./consts/index.js";
4
4
  import { processHtmlContent, processVueTemplate } from "./utils/index.js";
5
5
 
6
6
  /**
@@ -11,6 +11,7 @@ import { processHtmlContent, processVueTemplate } from "./utils/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");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -17,6 +17,6 @@
17
17
  "author": "Amar Smajlovic",
18
18
  "license": "MIT",
19
19
  "dependencies": {
20
- "cheerio": "^1.0.0-rc.12"
20
+ "magic-string": "^0.30.17"
21
21
  }
22
22
  }
package/utils/index.js CHANGED
@@ -1,41 +1,68 @@
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
+ import { TAG_REGEX } from "../consts/index.js";
3
4
 
4
5
  /**
5
- * Processes a Vue template content.
6
+ * Processes a Vue template content by inserting unique IDs while preserving formatting.
7
+ *
6
8
  * @param {string} templateContent - The content inside the Vue <template> tag.
7
9
  * @param {object} config - The configuration object.
10
+ * @param {string} config.attributeName - The attribute name to add (e.g. "id" or "data-id").
11
+ * @param {string} config.prefix - The prefix for generated IDs.
12
+ * @param {string[]} [config.excludeTags] - An array of tag names to exclude (optional).
8
13
  * @returns {string} - The updated template content.
9
14
  */
10
15
  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();
16
+ const { attributeName, prefix, excludeTags = [] } = config;
17
+ const ms = new MagicString(templateContent);
18
+
19
+ let match;
20
+ while ((match = TAG_REGEX.exec(templateContent)) !== null) {
21
+ const fullMatch = match[0];
22
+ const tagName = match[1];
23
+ const attributesStr = match[2];
24
+ const selfClosing = match[3] || "";
25
+
26
+ if (excludeTags.includes(tagName)) continue;
27
+
28
+ const idRegex = new RegExp(`\\s${attributeName}\\s*=`);
29
+ if (idRegex.test(attributesStr)) continue;
30
+
31
+ const insertPosition =
32
+ match.index + fullMatch.length - (selfClosing.length + 1);
33
+ ms.appendLeft(
34
+ insertPosition,
35
+ ` ${attributeName}="${prefix}${randomUUID()}"`
36
+ );
37
+ }
38
+
39
+ return ms.toString();
16
40
  }
17
41
 
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
42
  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();
43
+ const { attributeName, prefix, excludeTags = [] } = config;
44
+ const ms = new MagicString(htmlContent);
45
+
46
+ const tagRegex = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
47
+ let match;
48
+ while ((match = tagRegex.exec(htmlContent)) !== null) {
49
+ const fullMatch = match[0];
50
+ const tagName = match[1];
51
+ const attributesStr = match[2];
52
+ const selfClosing = match[3] || "";
53
+
54
+ if (excludeTags.includes(tagName)) continue;
55
+
56
+ const idRegex = new RegExp(`\\s${attributeName}\\s*=`);
57
+ if (idRegex.test(attributesStr)) continue;
58
+
59
+ const insertPosition =
60
+ match.index + fullMatch.length - (selfClosing.length + 1);
61
+ ms.appendLeft(
62
+ insertPosition,
63
+ ` ${attributeName}="${prefix}${randomUUID()}"`
64
+ );
40
65
  }
66
+
67
+ return ms.toString();
41
68
  }