MarkdownListUtil.js 4.1 KB

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