Ver Fonte

Show only custum image alt text

Markus-Rost há 4 anos atrás
pai
commit
84d5ccbd48
1 ficheiros alterados com 11 adições e 5 exclusões
  1. 11 5
      util/functions.js

+ 11 - 5
util/functions.js

@@ -187,7 +187,10 @@ function htmlToPlain(html) {
 				if ( listlevel > -1 ) text += '\u200b '.repeat(4 * (listlevel + 1));
 			}
 			if ( tagname === 'img' ) {
-				if ( attribs.alt && !reference ) text += escapeFormatting(attribs.alt);
+				if ( attribs.alt && attribs.src && !reference ) {
+					let regex = new RegExp( '/([\\da-f])/\\1[\\da-f]/' + attribs.alt.replace( / /g, '_' ).replace( /\W/g, '\\$&' ) + '(?:/|\\?|$)' );
+					if ( !regex.test(partialURIdecode(attribs.src)) ) text += escapeFormatting(attribs.alt);
+				}
 			}
 			if ( tagname === 'h1' ) {
 				text = text.replace( / +$/, '' );
@@ -301,10 +304,13 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
 				if ( listlevel > -1 ) text += '\u200b '.repeat(4 * (listlevel + 1));
 			}
 			if ( tagname === 'img' ) {
-				if ( attribs.alt && !reference ) {
-					if ( href && !code ) attribs.alt = attribs.alt.replace( /[\[\]]/g, '\\$&' );
-					if ( code ) text += attribs.alt.replace( /`/g, 'ˋ' );
-					else text += escapeFormatting(attribs.alt, ...escapeArgs);
+				if ( attribs.alt && attribs.src && !reference ) {
+					let regex = new RegExp( '/([\\da-f])/\\1[\\da-f]/' + attribs.alt.replace( / /g, '_' ).replace( /\W/g, '\\$&' ) + '(?:/|\\?|$)' );
+					if ( !regex.test(partialURIdecode(attribs.src)) ) {
+						if ( href && !code ) attribs.alt = attribs.alt.replace( /[\[\]]/g, '\\$&' );
+						if ( code ) text += attribs.alt.replace( /`/g, 'ˋ' );
+						else text += escapeFormatting(attribs.alt, ...escapeArgs);
+					}
 				}
 			}
 			if ( tagname === 'h1' ) {