MarkdownListUtil.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Utility for markdown list
  3. */
  4. class MarkdownListUtil {
  5. constructor() {
  6. // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
  7. // https://regex101.com/r/7BN2fR/5
  8. this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
  9. this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
  10. this.newlineAndIndentContinueMarkdownList = this.newlineAndIndentContinueMarkdownList.bind(this);
  11. this.pasteText = this.pasteText.bind(this);
  12. }
  13. /**
  14. * Self Implementation with AbstractEditor interface
  15. * @param {AbstractEditor} editor An instance of AbstractEditor
  16. */
  17. newlineAndIndentContinueMarkdownList(editor) {
  18. const strFromBol = editor.getStrFromBol();
  19. if (this.indentAndMarkOnlyRE.test(strFromBol)) {
  20. // clear current line and end list
  21. editor.replaceBolToCurrentPos('\n');
  22. }
  23. else if (this.indentAndMarkRE.test(strFromBol)) {
  24. // continue list
  25. const indentAndMark = strFromBol.match(this.indentAndMarkRE)[0];
  26. editor.insertText(`\n${indentAndMark}`);
  27. }
  28. else {
  29. editor.insertLinebreak();
  30. }
  31. }
  32. /**
  33. * paste text
  34. * @param {AbstractEditor} editor An instance of AbstractEditor
  35. * @param {any} event
  36. * @param {string} text
  37. */
  38. pasteText(editor, event, text) {
  39. // get strings from BOL(beginning of line) to current position
  40. const strFromBol = editor.getStrFromBol();
  41. // when match indentAndMarkOnlyRE
  42. // (this means the current position is the beginning of the list item)
  43. if (this.indentAndMarkOnlyRE.test(strFromBol)) {
  44. const adjusted = this.adjustPastedData(strFromBol, text);
  45. // replace
  46. if (adjusted != null) {
  47. event.preventDefault();
  48. editor.replaceBolToCurrentPos(adjusted);
  49. }
  50. }
  51. }
  52. /**
  53. * return adjusted pasted data by indentAndMark
  54. *
  55. * @param {string} indentAndMark
  56. * @param {string} text
  57. * @returns adjusted pasted data
  58. * returns null when adjustment is not necessary
  59. */
  60. adjustPastedData(indentAndMark, text) {
  61. let adjusted = null;
  62. // list data (starts with indent and mark)
  63. if (text.match(this.indentAndMarkRE)) {
  64. const indent = indentAndMark.match(this.indentAndMarkRE)[1];
  65. // splice to an array of line
  66. const lines = text.match(/[^\r\n]+/g);
  67. // indent
  68. const replacedLines = lines.map((line) => {
  69. return indent + line;
  70. });
  71. adjusted = replacedLines.join('\n');
  72. }
  73. // listful data
  74. else if (this.isListfulData(text)) {
  75. // do nothing (return null)
  76. }
  77. // not listful data
  78. else {
  79. // append `indentAndMark` at the beginning of all lines (except the first line)
  80. const replacedText = text.replace(/(\r\n|\r|\n)/g, '$1' + indentAndMark);
  81. // append `indentAndMark` to the first line
  82. adjusted = indentAndMark + replacedText;
  83. }
  84. return adjusted;
  85. }
  86. /**
  87. * evaluate whether `text` is list like data or not
  88. * @param {string} text
  89. */
  90. isListfulData(text) {
  91. // return false if includes at least one blank line
  92. // see https://stackoverflow.com/a/16369725
  93. if (text.match(/^\s*[\r\n]/m) != null) {
  94. return false;
  95. }
  96. const lines = text.match(/[^\r\n]+/g);
  97. // count lines that starts with indent and mark
  98. let isListful = false;
  99. let count = 0;
  100. lines.forEach((line) => {
  101. if (line.match(this.indentAndMarkRE)) {
  102. count++;
  103. }
  104. // ensure to be true if it is 50% or more
  105. if (count >= lines.length / 2) {
  106. isListful = true;
  107. return;
  108. }
  109. });
  110. return isListful;
  111. }
  112. }
  113. // singleton pattern
  114. const instance = new MarkdownListUtil();
  115. Object.freeze(instance);
  116. export default instance;