|
@@ -142,7 +142,7 @@ function toMarkdown(text = '', wiki, title = '', fullWikitext = false) {
|
|
function toPlaintext(text = '', fullWikitext = false) {
|
|
function toPlaintext(text = '', fullWikitext = false) {
|
|
text = text.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' );
|
|
text = text.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' );
|
|
if ( fullWikitext ) {
|
|
if ( fullWikitext ) {
|
|
- return htmlToPlain( text.replace( /\[(?:https?:)?\/\/(?:[^ ]+) ([^\]]+)\]/g, '$1' ) );
|
|
|
|
|
|
+ return htmlToDiscord( text.replace( /\[(?:https?:)?\/\/(?:[^ ]+) ([^\]]+)\]/g, '$1' ) );
|
|
}
|
|
}
|
|
else return escapeFormatting(text);
|
|
else return escapeFormatting(text);
|
|
};
|
|
};
|
|
@@ -155,100 +155,15 @@ function toPlaintext(text = '', fullWikitext = false) {
|
|
function htmlToPlain(html) {
|
|
function htmlToPlain(html) {
|
|
var text = '';
|
|
var text = '';
|
|
var reference = false;
|
|
var reference = false;
|
|
- var listlevel = -1;
|
|
|
|
var parser = new htmlparser.Parser( {
|
|
var parser = new htmlparser.Parser( {
|
|
onopentag: (tagname, attribs) => {
|
|
onopentag: (tagname, attribs) => {
|
|
if ( tagname === 'sup' && attribs.class === 'reference' ) reference = true;
|
|
if ( tagname === 'sup' && attribs.class === 'reference' ) reference = true;
|
|
- if ( tagname === 'br' ) {
|
|
|
|
- text += '\n';
|
|
|
|
- if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel + 3);
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'hr' ) {
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '─'.repeat(10) + '\n';
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'p' && !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- if ( tagname === 'ul' ) listlevel++;
|
|
|
|
- if ( tagname === 'li' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- if ( attribs.class !== 'mw-empty-elt' ) {
|
|
|
|
- if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel);
|
|
|
|
- text += '• ';
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'dl' ) listlevel++;
|
|
|
|
- if ( tagname === 'dt' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- if ( listlevel > -1 && attribs.class !== 'mw-empty-elt' ) text += '\u200b '.repeat(4 * listlevel);
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'dd' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- if ( listlevel > -1 && attribs.class !== 'mw-empty-elt' ) text += '\u200b '.repeat(4 * (listlevel + 1));
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'img' ) {
|
|
|
|
- if ( attribs.alt && attribs.src && !reference ) {
|
|
|
|
- let showAlt = true;
|
|
|
|
- if ( attribs['data-image-name'] === attribs.alt ) showAlt = false;
|
|
|
|
- else {
|
|
|
|
- let regex = new RegExp( '/([\\da-f])/\\1[\\da-f]/' + attribs.alt.replace( / /g, '_' ).replace( /\W/g, '\\$&' ) + '(?:/|\\?|$)' );
|
|
|
|
- if ( attribs.src.startsWith( 'data:' ) && attribs['data-src'] ) attribs.src = attribs['data-src'];
|
|
|
|
- if ( regex.test(attribs.src.replace( /(?:%[\dA-F]{2})+/g, partialURIdecode )) ) showAlt = false;
|
|
|
|
- }
|
|
|
|
- if ( showAlt ) text += escapeFormatting(attribs.alt);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'h1' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '***__';
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'h2' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '**__';
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'h3' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '**';
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'h4' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '__';
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'h5' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '*';
|
|
|
|
- }
|
|
|
|
- if ( tagname === 'h6' ) {
|
|
|
|
- text = text.replace( / +$/, '' );
|
|
|
|
- if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
|
|
- text += '';
|
|
|
|
- }
|
|
|
|
},
|
|
},
|
|
ontext: (htmltext) => {
|
|
ontext: (htmltext) => {
|
|
- if ( !reference ) {
|
|
|
|
- text += escapeFormatting(htmltext);
|
|
|
|
- }
|
|
|
|
|
|
+ if ( !reference ) text += escapeFormatting(htmltext);
|
|
},
|
|
},
|
|
onclosetag: (tagname) => {
|
|
onclosetag: (tagname) => {
|
|
if ( tagname === 'sup' ) reference = false;
|
|
if ( tagname === 'sup' ) reference = false;
|
|
- if ( tagname === 'ul' ) listlevel--;
|
|
|
|
- if ( tagname === 'dl' ) 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) => {
|
|
|
|
- if ( /^LINK'" \d+:\d+$/.test(commenttext) ) text += '*UNKNOWN LINK*';
|
|
|
|
}
|
|
}
|
|
} );
|
|
} );
|
|
parser.write( html );
|
|
parser.write( html );
|
|
@@ -276,11 +191,14 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
|
|
code = true;
|
|
code = true;
|
|
text += '`';
|
|
text += '`';
|
|
}
|
|
}
|
|
|
|
+ if ( tagname === 'pre' ) {
|
|
|
|
+ code = true;
|
|
|
|
+ text += '```\n';
|
|
|
|
+ }
|
|
if ( tagname === 'b' ) text += '**';
|
|
if ( tagname === 'b' ) text += '**';
|
|
if ( tagname === 'i' ) text += '*';
|
|
if ( tagname === 'i' ) text += '*';
|
|
if ( tagname === 's' ) text += '~~';
|
|
if ( tagname === 's' ) text += '~~';
|
|
if ( tagname === 'u' ) text += '__';
|
|
if ( tagname === 'u' ) text += '__';
|
|
- if ( !serverpath ) return;
|
|
|
|
if ( tagname === 'sup' && attribs.class === 'reference' ) reference = true;
|
|
if ( tagname === 'sup' && attribs.class === 'reference' ) reference = true;
|
|
if ( tagname === 'br' ) {
|
|
if ( tagname === 'br' ) {
|
|
text += '\n';
|
|
text += '\n';
|
|
@@ -361,6 +279,7 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
|
|
if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
if ( !text.endsWith( '\n' ) ) text += '\n';
|
|
text += '';
|
|
text += '';
|
|
}
|
|
}
|
|
|
|
+ if ( !serverpath ) return;
|
|
if ( tagname === 'a' && attribs.href && attribs.class !== 'new' && /^(?:(?:https?:)?\/)?\//.test(attribs.href) ) {
|
|
if ( tagname === 'a' && attribs.href && attribs.class !== 'new' && /^(?:(?:https?:)?\/)?\//.test(attribs.href) ) {
|
|
href = new URL(attribs.href, serverpath).href;
|
|
href = new URL(attribs.href, serverpath).href;
|
|
text += '[';
|
|
text += '[';
|
|
@@ -379,13 +298,16 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
|
|
code = false;
|
|
code = false;
|
|
text += '`';
|
|
text += '`';
|
|
}
|
|
}
|
|
|
|
+ if ( tagname === 'pre' ) {
|
|
|
|
+ code = false;
|
|
|
|
+ text += '\n```';
|
|
|
|
+ }
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if ( tagname === 'b' ) text += '**';
|
|
if ( tagname === 'b' ) text += '**';
|
|
if ( tagname === 'i' ) text += '*';
|
|
if ( tagname === 'i' ) text += '*';
|
|
if ( tagname === 's' ) text += '~~';
|
|
if ( tagname === 's' ) text += '~~';
|
|
if ( tagname === 'u' ) text += '__';
|
|
if ( tagname === 'u' ) text += '__';
|
|
- if ( !serverpath ) return;
|
|
|
|
if ( tagname === 'sup' ) reference = false;
|
|
if ( tagname === 'sup' ) reference = false;
|
|
if ( tagname === 'ul' ) listlevel--;
|
|
if ( tagname === 'ul' ) listlevel--;
|
|
if ( tagname === 'dl' ) listlevel--;
|
|
if ( tagname === 'dl' ) listlevel--;
|
|
@@ -396,6 +318,7 @@ function htmlToDiscord(html, serverpath = '', ...escapeArgs) {
|
|
if ( tagname === 'h4' ) text += '__';
|
|
if ( tagname === 'h4' ) text += '__';
|
|
if ( tagname === 'h5' ) text += '*';
|
|
if ( tagname === 'h5' ) text += '*';
|
|
if ( tagname === 'h6' ) text += '';
|
|
if ( tagname === 'h6' ) text += '';
|
|
|
|
+ if ( !serverpath ) return;
|
|
if ( tagname === 'a' && href ) {
|
|
if ( tagname === 'a' && href ) {
|
|
if ( text.endsWith( '[' ) ) text = text.substring(0, text.length - 1);
|
|
if ( text.endsWith( '[' ) ) text = text.substring(0, text.length - 1);
|
|
else text += '](<' + href.replace( /[()]/g, '\\$&' ) + '>)';
|
|
else text += '](<' + href.replace( /[()]/g, '\\$&' ) + '>)';
|