extract_desc.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { escapeFormatting } from './functions.js';
  2. /**
  3. * Get the description for a page.
  4. * @param {String} [text] - The full page extract.
  5. * @param {String} [fragment] - The section title.
  6. * @returns {String[]}
  7. */
  8. export default function extract_desc(text = '', fragment = '') {
  9. var sectionIndex = text.indexOf('\ufffd\ufffd');
  10. var extract = escapeFormatting(( sectionIndex !== -1 ? text.substring(0, sectionIndex) : text ).trim());
  11. if ( extract.length > 1000 ) extract = extract.substring(0, 1000) + '\u2026';
  12. var section = null;
  13. var regex = /\ufffd{2}(\d)\ufffd{2}([^\n]+)/g;
  14. var sectionHeader = '';
  15. var sectionText = '';
  16. while ( fragment && ( section = regex.exec(text) ) !== null ) {
  17. if ( section[2].replace( / /g, '_' ) !== fragment.replace( / /g, '_' ) ) continue;
  18. sectionHeader = escapeFormatting(section[2]);
  19. if ( sectionHeader.length > 240 ) sectionHeader = sectionHeader.substring(0, 240) + '\u2026';
  20. sectionHeader = section_formatting(sectionHeader, section[1]);
  21. sectionText = text.substring(regex.lastIndex);
  22. switch ( section[1] ) {
  23. case '6':
  24. sectionIndex = sectionText.indexOf('\ufffd\ufffd6\ufffd\ufffd');
  25. if ( sectionIndex !== -1 ) sectionText = sectionText.substring(0, sectionIndex);
  26. case '5':
  27. sectionIndex = sectionText.indexOf('\ufffd\ufffd5\ufffd\ufffd');
  28. if ( sectionIndex !== -1 ) sectionText = sectionText.substring(0, sectionIndex);
  29. case '4':
  30. sectionIndex = sectionText.indexOf('\ufffd\ufffd4\ufffd\ufffd');
  31. if ( sectionIndex !== -1 ) sectionText = sectionText.substring(0, sectionIndex);
  32. case '3':
  33. sectionIndex = sectionText.indexOf('\ufffd\ufffd3\ufffd\ufffd');
  34. if ( sectionIndex !== -1 ) sectionText = sectionText.substring(0, sectionIndex);
  35. case '2':
  36. sectionIndex = sectionText.indexOf('\ufffd\ufffd2\ufffd\ufffd');
  37. if ( sectionIndex !== -1 ) sectionText = sectionText.substring(0, sectionIndex);
  38. case '1':
  39. sectionIndex = sectionText.indexOf('\ufffd\ufffd1\ufffd\ufffd');
  40. if ( sectionIndex !== -1 ) sectionText = sectionText.substring(0, sectionIndex);
  41. }
  42. sectionText = escapeFormatting(sectionText.trim()).replace( /\ufffd{2}(\d)\ufffd{2}([^\n]+)/g, (match, n, sectionTitle) => {
  43. return section_formatting(sectionTitle, n);
  44. } );
  45. if ( sectionText.length > 1000 ) sectionText = sectionText.substring(0, 1000) + '\u2026';
  46. break;
  47. }
  48. return [extract, sectionHeader, sectionText];
  49. }
  50. /**
  51. * Format section title.
  52. * @param {String} title - The section title.
  53. * @param {String} n - The header level.
  54. * @returns {String}
  55. */
  56. function section_formatting(title, n) {
  57. switch ( n ) {
  58. case '1':
  59. title = '***__' + title + '__***';
  60. break;
  61. case '2':
  62. title = '**__' + title + '__**';
  63. break;
  64. case '3':
  65. title = '**' + title + '**';
  66. break;
  67. case '4':
  68. title = '__' + title + '__';
  69. break;
  70. case '5':
  71. title = '*' + title + '*';
  72. break;
  73. }
  74. return title;
  75. }