functions.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const htmlparser = require('htmlparser2');
  2. const got = require('got').extend( {
  3. throwHttpErrors: false,
  4. timeout: 5000,
  5. headers: {
  6. 'User-Agent': 'Wiki-Bot/' + ( isDebug ? 'testing' : process.env.npm_package_version ) + ' (Discord; ' + process.env.npm_package_name + ')'
  7. },
  8. responseType: 'json'
  9. } );
  10. /**
  11. * Make wikitext formatting usage.
  12. * @param {String} [text] - The text to modify.
  13. * @param {Boolean} [showEmbed] - If the text is used in an embed.
  14. * @param {import('./wiki.js')|String} [args] - The text contains markdown links.
  15. * @returns {String}
  16. */
  17. function toFormatting(text = '', showEmbed = false, ...args) {
  18. if ( showEmbed ) return toMarkdown(text, ...args);
  19. else return toPlaintext(text);
  20. };
  21. /**
  22. * Turns wikitext formatting into markdown.
  23. * @param {String} [text] - The text to modify.
  24. * @param {import('./wiki.js')} [wiki] - The wiki.
  25. * @param {String} [title] - The page title.
  26. * @returns {String}
  27. */
  28. function toMarkdown(text = '', wiki, title = '') {
  29. text = text.replace( /[()\\]/g, '\\$&' );
  30. var link = null;
  31. var regex = /\[\[(?:([^\|\]]+)\|)?([^\]]+)\]\]([a-z]*)/g;
  32. while ( ( link = regex.exec(text) ) !== null ) {
  33. var pagetitle = ( link[1] || link[2] );
  34. var page = wiki.toLink(( /^[#\/]/.test(pagetitle) ? title + ( pagetitle.startsWith( '/' ) ? pagetitle : '' ) : pagetitle ), '', ( pagetitle.startsWith( '#' ) ? pagetitle.substring(1) : '' ), true);
  35. text = text.replaceSave( link[0], '[' + link[2] + link[3] + '](' + page + ')' );
  36. }
  37. regex = /\/\*\s*([^\*]+?)\s*\*\/\s*(.)?/g;
  38. while ( title !== '' && ( link = regex.exec(text) ) !== null ) {
  39. text = text.replaceSave( link[0], '[→' + link[1] + '](' + wiki.toLink(title, '', link[1], true) + ')' + ( link[2] ? ': ' + link[2] : '' ) );
  40. }
  41. return escapeFormatting(text, true);
  42. };
  43. /**
  44. * Removes wikitext formatting.
  45. * @param {String} [text] - The text to modify.
  46. * @param {Boolean} [canHTML] - If the text can contain HTML.
  47. * @returns {String}
  48. */
  49. function toPlaintext(text = '', fullWikitext = false) {
  50. text = text.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' );
  51. if ( fullWikitext ) {
  52. return htmlToPlain( text.replace( /\[(?:https?:)?\/\/(?:[^ ]+) ([^\]]+)\]/g, '$1' ) );
  53. }
  54. else return escapeFormatting(text);
  55. };
  56. /**
  57. * Change HTML text to plain text.
  58. * @param {String} html - The text in HTML.
  59. * @returns {String}
  60. */
  61. function htmlToPlain(html) {
  62. var text = '';
  63. var parser = new htmlparser.Parser( {
  64. ontext: (htmltext) => {
  65. text += escapeFormatting(htmltext);
  66. }
  67. } );
  68. parser.write( html );
  69. parser.end();
  70. return text;
  71. };
  72. /**
  73. * Change HTML text to markdown text.
  74. * @param {String} html - The text in HTML.
  75. * @returns {String}
  76. */
  77. function htmlToDiscord(html) {
  78. var text = '';
  79. var parser = new htmlparser.Parser( {
  80. onopentag: (tagname, attribs) => {
  81. switch (tagname) {
  82. case 'b':
  83. text += '**';
  84. break;
  85. case 'i':
  86. text += '*';
  87. break;
  88. case 's':
  89. text += '~~';
  90. break;
  91. case 'u':
  92. text += '__';
  93. break;
  94. }
  95. },
  96. ontext: (htmltext) => {
  97. text += escapeFormatting(htmltext);
  98. },
  99. onclosetag: (tagname) => {
  100. switch (tagname) {
  101. case 'b':
  102. text += '**';
  103. break;
  104. case 'i':
  105. text += '*';
  106. break;
  107. case 's':
  108. text += '~~';
  109. break;
  110. case 'u':
  111. text += '__';
  112. break;
  113. }
  114. }
  115. } );
  116. parser.write( html );
  117. parser.end();
  118. return text;
  119. };
  120. /**
  121. * Escapes formatting.
  122. * @param {String} [text] - The text to modify.
  123. * @param {Boolean} [isMarkdown] - The text contains markdown links.
  124. * @returns {String}
  125. */
  126. function escapeFormatting(text = '', isMarkdown = false) {
  127. if ( !isMarkdown ) text = text.replace( /[()\\]/g, '\\$&' );
  128. return text.replace( /[`_*~:<>{}@|]|\/\//g, '\\$&' );
  129. };
  130. module.exports = {
  131. got,
  132. toFormatting,
  133. toMarkdown,
  134. toPlaintext,
  135. htmlToPlain,
  136. htmlToDiscord,
  137. escapeFormatting
  138. };