edit_diff.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const htmlparser = require('htmlparser2');
  2. const {escapeFormatting} = require('./functions.js');
  3. /**
  4. * Change edit diffs to markdown text.
  5. * @param {String} html - The edit diff in HTML.
  6. * @param {String} more - The localized string for more content.
  7. * @param {String} whitespace - The localized string for only whitespace.
  8. * @returns {String[]}
  9. */
  10. function diffParser(html, more, whitespace) {
  11. var current_tag = '';
  12. var small_prev_ins = '';
  13. var small_prev_del = '';
  14. var ins_length = more.length;
  15. var del_length = more.length;
  16. var added = false;
  17. var parser = new htmlparser.Parser( {
  18. onopentag: (tagname, attribs) => {
  19. if ( tagname === 'ins' || tagname == 'del' ) current_tag = tagname;
  20. if ( tagname === 'td' && attribs.class === 'diff-addedline' ) current_tag = tagname+'a';
  21. if ( tagname === 'td' && attribs.class === 'diff-deletedline' ) current_tag = tagname+"d";
  22. if ( tagname === 'td' && attribs.class === 'diff-marker' ) added = true;
  23. },
  24. ontext: (htmltext) => {
  25. if ( current_tag === 'ins' && ins_length <= 1000 ) {
  26. ins_length += ( '**' + escapeFormatting(htmltext) + '**' ).length;
  27. if ( ins_length <= 1000 ) small_prev_ins += '**' + escapeFormatting(htmltext) + '**';
  28. else small_prev_ins += more;
  29. }
  30. if ( current_tag === 'del' && del_length <= 1000 ) {
  31. del_length += ( '~~' + escapeFormatting(htmltext) + '~~' ).length;
  32. if ( del_length <= 1000 ) small_prev_del += '~~' + escapeFormatting(htmltext) + '~~';
  33. else small_prev_del += more;
  34. }
  35. if ( ( current_tag === 'afterins' || current_tag === 'tda') && ins_length <= 1000 ) {
  36. ins_length += escapeFormatting(htmltext).length;
  37. if ( ins_length <= 1000 ) small_prev_ins += escapeFormatting(htmltext);
  38. else small_prev_ins += more;
  39. }
  40. if ( ( current_tag === 'afterdel' || current_tag === 'tdd') && del_length <= 1000 ) {
  41. del_length += escapeFormatting(htmltext).length;
  42. if ( del_length <= 1000 ) small_prev_del += escapeFormatting(htmltext);
  43. else small_prev_del += more;
  44. }
  45. if ( added ) {
  46. if ( htmltext === '+' && ins_length <= 1000 ) {
  47. ins_length++;
  48. if ( ins_length <= 1000 ) small_prev_ins += '\n';
  49. else small_prev_ins += more;
  50. }
  51. if ( htmltext === '−' && del_length <= 1000 ) {
  52. del_length++;
  53. if ( del_length <= 1000 ) small_prev_del += '\n';
  54. else small_prev_del += more;
  55. }
  56. added = false;
  57. }
  58. },
  59. onclosetag: (tagname) => {
  60. if ( tagname === 'ins' ) {
  61. current_tag = 'afterins';
  62. } else if ( tagname === 'del' ) {
  63. current_tag = 'afterdel';
  64. } else {
  65. current_tag = '';
  66. }
  67. }
  68. } );
  69. parser.write( html );
  70. parser.end();
  71. var compare = ['', ''];
  72. if ( small_prev_del.length ) {
  73. if ( small_prev_del.replace( /\~\~/g, '' ).trim().length ) {
  74. compare[0] = small_prev_del.replace( /\~\~\~\~/g, '' );
  75. } else compare[0] = whitespace;
  76. }
  77. if ( small_prev_ins.length ) {
  78. if ( small_prev_ins.replace( /\*\*/g, '' ).trim().length ) {
  79. compare[1] = small_prev_ins.replace( /\*\*\*\*/g, '' );
  80. } else compare[1] = whitespace;
  81. }
  82. return compare;
  83. }
  84. module.exports = diffParser;