Markus-Rost 4 лет назад
Родитель
Сommit
006460d0d0
5 измененных файлов с 43 добавлено и 12 удалено
  1. 1 1
      RcGcDb
  2. 3 3
      cmds/wiki/general.js
  3. 2 2
      cmds/wiki/user.js
  4. 25 3
      functions/parse_page.js
  5. 12 3
      util/functions.js

+ 1 - 1
RcGcDb

@@ -1 +1 @@
-Subproject commit fc29d097081343d65ca8fbd1311542d04570cb12
+Subproject commit 5aab6222f91e49d28527d7065778bf242cb577d4

+ 3 - 3
cmds/wiki/general.js

@@ -268,7 +268,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 								}
 								}
 							}
 							}
 				
 				
-							msg.sendChannel( spoiler + '<' + pagelink + '>' + text + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, ( querypage.title === body.query.general.mainpage ? '' : new URL(body.query.general.logo, wiki).href )) );
+							msg.sendChannel( spoiler + '<' + pagelink + '>' + text + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, ( querypage.title === body.query.general.mainpage ? '' : new URL(body.query.general.logo, wiki).href ), fragment) );
 						}
 						}
 					}
 					}
 				}, error => {
 				}, error => {
@@ -358,7 +358,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 					}
 					}
 				}
 				}
 				
 				
-				msg.sendChannel( spoiler + '<' + pagelink + '>' + text + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, ( querypage.title === body.query.general.mainpage ? '' : new URL(body.query.general.logo, wiki).href )) );
+				msg.sendChannel( spoiler + '<' + pagelink + '>' + text + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, ( querypage.title === body.query.general.mainpage ? '' : new URL(body.query.general.logo, wiki).href ), ( fragment || ( body.query.redirects && body.query.redirects[0].tofragment ) || '' )) );
 				
 				
 				if ( reaction ) reaction.removeEmoji();
 				if ( reaction ) reaction.removeEmoji();
 			}
 			}
@@ -446,7 +446,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 			}, error => {
 			}, error => {
 				console.log( '- Error while getting the main page: ' + error );
 				console.log( '- Error while getting the main page: ' + error );
 			} ).finally( () => {
 			} ).finally( () => {
-				msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} ).then( message => parse_page(message, body.query.general.mainpage, embed, wiki, '') );
+				msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} ).then( message => parse_page(message, body.query.general.mainpage, embed, wiki, '', fragment) );
 				
 				
 				if ( reaction ) reaction.removeEmoji();
 				if ( reaction ) reaction.removeEmoji();
 			} );
 			} );

+ 2 - 2
cmds/wiki/user.js

@@ -61,7 +61,7 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 						embed.setThumbnail( wiki.toLink('Special:FilePath/' + querypage.pageprops.page_image_free, {version:Date.now()}) );
 						embed.setThumbnail( wiki.toLink('Special:FilePath/' + querypage.pageprops.page_image_free, {version:Date.now()}) );
 					}
 					}
 					
 					
-					msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, '') );
+					msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, '', fragment) );
 				}
 				}
 			}
 			}
 			else {
 			else {
@@ -216,7 +216,7 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 				}
 				}
 				else embed.setThumbnail( new URL(body.query.general.logo, wiki).href );
 				else embed.setThumbnail( new URL(body.query.general.logo, wiki).href );
 				
 				
-				msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, new URL(body.query.general.logo, wiki).href) );
+				msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} ).then( message => parse_page(message, querypage.title, embed, wiki, new URL(body.query.general.logo, wiki).href, fragment) );
 			}
 			}
 			
 			
 			if ( reaction ) reaction.removeEmoji();
 			if ( reaction ) reaction.removeEmoji();

+ 25 - 3
functions/parse_page.js

@@ -39,12 +39,13 @@ const keepMainPageTag = [
  * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
  * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
  * @param {import('../util/wiki.js')} wiki - The wiki for the page.
  * @param {import('../util/wiki.js')} wiki - The wiki for the page.
  * @param {String} thumbnail - The default thumbnail for the wiki.
  * @param {String} thumbnail - The default thumbnail for the wiki.
+ * @param {String} [fragment] - The section title to embed.
  */
  */
-function parse_page(msg, title, embed, wiki, thumbnail) {
-	if ( !msg || ( embed.description && embed.thumbnail?.url !== thumbnail && !embed.brokenInfobox ) ) {
+function parse_page(msg, title, embed, wiki, thumbnail, fragment = '') {
+	if ( !msg || ( embed.description && embed.thumbnail?.url !== thumbnail && !embed.brokenInfobox && !fragment ) ) {
 		return;
 		return;
 	}
 	}
-	got.get( wiki + 'api.php?action=parse&prop=text|images&section=0&disablelimitreport=true&disableeditsection=true&disabletoc=true&sectionpreview=true&page=' + encodeURIComponent( title ) + '&format=json' ).then( response => {
+	got.get( wiki + 'api.php?action=parse&prop=text|images' + ( fragment ? '' : '&section=0' ) + '&disablelimitreport=true&disableeditsection=true&disabletoc=true&sectionpreview=true&page=' + encodeURIComponent( title ) + '&format=json' ).then( response => {
 		if ( response.statusCode !== 200 || !response?.body?.parse?.text ) {
 		if ( response.statusCode !== 200 || !response?.body?.parse?.text ) {
 			console.log( '- ' + response.statusCode + ': Error while parsing the page: ' + response?.body?.error?.info );
 			console.log( '- ' + response.statusCode + ': Error while parsing the page: ' + response?.body?.error?.info );
 			return;
 			return;
@@ -91,6 +92,27 @@ function parse_page(msg, title, embed, wiki, thumbnail) {
 				change = true;
 				change = true;
 			}
 			}
 		}
 		}
+		if ( fragment && embed.length < 4750 && embed.fields.length < 25
+		&& embed.fields[0]?.name.replace( / /g, '_' ) !== fragment.replace( / /g, '_' ) ) {
+			var section = $('h1, h2, h3, h4, h5, h6').children('span#' + fragment).parent();
+			if ( section.length ) {
+				var sectionLevel = section[0].tagName.replace('h', '');
+				var sectionContent = $('<div>').append(
+					section.nextUntil(['h1','h2','h3','h4','h5','h6'].slice(0, sectionLevel).join(', '))
+				);
+				section.find(removeClasses.join(', ')).remove();
+				sectionContent.find(infoboxList.join(', ')).remove();
+				sectionContent.find(removeClasses.join(', ')).remove();
+				var name = htmlToPlain(section).trim();
+				if ( name.length > 250 ) name = name.substring(0, 250) + '\u2026';
+				var value = htmlToPlain(sectionContent).trim();
+				if ( value.length > 1000 ) value = value.substring(0, 1000) + '\u2026';
+				if ( name.length && value.length ) {
+					embed.spliceFields( 0, 0, {name, value} );
+					change = true;
+				}
+			}
+		}
 		if ( !embed.description && embed.length < 5000 ) {
 		if ( !embed.description && embed.length < 5000 ) {
 			$('h1, h2, h3, h4, h5, h6').nextAll().remove();
 			$('h1, h2, h3, h4, h5, h6').nextAll().remove();
 			$('h1, h2, h3, h4, h5, h6').remove();
 			$('h1, h2, h3, h4, h5, h6').remove();

+ 12 - 3
util/functions.js

@@ -153,18 +153,27 @@ function htmlToPlain(html) {
 				if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel);
 				if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel);
 				text += '• ';
 				text += '• ';
 			}
 			}
+			if ( tagname === 'h1' ) text += '***__';
+			if ( tagname === 'h2' ) text += '**__';
+			if ( tagname === 'h3' ) text += '**';
+			if ( tagname === 'h4' ) text += '__';
+			if ( tagname === 'h5' ) text += '*';
+			if ( tagname === 'h6' ) text += '';
 		},
 		},
 		ontext: (htmltext) => {
 		ontext: (htmltext) => {
 			if ( !reference ) {
 			if ( !reference ) {
-				if ( listlevel > -1 ) {
-					htmltext = htmltext.replace( /\n/g, '\n' + '\u200b '.repeat(4 * listlevel + 3) );
-				}
 				text += escapeFormatting(htmltext);
 				text += escapeFormatting(htmltext);
 			}
 			}
 		},
 		},
 		onclosetag: (tagname) => {
 		onclosetag: (tagname) => {
 			if ( tagname === 'sup' ) reference = false;
 			if ( tagname === 'sup' ) reference = false;
 			if ( tagname === 'ul' ) listlevel--;
 			if ( tagname === 'ul' ) listlevel--;
+			if ( tagname === 'h1' ) text += '__***';
+			if ( tagname === 'h2' ) text += '__**';
+			if ( tagname === 'h3' ) text += '**';
+			if ( tagname === 'h4' ) text += '__';
+			if ( tagname === 'h5' ) text += '*';
+			if ( tagname === 'h6' ) text += '';
 		},
 		},
 		oncomment: (commenttext) => {
 		oncomment: (commenttext) => {
 			if ( /^LINK'" \d+:\d+$/.test(commenttext) ) text += '*UNKNOWN LINK*';
 			if ( /^LINK'" \d+:\d+$/.test(commenttext) ) text += '*UNKNOWN LINK*';