77/* eslint-disable @typescript-eslint/member-ordering */
88/* eslint-disable no-console */
99import * as fs from 'fs' ;
10- import * as cheerio from 'cheerio' ;
1110import { FileConstant } from '../fileutils/FileConstant' ;
1211
1312const DEFAULT_NAMESPACE = 'c' ;
14- const TAG = 'tag' ;
15-
16- // const { window } = new JSDOM();
17- // const { document } = window;
1813
1914export class HTMLParser {
20- private parser : cheerio . CheerioAPI ;
2115 html : string ;
2216 // eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
2317 constructor ( htmlFilePath : string ) {
2418 // Load the HTML file and initialize cheerio
2519 this . html = this . loadHTMLFromFile ( htmlFilePath ) ;
26- this . parser = cheerio . load ( this . html , { xmlMode : true } ) ;
2720 }
2821
2922 // Method to load HTML from a file
@@ -39,27 +32,14 @@ export class HTMLParser {
3932 // Method to replace custom tags
4033 public replaceTags ( namespaceTag : string ) : Map < string , string > {
4134 const htmlContentMap = new Map < string , string > ( ) ;
42- // Load the HTML into cheerio
43- const $ = this . parser ;
4435 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 ( ) ) ;
36+ // Use a regular expression to match <omnistudio-input> to </omnistudio-input>
5137
52- // Copy all attributes from the old element to the new one
53- Object . keys ( element . attribs ) . forEach ( ( attr ) => {
54- newElement . attr ( attr , $ ( element ) . attr ( attr ) ) ;
55- } ) ;
38+ this . html = this . html
39+ . replace ( '<' + namespaceTag , '<' + DEFAULT_NAMESPACE )
40+ . replace ( '</' + namespaceTag , '</' + DEFAULT_NAMESPACE ) ;
5641
57- // Replace the old element with the new one
58- $ ( element ) . replaceWith ( newElement ) ;
59- }
60- } ) ;
61- $ . html ( ) . replace ( / \n \s * / g, '' ) ;
62- htmlContentMap . set ( FileConstant . MODIFIED_CONTENT , $ . html ( ) ) ;
42+ htmlContentMap . set ( FileConstant . MODIFIED_CONTENT , this . html ) ;
6343 return htmlContentMap ;
6444 }
6545
@@ -76,6 +56,6 @@ export class HTMLParser {
7656
7757 // Optional: Method to get the modified HTML as a string
7858 public getModifiedHTML ( ) : string {
79- return this . parser . html ( ) ;
59+ return this . html ;
8060 }
8161}
0 commit comments