Parcourir la source

parse section links

Markus-Rost il y a 4 ans
Parent
commit
28a2d10d1a
7 fichiers modifiés avec 30 ajouts et 25 suppressions
  1. 1 1
      cmds/wiki/diff.js
  2. 1 1
      cmds/wiki/fandom/diff.js
  3. 3 3
      cmds/wiki/general.js
  4. 1 1
      cmds/wiki/random.js
  5. 7 2
      cmds/wiki/user.js
  6. 4 4
      functions/parse_page.js
  7. 13 13
      util/functions.js

+ 1 - 1
cmds/wiki/diff.js

@@ -197,7 +197,7 @@ function gamepedia_diff_send(lang, msg, args, wiki, reaction, spoiler, compare)
 				}
 				if ( editor[1] === lang.get('diff.hidden') ) editorlink = editor[1];
 				var embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( ( title + '?diff=' + diff + '&oldid=' + oldid ).escapeFormatting() ).setURL( pagelink ).addField( editor[0], editorlink, true ).addField( size[0], size[1], true ).addField( comment[0], comment[1] ).setFooter( timestamp[1] );
-				if ( tags ) embed.addField( tags[0], htmlToDiscord(tags[1]) );
+				if ( tags ) embed.addField( tags[0], htmlToDiscord(tags[1], pagelink, true) );
 				
 				var more = '\n__' + lang.get('diff.info.more') + '__';
 				var whitespace = '__' + lang.get('diff.info.whitespace') + '__';

+ 1 - 1
cmds/wiki/fandom/diff.js

@@ -184,7 +184,7 @@ function fandom_diff_send(lang, msg, args, wiki, reaction, spoiler, compare) {
 					}
 					if ( editor[1] === lang.get('diff.hidden') ) editorlink = editor[1];
 					var embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( ( title + '?diff=' + diff + '&oldid=' + oldid ).escapeFormatting() ).setURL( pagelink ).addField( editor[0], editorlink, true ).addField( size[0], size[1], true ).addField( comment[0], comment[1] ).setFooter( timestamp[1] );
-					if ( tags ) embed.addField( tags[0], htmlToDiscord(tags[1]) );
+					if ( tags ) embed.addField( tags[0], htmlToDiscord(tags[1], pagelink, true) );
 					
 					var more = '\n__' + lang.get('diff.info.more') + '__';
 					var whitespace = '__' + lang.get('diff.info.whitespace') + '__';

+ 3 - 3
cmds/wiki/general.js

@@ -276,7 +276,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 				if ( !fragment && !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 					try {
 						var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-						parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, wiki.articleURL.href);
+						parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, pagelink);
 					}
 					catch ( error ) {
 						console.log( '- Failed to parse the infobox: ' + error );
@@ -368,7 +368,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 			if ( !( fragment || ( body.query.redirects && body.query.redirects[0].tofragment ) || '' ) && !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 				try {
 					var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-					parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, wiki.articleURL.href);
+					parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, pagelink);
 				}
 				catch ( error ) {
 					console.log( '- Failed to parse the infobox: ' + error );
@@ -458,7 +458,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 			if ( !fragment && !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 				try {
 					var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-					parse_infobox(infobox, embed, '', wiki.articleURL.href);
+					parse_infobox(infobox, embed, '', pagelink);
 				}
 				catch ( error ) {
 					console.log( '- Failed to parse the infobox: ' + error );

+ 1 - 1
cmds/wiki/random.js

@@ -65,7 +65,7 @@ function gamepedia_random(lang, msg, wiki, reaction, spoiler) {
 		if ( !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 			try {
 				var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-				parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, wiki.articleURL.href);
+				parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, pagelink);
 			}
 			catch ( error ) {
 				console.log( '- Failed to parse the infobox: ' + error );

+ 7 - 2
cmds/wiki/user.js

@@ -68,7 +68,7 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 					if ( !fragment && !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 						try {
 							var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-							parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, wiki.articleURL.href);
+							parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, pagelink);
 						}
 						catch ( error ) {
 							console.log( '- Failed to parse the infobox: ' + error );
@@ -238,7 +238,7 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 				if ( !fragment && !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 					try {
 						var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-						parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, wiki.articleURL.href);
+						parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href, pagelink);
 					}
 					catch ( error ) {
 						console.log( '- Failed to parse the infobox: ' + error );
@@ -366,6 +366,11 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 				}
 				embed.addField( gender[0], gender[1], true ).addField( registration[0], registration[1], true );
 				
+				if ( querypage.pageprops && querypage.pageprops.displaytitle ) {
+					var displaytitle = htmlToDiscord( querypage.pageprops.displaytitle );
+					if ( displaytitle.length > 250 ) displaytitle = displaytitle.substring(0, 250) + '\u2026';
+					embed.setTitle( displaytitle );
+				}
 				if ( querypage.pageprops && querypage.pageprops.description ) {
 					var description = htmlToDiscord( querypage.pageprops.description );
 					if ( description.length > 1000 ) description = description.substring(0, 1000) + '\u2026';

+ 4 - 4
functions/parse_page.js

@@ -149,7 +149,7 @@ function parse_page(msg, content, embed, wiki, reaction, {title, contentmodel},
 					let value = infobox.find(field.value.replace( /^`(.+)`$/, '[data-source="$1"] .pi-data-value, .pi-data-value[data-source="$1"]' )).html();
 					if ( !value ) value = infobox.find(field.value.replace( /^`(.+)`$/, '[data-item-name="$1"] .pi-data-value, .pi-data-value[data-item-name="$1"]' )).html();
 					if ( value ) {
-						value = htmlToDiscord(value, wiki.articleURL.href, true).trim().replace( /\n{3,}/g, '\n\n' );
+						value = htmlToDiscord(value, embed.url, true).trim().replace( /\n{3,}/g, '\n\n' );
 						if ( value.length > 500 ) value = limitLength(value, 500, 250);
 						if ( value ) field.value = value;
 					}
@@ -191,7 +191,7 @@ function parse_page(msg, content, embed, wiki, reaction, {title, contentmodel},
 					value.find(removeClasses.join(', ')).remove();
 					if ( !label.is('td') && label.html()?.trim() && value.html()?.trim() ) tdLabel = false;
 					label = htmlToPlain(label).trim().split('\n')[0];
-					value = htmlToDiscord(value, wiki.articleURL.href, true).trim().replace( /\n{3,}/g, '\n\n' );
+					value = htmlToDiscord(value, embed.url, true).trim().replace( /\n{3,}/g, '\n\n' );
 					if ( label.length > 100 ) label = label.substring(0, 100) + '\u2026';
 					if ( value.length > 500 ) value = limitLength(value, 500, 250);
 					if ( label && value ) embed.addField( label, value, true );
@@ -249,7 +249,7 @@ function parse_page(msg, content, embed, wiki, reaction, {title, contentmodel},
 				sectionContent.find('div, ' + removeClasses.join(', ')).remove();
 				var name = htmlToPlain(section).trim();
 				if ( name.length > 250 ) name = name.substring(0, 250) + '\u2026';
-				var value = htmlToDiscord(sectionContent, wiki.articleURL.href, true).trim().replace( /\n{3,}/g, '\n\n' );
+				var value = htmlToDiscord(sectionContent, embed.url, true).trim().replace( /\n{3,}/g, '\n\n' );
 				if ( value.length > 1000 ) value = limitLength(value, 1000, 20);
 				if ( name.length && value.length ) {
 					embed.spliceFields( 0, 0, {name, value} );
@@ -267,7 +267,7 @@ function parse_page(msg, content, embed, wiki, reaction, {title, contentmodel},
 			$('h1, h2, h3, h4, h5, h6').remove();
 			$(infoboxList.join(', ')).remove();
 			$('div, ' + removeClasses.join(', '), $('.mw-parser-output')).not(keepMainPageTag.join(', ')).remove();
-			var description = htmlToDiscord($.html(), wiki.articleURL.href, true).trim().replace( /\n{3,}/g, '\n\n' );
+			var description = htmlToDiscord($.html(), embed.url, true).trim().replace( /\n{3,}/g, '\n\n' );
 			if ( description ) {
 				if ( description.length > 1000 ) description = limitLength(description, 1000, 500);
 				embed.setDescription( description );

+ 13 - 13
util/functions.js

@@ -13,14 +13,14 @@ const got = require('got').extend( {
  * @param {Object} infobox - The content of the infobox.
  * @param {import('discord.js').MessageEmbed} embed - The message embed.
  * @param {String} [thumbnail] - The default thumbnail for the wiki.
- * @param {String} [serverpath] - The article path for relative links.
+ * @param {String} [pagelink] - The article path for relative links.
  * @returns {import('discord.js').MessageEmbed?}
  */
-function parse_infobox(infobox, embed, thumbnail, serverpath = '') {
+function parse_infobox(infobox, embed, thumbnail, pagelink = '') {
 	if ( !infobox || embed.fields.length >= 25 || embed.length > 5400 ) return;
 	if ( infobox.parser_tag_version === 2 ) {
 		infobox.data.forEach( group => {
-			parse_infobox(group, embed, thumbnail, serverpath);
+			parse_infobox(group, embed, thumbnail, pagelink);
 		} );
 		embed.fields = embed.fields.filter( (field, i, fields) => {
 			if ( field.name !== '\u200b' || !field.value.startsWith( '__**' ) ) return true;
@@ -32,7 +32,7 @@ function parse_infobox(infobox, embed, thumbnail, serverpath = '') {
 		case 'data':
 			var {label = '', value = '', source = '', 'item-name': name = ''} = infobox.data;
 			label = htmlToPlain(label).trim();
-			value = htmlToDiscord(value, serverpath, true).trim();
+			value = htmlToDiscord(value, pagelink, true).trim();
 			if ( label.includes( '*UNKNOWN LINK*' ) ) {
 				label = '`' + ( source || name )  + '`';
 				embed.brokenInfobox = true;
@@ -48,7 +48,7 @@ function parse_infobox(infobox, embed, thumbnail, serverpath = '') {
 		case 'panel':
 			var embedLength = embed.fields.length;
 			infobox.data.value.forEach( group => {
-				parse_infobox(group, embed, thumbnail, serverpath);
+				parse_infobox(group, embed, thumbnail, pagelink);
 			} );
 			embed.fields = embed.fields.filter( (field, i, fields) => {
 				if ( i < embedLength || field.name !== '\u200b' ) return true;
@@ -67,7 +67,7 @@ function parse_infobox(infobox, embed, thumbnail, serverpath = '') {
 			if ( label ) embed.addField( '\u200b', '**' + label + '**', false );
 		case 'group':
 			infobox.data.value.forEach( group => {
-				parse_infobox(group, embed, thumbnail, serverpath);
+				parse_infobox(group, embed, thumbnail, pagelink);
 			} );
 			break;
 		case 'header':
@@ -174,11 +174,11 @@ function htmlToPlain(html) {
 /**
  * Change HTML text to markdown text.
  * @param {String} html - The text in HTML.
- * @param {String} [serverpath] - The article path for relative links.
+ * @param {String} [pagelink] - The article path for relative links.
  * @param {Boolean[]} [escapeArgs] - Arguments for the escaping of text formatting.
  * @returns {String}
  */
-function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
+function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
 	var text = '';
 	var code = false;
 	var href = '';
@@ -279,9 +279,9 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
 				if ( !text.endsWith( '\n' ) ) text += '\n';
 				text += '';
 			}
-			if ( !serverpath ) return;
-			if ( tagname === 'a' && attribs.href && attribs.class !== 'new' && /^(?:(?:https?:)?\/)?\//.test(attribs.href) ) {
-				href = new URL(attribs.href, serverpath).href;
+			if ( !pagelink ) return;
+			if ( tagname === 'a' && attribs.href && attribs.class !== 'new' && /^(?:(?:https?:)?\/\/|\/|#)/.test(attribs.href) ) {
+				href = new URL(attribs.href, pagelink).href;
 				text += '[';
 			}
 		},
@@ -318,7 +318,7 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
 			if ( tagname === 'h4' ) text += '__';
 			if ( tagname === 'h5' ) text += '*';
 			if ( tagname === 'h6' ) text += '';
-			if ( !serverpath ) return;
+			if ( !pagelink ) return;
 			if ( tagname === 'a' && href ) {
 				if ( text.endsWith( '[' ) ) text = text.substring(0, text.length - 1);
 				else text += '](<' + href.replace( /[()]/g, '\\$&' ) + '>)';
@@ -326,7 +326,7 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
 			}
 		},
 		oncomment: (commenttext) => {
-			if ( serverpath && /^LINK'" \d+:\d+$/.test(commenttext) ) {
+			if ( pagelink && /^LINK'" \d+:\d+$/.test(commenttext) ) {
 				text += '*UNKNOWN LINK*';
 			}
 		}