瀏覽代碼

add Discord highlighting for SyntaxHighlight

MarkusRost 4 年之前
父節點
當前提交
c7880e21f1
共有 1 個文件被更改,包括 10 次插入2 次删除
  1. 10 2
      util/functions.js

+ 10 - 2
util/functions.js

@@ -186,6 +186,7 @@ function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
 	var code = false;
 	var code = false;
 	var href = '';
 	var href = '';
 	var ignoredTag = '';
 	var ignoredTag = '';
+	var syntaxhighlight = '';
 	var listlevel = -1;
 	var listlevel = -1;
 	var parser = new htmlparser.Parser( {
 	var parser = new htmlparser.Parser( {
 		onopentag: (tagname, attribs) => {
 		onopentag: (tagname, attribs) => {
@@ -198,7 +199,13 @@ function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
 			}
 			}
 			if ( tagname === 'pre' ) {
 			if ( tagname === 'pre' ) {
 				code = true;
 				code = true;
-				text += '```\n';
+				text += '```' + syntaxhighlight + '\n';
+			}
+			if ( tagname === 'div' && attribs.class ) {
+				let classes = attribs.class.split(' ');
+				if ( classes.includes( 'mw-highlight' ) ) {
+					syntaxhighlight = ( classes.find( syntax => syntax.startsWith( 'mw-highlight-lang-' ) )?.replace( 'mw-highlight-lang-', '' ) || '' );
+				}
 			}
 			}
 			if ( tagname === 'b' ) text += '**';
 			if ( tagname === 'b' ) text += '**';
 			if ( tagname === 'i' ) text += '*';
 			if ( tagname === 'i' ) text += '*';
@@ -315,6 +322,7 @@ function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
 				}
 				}
 				return;
 				return;
 			}
 			}
+			if ( syntaxhighlight && tagname === 'div' ) syntaxhighlight = '';
 			if ( tagname === 'b' ) text += '**';
 			if ( tagname === 'b' ) text += '**';
 			if ( tagname === 'i' ) text += '*';
 			if ( tagname === 'i' ) text += '*';
 			if ( tagname === 's' ) text += '~~';
 			if ( tagname === 's' ) text += '~~';
@@ -425,4 +433,4 @@ module.exports = {
 	limitLength,
 	limitLength,
 	partialURIdecode,
 	partialURIdecode,
 	allowDelete
 	allowDelete
-};
+};