MarkdownListHelper.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import * as codemirror from 'codemirror';
  2. class MarkdownListHelper {
  3. constructor() {
  4. // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
  5. // https://regex101.com/r/7BN2fR/5
  6. this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
  7. this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
  8. this.newlineAndIndentContinueMarkdownList = this.newlineAndIndentContinueMarkdownList.bind(this);
  9. this.pasteText = this.pasteText.bind(this);
  10. this.getBol = this.getBol.bind(this);
  11. this.getEol = this.getEol.bind(this);
  12. this.getStrFromBol = this.getStrFromBol.bind(this);
  13. this.getStrToEol = this.getStrToEol.bind(this);
  14. }
  15. /**
  16. * return whether context is matched by list
  17. * @param {any} editor An editor instance of CodeMirror
  18. */
  19. isMatchedContext(editor) {
  20. console.log('MarkdownListHelper.isMatchedContext');
  21. // get strings from BOL(beginning of line) to current position
  22. const strFromBol = this.getStrFromBol(editor);
  23. const strToEol = this.getStrToEol(editor);
  24. console.log('strToEol: ' + strToEol);
  25. console.log('strFromBol: ' + strFromBol);
  26. console.log('will return ' + (this.indentAndMarkRE.test(strToEol)
  27. || this.indentAndMarkRE.test(strFromBol)
  28. || this.indentAndMarkOnlyRE.test(strFromBol) ? 'true' : 'false'));
  29. return this.indentAndMarkRE.test(strToEol)
  30. || this.indentAndMarkRE.test(strFromBol)
  31. || this.indentAndMarkOnlyRE.test(strFromBol);
  32. }
  33. /**
  34. * handle new line
  35. * @param {any} editor An editor instance of CodeMirror
  36. */
  37. handleNewLine(editor) {
  38. console.log('MarkdownListHelper.handleNewLine');
  39. this.newlineAndIndentContinueMarkdownList(editor);
  40. }
  41. /**
  42. * wrap codemirror.commands.newlineAndIndentContinueMarkdownList
  43. * @param {any} editor An editor instance of CodeMirror
  44. */
  45. newlineAndIndentContinueMarkdownList(editor) {
  46. console.log('MarkdownListHelper.newlineAndIndentContinueMarkdownList');
  47. // get strings from current position to EOL(end of line) before break the line
  48. const strToEol = this.getStrToEol(editor);
  49. if (this.indentAndMarkRE.test(strToEol)) {
  50. console.log('MarkdownListHelper.newlineAndIndentContinueMarkdownList: abort auto indent');
  51. codemirror.commands.newlineAndIndent(editor);
  52. // replace the line with strToEol (abort auto indent)
  53. editor.getDoc().replaceRange(strToEol, this.getBol(editor), this.getEol(editor));
  54. }
  55. else {
  56. console.log('MarkdownListHelper.newlineAndIndentContinueMarkdownList: will auto indent');
  57. codemirror.commands.newlineAndIndentContinueMarkdownList(editor);
  58. }
  59. }
  60. /**
  61. * paste text
  62. * @param {any} editor An editor instance of CodeMirror
  63. * @param {any} event
  64. * @param {string} text
  65. */
  66. pasteText(editor, event, text) {
  67. // get strings from BOL(beginning of line) to current position
  68. const strFromBol = this.getStrFromBol(editor);
  69. const matched = strFromBol.match(this.indentAndMarkRE);
  70. // when match indentAndMarkOnlyRE
  71. // (this means the current position is the beginning of the list item)
  72. if (this.indentAndMarkOnlyRE.test(strFromBol)) {
  73. const adjusted = this.adjustPastedData(strFromBol, text);
  74. // replace
  75. if (adjusted != null) {
  76. event.preventDefault();
  77. editor.getDoc().replaceRange(adjusted, this.getBol(editor), editor.getCursor());
  78. }
  79. }
  80. }
  81. /**
  82. * return adjusted pasted data by indentAndMark
  83. *
  84. * @param {string} indentAndMark
  85. * @param {string} text
  86. * @returns adjusted pasted data
  87. * returns null when adjustment is not necessary
  88. */
  89. adjustPastedData(indentAndMark, text) {
  90. let adjusted = null;
  91. // list data (starts with indent and mark)
  92. if (text.match(this.indentAndMarkRE)) {
  93. const indent = indentAndMark.match(this.indentAndMarkRE)[1];
  94. // splice to an array of line
  95. const lines = text.match(/[^\r\n]+/g);
  96. // indent
  97. const replacedLines = lines.map((line) => {
  98. return indent + line;
  99. })
  100. adjusted = replacedLines.join('\n');
  101. }
  102. // listful data
  103. else if (this.isListfulData(text)) {
  104. // do nothing (return null)
  105. }
  106. // not listful data
  107. else {
  108. // append `indentAndMark` at the beginning of all lines (except the first line)
  109. const replacedText = text.replace(/(\r\n|\r|\n)/g, "$1" + indentAndMark);
  110. // append `indentAndMark` to the first line
  111. adjusted = indentAndMark + replacedText;
  112. }
  113. return adjusted;
  114. }
  115. /**
  116. * evaluate whether `text` is list like data or not
  117. * @param {string} text
  118. */
  119. isListfulData(text) {
  120. // return false if includes at least one blank line
  121. // see https://stackoverflow.com/a/16369725
  122. if (text.match(/^\s*[\r\n]/m) != null) {
  123. return false;
  124. }
  125. const lines = text.match(/[^\r\n]+/g);
  126. // count lines that starts with indent and mark
  127. let isListful = false;
  128. let count = 0;
  129. lines.forEach((line) => {
  130. if (line.match(this.indentAndMarkRE)) {
  131. count++;
  132. }
  133. // ensure to be true if it is 50% or more
  134. if (count >= lines.length / 2) {
  135. isListful = true;
  136. return;
  137. }
  138. });
  139. return isListful;
  140. }
  141. /**
  142. * return the postion of the BOL(beginning of line)
  143. */
  144. getBol(editor) {
  145. const curPos = editor.getCursor();
  146. return { line: curPos.line, ch: 0 };
  147. }
  148. /**
  149. * return the postion of the EOL(end of line)
  150. */
  151. getEol(editor) {
  152. const curPos = editor.getCursor();
  153. const lineLength = editor.getDoc().getLine(curPos.line).length;
  154. return { line: curPos.line, ch: lineLength };
  155. }
  156. /**
  157. * return strings from BOL(beginning of line) to current position
  158. */
  159. getStrFromBol(editor) {
  160. const curPos = editor.getCursor();
  161. return editor.getDoc().getRange(this.getBol(editor), curPos);
  162. }
  163. /**
  164. * return strings from current position to EOL(end of line)
  165. */
  166. getStrToEol(editor) {
  167. const curPos = editor.getCursor();
  168. return editor.getDoc().getRange(curPos, this.getEol(editor));
  169. }
  170. }
  171. // singleton pattern
  172. const instance = new MarkdownListHelper();
  173. Object.freeze(instance);
  174. export default instance;