reshape-contents-body.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * RegExp for Slack message header
  3. * @type {RegExp}
  4. * @see https://regex101.com/r/wk24Z0/1
  5. */
  6. const regexpMessageHeader = new RegExp(/.+\s\s[\d]{1,2}:[\d]{2}(\s[AP]{1}M)?$/);
  7. /**
  8. * RegExp for Slack message Time with/without AM, PM
  9. * @type {RegExp}
  10. * @see https://regex101.com/r/Tz3ZPG/1
  11. */
  12. const regexpTime = new RegExp(/\s\s[\d]{1,2}:[\d]{2}(\s[AP]{1}M)?$/);
  13. /**
  14. * RegExp for Slack message Time without AM, PM
  15. * @type {RegExp}
  16. * @see https://regex101.com/r/e1Yi6t/1
  17. */
  18. const regexpShortTime = new RegExp(/^[\d]{1,2}:[\d]{2}$/);
  19. /**
  20. * RegExp for Slack message reaction
  21. * @type {RegExp}
  22. * @see https://regex101.com/r/LQX3s2/1
  23. */
  24. const regexpReaction = new RegExp(/^:[+\w-]+:$/);
  25. // Remove everything before the first Header
  26. const devideLinesBeforeAfterFirstHeader = (lines: string[]) => {
  27. let i = 0;
  28. while (!regexpMessageHeader.test(lines[i]) && i <= lines.length) {
  29. i++;
  30. }
  31. const linesBeforeFirstHeader = lines.slice(0, i);
  32. const linesAfterFirstHeader = lines.slice(i);
  33. return { linesBeforeFirstHeader, linesAfterFirstHeader };
  34. };
  35. // Reshape linesAfterFirstHeader
  36. export const reshapeContentsBody = (str: string): string => {
  37. const splitted = str.split('\n');
  38. const { linesBeforeFirstHeader, linesAfterFirstHeader } = devideLinesBeforeAfterFirstHeader(splitted);
  39. if (linesAfterFirstHeader.length === 0) {
  40. return linesBeforeFirstHeader.join('\n');
  41. }
  42. let didReactionRemoved = false;
  43. const reshapedArray = linesAfterFirstHeader.map((line) => {
  44. let copyline = line;
  45. // Check 1: Did a reaction removed last time?
  46. if (didReactionRemoved) {
  47. // remove reaction count
  48. copyline = '';
  49. didReactionRemoved = false;
  50. }
  51. // Check 2: Is this line a header?
  52. else if (regexpMessageHeader.test(copyline)) {
  53. // extract time from line
  54. const matched = copyline.match(regexpTime);
  55. let time = '';
  56. if (matched !== null && matched.length > 0) {
  57. time = matched[0];
  58. }
  59. // ##*username* HH:mm AM
  60. copyline = '\n## **'.concat(copyline);
  61. copyline = copyline.replace(regexpTime, '**<span class="grw-keep-time">'.concat(time, '</span>\n'));
  62. }
  63. // Check 3: Is this line a short time(HH:mm)?
  64. else if (regexpShortTime.test(copyline)) {
  65. // --HH:mm--
  66. copyline = '--'.concat(copyline, '--');
  67. }
  68. // Check 4: Is this line a reaction?
  69. else if (regexpReaction.test(copyline)) {
  70. // remove reaction
  71. copyline = '';
  72. didReactionRemoved = true;
  73. }
  74. return copyline;
  75. });
  76. // remove all blanks
  77. const blanksRemoved = reshapedArray.filter(line => line !== '');
  78. // add <div> to the first line & add </div> to the last line
  79. blanksRemoved[0] = '\n<div class="grw-keep">\n'.concat(blanksRemoved[0]);
  80. blanksRemoved.push('</div>');
  81. // Add 2 spaces and 1 enter to all lines
  82. const completedArray = blanksRemoved.map(line => line.concat(' \n'));
  83. // join all
  84. const contentsBeforeFirstHeader = linesBeforeFirstHeader.join('');
  85. const contentsAfterFirstHeader = completedArray.join('');
  86. return contentsBeforeFirstHeader.concat(contentsAfterFirstHeader);
  87. };