@@ -24,9 +24,41 @@ export const prepareShowBtnEscapeHTML = () => {
2424} ;
2525
2626export const cleanHTML = ( { initialText } ) => {
27+ const altRegex = / ( a l t = " ( .* ?) " ) / g;
28+ const altTextMap = { } ;
29+ let altTextIndex = 0 ;
30+
31+ // Replace alt attributes with ALT_TEXT_0/1/2...
32+ const initialTextWithoutAltAttr = initialText . replace ( altRegex , ( match ) => {
33+ const altText = `ALT_TEXT_${ altTextIndex ++ } ` ;
34+ altTextMap [ altText ] = match ;
35+ return altText ;
36+ } ) ;
37+
38+ // Replace HTML entities in the rest of the text, excluding altTexts
2739 const translateRegex = new RegExp ( `&(${ Object . keys ( alphanumericMap ) . join ( '|' ) } );` , 'g' ) ;
2840 const translator = ( $0 , $1 ) => alphanumericMap [ $1 ] ;
29- return initialText . replace ( translateRegex , translator ) ;
41+ const cleanedTextWithoutAltAttr = initialTextWithoutAltAttr . replace ( translateRegex , translator ) ;
42+
43+ // Clean altText matches in altTextMap, ignore "
44+ const alphanumericMapForAltText = { ...alphanumericMap } ;
45+ delete alphanumericMapForAltText . quot ;
46+ Object . keys ( altTextMap ) . forEach ( ( key ) => {
47+ const altTextValue = altTextMap [ key ] ;
48+ const cleanedAltTextValue = altTextValue . replace (
49+ / & ( \w + ) ; / g,
50+ ( $0 , $1 ) => alphanumericMapForAltText [ $1 ] || $0 ,
51+ ) ;
52+ altTextMap [ key ] = cleanedAltTextValue ;
53+ } ) ;
54+
55+ // Restore the original alt attributes from altTextMap
56+ const finalCleanedText = Object . keys ( altTextMap ) . reduce (
57+ ( text , altText ) => text . replace ( altText , altTextMap [ altText ] ) ,
58+ cleanedTextWithoutAltAttr ,
59+ ) ;
60+
61+ return finalCleanedText ;
3062} ;
3163
3264export const syntaxChecker = ( { textArr, lang } ) => {
0 commit comments