1- /* eslint-disable @typescript-eslint/no-unsafe-return */
2- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
3- /* eslint-disable @typescript-eslint/no-unsafe-call */
4- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
5- /* eslint-disable @typescript-eslint/explicit-member-accessibility */
6- /* eslint-disable @typescript-eslint/restrict-template-expressions */
7- /* eslint-disable @typescript-eslint/member-ordering */
8- /* eslint-disable no-console */
91import * as fs from 'fs' ;
10- import * as cheerio from 'cheerio' ;
112import { FileConstant } from '../fileutils/FileConstant' ;
123
134const DEFAULT_NAMESPACE = 'c' ;
14- const TAG = 'tag' ;
15-
16- // const { window } = new JSDOM();
17- // const { document } = window;
185
196export class HTMLParser {
20- private parser : cheerio . CheerioAPI ;
217 html : string ;
228 // eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
239 constructor ( htmlFilePath : string ) {
2410 // Load the HTML file and initialize cheerio
2511 this . html = this . loadHTMLFromFile ( htmlFilePath ) ;
26- this . parser = cheerio . load ( this . html , { xmlMode : true } ) ;
2712 }
2813
2914 // Method to load HTML from a file
@@ -38,50 +23,12 @@ export class HTMLParser {
3823
3924 // Method to replace custom tags
4025 public replaceTags ( namespaceTag : string ) : Map < string , string > {
41- const htmlContentMap = new Map < string , string > ( ) ;
42- // Load the HTML into cheerio
43- //const $ = this.parser;
44- htmlContentMap . set ( FileConstant . BASE_CONTENT , this . html ) ;
45- // Find all tags that contain the substring "omnistudio" in their tag name
46- // $('*').each((i, element) => {
47- // if (element.type === TAG && element.name && element.name.includes(namespaceTag + '-')) {
48- // // Create a new tag with the same content and attributes as the old tag
49- // const newTag = DEFAULT_NAMESPACE + element.name.substring(element.name.indexOf('-'));
50- // const newElement = $(`<${newTag}>`).html($(element).html());
51-
52- // // Copy all attributes from the old element to the new one
53- // Object.entries(element.attribs).forEach(([key, value]) => {
54- // newElement.attr(key, value);
55- // });
56-
57- // // Replace the old element with the new one
58- // $(element).replaceWith(newElement);
59- // }
60- // });
61- // // $.html().replace(/\n\s*/g, '');
62-
63- // let html = `
64- // <html lang="en">
65- // <head>
66- // <meta charset="UTF-8" />
67- // <meta name="viewport" content="width=device-width, initial-scale=1.0" />
68- // <title>Replace Tags Example</title>
69- // </head>
70- // <span dir={_dir}>test</span>
71- // <body>
72- // <template>
73- // <vlocity_ins-input vertical-align=stretch>Hello Shailesh</vlocity_ins-input>
74- // <vlocity_ins-input vertical-align=stretch>Another Input</vlocity_ins-input>
75- // </template>
76- // </body>
77- // </html>`;
78-
79- // Use a regular expression to match <omnistudio-input ...>...</omnistudio-input>
80- this . html = this . html . replace ( '/<' + namespaceTag + '-/g' , '<c-' ) . replace ( '/<' + namespaceTag + '-/g' , '</c-' ) ;
81-
82- console . log ( this . html ) ;
83- // htmlContentMap.set(FileConstant.MODIFIED_CONTENT, $.html());
84- htmlContentMap . set ( FileConstant . MODIFIED_CONTENT , this . html ) ;
26+ const htmlContentMap = new Map < string , string > ( ) ;
27+ htmlContentMap . set ( FileConstant . BASE_CONTENT , this . html ) ;
28+ // Use a regular expression to match <omnistudio-input ...>...</omnistudio-input>
29+ this . html = this . html . replace ( '<' + namespaceTag , '<' + DEFAULT_NAMESPACE ) . replace ( '</' + namespaceTag , '</' + DEFAULT_NAMESPACE ) ;
30+ htmlContentMap . set ( FileConstant . MODIFIED_CONTENT , this . html ) ;
31+ console . log ( this . html ) ;
8532 return htmlContentMap ;
8633 }
8734
@@ -98,6 +45,6 @@ console.log(this.html);
9845
9946 // Optional: Method to get the modified HTML as a string
10047 public getModifiedHTML ( ) : string {
101- return this . parser . html ( ) ;
48+ return this . html ;
10249 }
10350}
0 commit comments