@idomatic/parser-html 1.0.0 → 1.0.1

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