|
@@ -8,26 +8,64 @@ const got = require('got').extend( {
|
|
responseType: 'json'
|
|
responseType: 'json'
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Parse infobox content
|
|
|
|
+ * @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.
|
|
|
|
+ */
|
|
|
|
+function parse_infobox(infobox, embed, thumbnail) {
|
|
|
|
+ if ( embed.fields.length >= 25 ) return;
|
|
|
|
+ switch ( infobox.type ) {
|
|
|
|
+ case 'data':
|
|
|
|
+ var {label = '', value = ''} = infobox.data;
|
|
|
|
+ label = htmlToPlain(label).trim();
|
|
|
|
+ value = htmlToPlain(value).trim();
|
|
|
|
+ if ( label && value ) embed.addField( label, value, true );
|
|
|
|
+ break;
|
|
|
|
+ case 'group':
|
|
|
|
+ infobox.data.value.forEach( group => {
|
|
|
|
+ parse_infobox(group, embed, thumbnail);
|
|
|
|
+ } );
|
|
|
|
+ break;
|
|
|
|
+ case 'header':
|
|
|
|
+ var {value = ''} = infobox.data;
|
|
|
|
+ value = htmlToPlain(value).trim();
|
|
|
|
+ if ( value ) embed.addField( '\u200b', '__**' + value + '**__', false );
|
|
|
|
+ break;
|
|
|
|
+ case 'image':
|
|
|
|
+ if ( embed.thumbnail?.url !== thumbnail ) return;
|
|
|
|
+ var image = infobox.data.find( img => {
|
|
|
|
+ return ( /^(?:https?:)?\/\//.test(img.url) && /\.(?:png|jpg|jpeg|gif)$/.test(img.name) );
|
|
|
|
+ } );
|
|
|
|
+ if ( image ) embed.setThumbnail( image.url );
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Make wikitext formatting usage.
|
|
* Make wikitext formatting usage.
|
|
* @param {String} [text] - The text to modify.
|
|
* @param {String} [text] - The text to modify.
|
|
* @param {Boolean} [showEmbed] - If the text is used in an embed.
|
|
* @param {Boolean} [showEmbed] - If the text is used in an embed.
|
|
- * @param {import('./wiki.js')|String} [args] - The text contains markdown links.
|
|
|
|
|
|
+ * @param {import('./wiki.js')} [wiki] - The wiki.
|
|
|
|
+ * @param {String} [title] - The page title.
|
|
|
|
+ * @param {Boolean} [fullWikitext] - If the text can contain full wikitext.
|
|
* @returns {String}
|
|
* @returns {String}
|
|
*/
|
|
*/
|
|
-function toFormatting(text = '', showEmbed = false, ...args) {
|
|
|
|
- if ( showEmbed ) return toMarkdown(text, ...args);
|
|
|
|
- else return toPlaintext(text);
|
|
|
|
|
|
+function toFormatting(text = '', showEmbed = false, wiki, title = '', fullWikitext = false) {
|
|
|
|
+ if ( showEmbed ) return toMarkdown(text, wiki, title, fullWikitext);
|
|
|
|
+ else return toPlaintext(text, fullWikitext);
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
/**
|
|
* Turns wikitext formatting into markdown.
|
|
* Turns wikitext formatting into markdown.
|
|
* @param {String} [text] - The text to modify.
|
|
* @param {String} [text] - The text to modify.
|
|
- * @param {import('./wiki.js')} [wiki] - The wiki.
|
|
|
|
|
|
+ * @param {import('./wiki.js')} wiki - The wiki.
|
|
* @param {String} [title] - The page title.
|
|
* @param {String} [title] - The page title.
|
|
|
|
+ * @param {Boolean} [fullWikitext] - If the text can contain full wikitext.
|
|
* @returns {String}
|
|
* @returns {String}
|
|
*/
|
|
*/
|
|
-function toMarkdown(text = '', wiki, title = '') {
|
|
|
|
|
|
+function toMarkdown(text = '', wiki, title = '', fullWikitext = false) {
|
|
text = text.replace( /[()\\]/g, '\\$&' );
|
|
text = text.replace( /[()\\]/g, '\\$&' );
|
|
var link = null;
|
|
var link = null;
|
|
var regex = /\[\[(?:([^\|\]]+)\|)?([^\]]+)\]\]([a-z]*)/g;
|
|
var regex = /\[\[(?:([^\|\]]+)\|)?([^\]]+)\]\]([a-z]*)/g;
|
|
@@ -36,9 +74,18 @@ function toMarkdown(text = '', wiki, title = '') {
|
|
var page = wiki.toLink(( /^[#\/]/.test(pagetitle) ? title + ( pagetitle.startsWith( '/' ) ? pagetitle : '' ) : pagetitle ), '', ( pagetitle.startsWith( '#' ) ? pagetitle.substring(1) : '' ), true);
|
|
var page = wiki.toLink(( /^[#\/]/.test(pagetitle) ? title + ( pagetitle.startsWith( '/' ) ? pagetitle : '' ) : pagetitle ), '', ( pagetitle.startsWith( '#' ) ? pagetitle.substring(1) : '' ), true);
|
|
text = text.replaceSave( link[0], '[' + link[2] + link[3] + '](' + page + ')' );
|
|
text = text.replaceSave( link[0], '[' + link[2] + link[3] + '](' + page + ')' );
|
|
}
|
|
}
|
|
- regex = /\/\*\s*([^\*]+?)\s*\*\/\s*(.)?/g;
|
|
|
|
- while ( title !== '' && ( link = regex.exec(text) ) !== null ) {
|
|
|
|
- text = text.replaceSave( link[0], '[→' + link[1] + '](' + wiki.toLink(title, '', link[1], true) + ')' + ( link[2] ? ': ' + link[2] : '' ) );
|
|
|
|
|
|
+ if ( title !== '' ) {
|
|
|
|
+ regex = /\/\*\s*([^\*]+?)\s*\*\/\s*(.)?/g;
|
|
|
|
+ while ( ( link = regex.exec(text) ) !== null ) {
|
|
|
|
+ text = text.replaceSave( link[0], '[→' + link[1] + '](' + wiki.toLink(title, '', link[1], true) + ')' + ( link[2] ? ': ' + link[2] : '' ) );
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if ( fullWikitext ) {
|
|
|
|
+ regex = /\[(?:https?:)?\/\/([^ ]+) ([^\]]+)\]/g;
|
|
|
|
+ while ( ( link = regex.exec(text) ) !== null ) {
|
|
|
|
+ text = text.replaceSave( link[0], '[' + link[2] + '](https://' + link[1] + ')' );
|
|
|
|
+ }
|
|
|
|
+ return htmlToDiscord( text, true, true ).replaceSave( /'''/g, '**' ).replaceSave( /''/g, '*' );
|
|
}
|
|
}
|
|
return escapeFormatting(text, true);
|
|
return escapeFormatting(text, true);
|
|
};
|
|
};
|
|
@@ -46,7 +93,7 @@ function toMarkdown(text = '', wiki, title = '') {
|
|
/**
|
|
/**
|
|
* Removes wikitext formatting.
|
|
* Removes wikitext formatting.
|
|
* @param {String} [text] - The text to modify.
|
|
* @param {String} [text] - The text to modify.
|
|
- * @param {Boolean} [canHTML] - If the text can contain HTML.
|
|
|
|
|
|
+ * @param {Boolean} [fullWikitext] - If the text can contain full wikitext.
|
|
* @returns {String}
|
|
* @returns {String}
|
|
*/
|
|
*/
|
|
function toPlaintext(text = '', fullWikitext = false) {
|
|
function toPlaintext(text = '', fullWikitext = false) {
|
|
@@ -64,9 +111,17 @@ function toPlaintext(text = '', fullWikitext = false) {
|
|
*/
|
|
*/
|
|
function htmlToPlain(html) {
|
|
function htmlToPlain(html) {
|
|
var text = '';
|
|
var text = '';
|
|
|
|
+ var reference = false;
|
|
var parser = new htmlparser.Parser( {
|
|
var parser = new htmlparser.Parser( {
|
|
|
|
+ onopentag: (tagname, attribs) => {
|
|
|
|
+ if ( tagname === 'sup' && attribs.class === 'reference' ) reference = true;
|
|
|
|
+ if ( tagname === 'br' ) text += '\n';
|
|
|
|
+ },
|
|
ontext: (htmltext) => {
|
|
ontext: (htmltext) => {
|
|
- text += escapeFormatting(htmltext);
|
|
|
|
|
|
+ if ( !reference ) text += escapeFormatting(htmltext);
|
|
|
|
+ },
|
|
|
|
+ onclosetag: (tagname) => {
|
|
|
|
+ if ( tagname === 'sup' ) reference = false;
|
|
}
|
|
}
|
|
} );
|
|
} );
|
|
parser.write( html );
|
|
parser.write( html );
|
|
@@ -77,9 +132,10 @@ function htmlToPlain(html) {
|
|
/**
|
|
/**
|
|
* Change HTML text to markdown text.
|
|
* Change HTML text to markdown text.
|
|
* @param {String} html - The text in HTML.
|
|
* @param {String} html - The text in HTML.
|
|
|
|
+ * @param {Boolean[]} [escapeArgs] - Arguments for the escaping of text formatting.
|
|
* @returns {String}
|
|
* @returns {String}
|
|
*/
|
|
*/
|
|
-function htmlToDiscord(html) {
|
|
|
|
|
|
+function htmlToDiscord(html, ...escapeArgs) {
|
|
var text = '';
|
|
var text = '';
|
|
var parser = new htmlparser.Parser( {
|
|
var parser = new htmlparser.Parser( {
|
|
onopentag: (tagname, attribs) => {
|
|
onopentag: (tagname, attribs) => {
|
|
@@ -99,7 +155,7 @@ function htmlToDiscord(html) {
|
|
}
|
|
}
|
|
},
|
|
},
|
|
ontext: (htmltext) => {
|
|
ontext: (htmltext) => {
|
|
- text += escapeFormatting(htmltext);
|
|
|
|
|
|
+ text += escapeFormatting(htmltext, ...escapeArgs);
|
|
},
|
|
},
|
|
onclosetag: (tagname) => {
|
|
onclosetag: (tagname) => {
|
|
switch (tagname) {
|
|
switch (tagname) {
|
|
@@ -127,15 +183,18 @@ function htmlToDiscord(html) {
|
|
* Escapes formatting.
|
|
* Escapes formatting.
|
|
* @param {String} [text] - The text to modify.
|
|
* @param {String} [text] - The text to modify.
|
|
* @param {Boolean} [isMarkdown] - The text contains markdown links.
|
|
* @param {Boolean} [isMarkdown] - The text contains markdown links.
|
|
|
|
+ * @param {Boolean} [keepLinks] - Don't escape non-markdown links.
|
|
* @returns {String}
|
|
* @returns {String}
|
|
*/
|
|
*/
|
|
-function escapeFormatting(text = '', isMarkdown = false) {
|
|
|
|
|
|
+function escapeFormatting(text = '', isMarkdown = false, keepLinks = false) {
|
|
if ( !isMarkdown ) text = text.replace( /[()\\]/g, '\\$&' );
|
|
if ( !isMarkdown ) text = text.replace( /[()\\]/g, '\\$&' );
|
|
- return text.replace( /[`_*~:<>{}@|]|\/\//g, '\\$&' );
|
|
|
|
|
|
+ if ( !keepLinks ) text = text.replace( /\/\//g, '\\$&' );
|
|
|
|
+ return text.replace( /[`_*~:<>{}@|]/g, '\\$&' );
|
|
};
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|
|
got,
|
|
got,
|
|
|
|
+ parse_infobox,
|
|
toFormatting,
|
|
toFormatting,
|
|
toMarkdown,
|
|
toMarkdown,
|
|
toPlaintext,
|
|
toPlaintext,
|