1- // @ts -nocheck
2- function escapeRegExp ( string ) {
1+ import * as cheerio from 'cheerio' ;
2+
3+ type TransformFunction = ( url : string , siteUrl : string , itemPath : string , options : HtmlTransformOptions ) => string ;
4+
5+ interface HtmlTransformOptions {
6+ assetsOnly ?: boolean ;
7+ secure ?: boolean ;
8+ earlyExitMatchStr ?: string ;
9+ }
10+
11+ interface Replacement {
12+ name : string ;
13+ originalValue : string ;
14+ transformedValue ?: string ;
15+ skip ?: boolean ;
16+ }
17+
18+ function escapeRegExp ( string : string ) : string {
319 return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
420}
521
6- function extractSrcsetUrls ( srcset = '' ) {
22+ function extractSrcsetUrls ( srcset : string = '' ) : string [ ] {
723 return srcset . split ( ',' ) . map ( ( part ) => {
824 return part . trim ( ) . split ( / \s + / ) [ 0 ] ;
925 } ) ;
1026}
1127
12- function extractStyleUrls ( style = '' ) {
13- const urls = [ ] ;
28+ function extractStyleUrls ( style : string = '' ) : string [ ] {
29+ const urls : string [ ] = [ ] ;
1430 const regex = / u r l \( [ ' | " ] ( [ ^ ) ] + ) [ ' | " ] \) / g;
15- let match ;
31+ let match : RegExpExecArray | null ;
1632
1733 while ( ( match = regex . exec ( style ) ) !== null ) {
1834 urls . push ( match [ 1 ] ) ;
@@ -21,15 +37,20 @@ function extractStyleUrls(style = '') {
2137 return urls ;
2238}
2339
24- function htmlTransform ( html = '' , siteUrl , transformFunction , itemPath , _options ) {
25- const defaultOptions = { assetsOnly : false , secure : false } ;
40+ function htmlTransform (
41+ html : string = '' ,
42+ siteUrl : string ,
43+ transformFunction : TransformFunction ,
44+ itemPath : string ,
45+ _options : HtmlTransformOptions = { }
46+ ) : string {
47+ const defaultOptions : Required < Pick < HtmlTransformOptions , 'assetsOnly' | 'secure' > > = { assetsOnly : false , secure : false } ;
2648 const options = Object . assign ( { } , defaultOptions , _options || { } ) ;
2749
2850 if ( ! html || ( options . earlyExitMatchStr && ! html . match ( new RegExp ( options . earlyExitMatchStr ) ) ) ) {
2951 return html ;
3052 }
3153
32- const cheerio = require ( 'cheerio' ) ;
3354 const htmlContent = cheerio . load ( html , { decodeEntities : false } ) ;
3455
3556 // replacements is keyed with the attr name + original relative value so
@@ -42,9 +63,9 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
4263 // {name: 'href', originalValue: '/test', absoluteValue: '.../test'},
4364 // ]
4465 // }
45- const replacements = { } ;
66+ const replacements : Record < string , Replacement [ ] > = { } ;
4667
47- function addReplacement ( replacement ) {
68+ function addReplacement ( replacement : Replacement ) : void {
4869 const key = `${ replacement . name } ="${ replacement . originalValue } "` ;
4970
5071 if ( ! replacements [ key ] ) {
@@ -55,23 +76,29 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
5576 }
5677
5778 // find all of the relative url attributes that we care about
58- [ 'href' , 'src' , 'srcset' , 'style' ] . forEach ( ( attributeName ) => {
59- htmlContent ( '[' + attributeName + ']' ) . each ( ( ix , el ) => {
79+ ( [ 'href' , 'src' , 'srcset' , 'style' ] as const ) . forEach ( ( attributeName ) => {
80+ htmlContent ( '[' + attributeName + ']' ) . each ( ( _ix , el ) => {
81+ const $el = htmlContent ( el ) ;
6082 // ignore <stream> elems and html inside of <code> elements
61- if ( el . name === 'stream' || htmlContent ( el ) . closest ( 'code' ) . length ) {
62- addReplacement ( {
63- name : attributeName ,
64- originalValue : htmlContent ( el ) . attr ( attributeName ) ,
65- skip : true
66- } ) ;
83+ if ( ( el as any ) . name === 'stream' || $el . closest ( 'code' ) . length ) {
84+ const attrValue = $el . attr ( attributeName ) ;
85+ if ( attrValue ) {
86+ addReplacement ( {
87+ name : attributeName ,
88+ originalValue : attrValue ,
89+ skip : true
90+ } ) ;
91+ }
6792 return ;
6893 }
6994
70- el = htmlContent ( el ) ;
71- const originalValue = el . attr ( attributeName ) ;
95+ const originalValue = $el . attr ( attributeName ) ;
96+ if ( ! originalValue ) {
97+ return ;
98+ }
7299
73100 if ( attributeName === 'srcset' || attributeName === 'style' ) {
74- let urls ;
101+ let urls : string [ ] ;
75102
76103 if ( attributeName === 'srcset' ) {
77104 urls = extractSrcsetUrls ( originalValue ) ;
@@ -83,7 +110,7 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
83110
84111 urls . forEach ( ( url , i ) => {
85112 if ( absoluteUrls [ i ] ) {
86- let regex = new RegExp ( escapeRegExp ( url ) , 'g' ) ;
113+ const regex = new RegExp ( escapeRegExp ( url ) , 'g' ) ;
87114 transformedValue = transformedValue . replace ( regex , absoluteUrls [ i ] ) ;
88115 }
89116 } ) ;
@@ -114,7 +141,7 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
114141 for ( const [ , attrs ] of Object . entries ( replacements ) ) {
115142 let skipCount = 0 ;
116143
117- attrs . forEach ( ( { skip, name, originalValue, transformedValue} ) => {
144+ attrs . forEach ( ( { skip, name, originalValue, transformedValue} : Replacement ) => {
118145 if ( skip ) {
119146 skipCount += 1 ;
120147 return ;
@@ -129,7 +156,7 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
129156 let matchCount = 0 ;
130157 html = html . replace ( regex , ( match , p1 ) => {
131158 let result = match ;
132- if ( matchCount === skipCount ) {
159+ if ( matchCount === skipCount && transformedValue ) {
133160 result = match . replace ( p1 , p1 . replace ( originalValue , transformedValue ) ) ;
134161 }
135162 matchCount += 1 ;
@@ -141,4 +168,5 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
141168 return html ;
142169}
143170
171+ export default htmlTransform ;
144172module . exports = htmlTransform ;
0 commit comments