MarkdownListHelper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { BasicInterceptor } from 'crowi-pluginkit';
  2. import * as codemirror from 'codemirror';
  3. import mlu from '../../util/interceptor/MarkdownListUtil';
  4. export default class MarkdownListHelper extends BasicInterceptor {
  5. constructor() {
  6. super();
  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. }
  13. /**
  14. * @inheritdoc
  15. */
  16. isInterceptWhen(contextName) {
  17. return (
  18. contextName === 'preHandleEnter'
  19. );
  20. }
  21. /**
  22. * return boolean value whether processable parallel
  23. */
  24. isProcessableParallel() {
  25. return false;
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. process(contextName, ...args) {
  31. const context = Object.assign(args[0]); // clone
  32. const editor = context.editor;
  33. // get strings from current position to EOL(end of line) before break the line
  34. const strToEol = mlu.getStrToEol(editor);
  35. if (this.indentAndMarkRE.test(strToEol)) {
  36. codemirror.commands.newlineAndIndent(editor);
  37. // replace the line with strToEol (abort auto indent)
  38. editor.getDoc().replaceRange(strToEol, mlu.getBol(editor), mlu.getEol(editor));
  39. // report to manager that handling was done
  40. context.handlers.push(this.className);
  41. }
  42. // resolve
  43. return Promise.resolve(context);
  44. }
  45. /**
  46. * paste text
  47. * @param {any} editor An editor instance of CodeMirror
  48. * @param {any} event
  49. * @param {string} text
  50. */
  51. pasteText(editor, event, text) {
  52. // get strings from BOL(beginning of line) to current position
  53. const strFromBol = mlu.getStrFromBol(editor);
  54. const matched = strFromBol.match(this.indentAndMarkRE);
  55. // when match indentAndMarkOnlyRE
  56. // (this means the current position is the beginning of the list item)
  57. if (this.indentAndMarkOnlyRE.test(strFromBol)) {
  58. const adjusted = this.adjustPastedData(strFromBol, text);
  59. // replace
  60. if (adjusted != null) {
  61. event.preventDefault();
  62. editor.getDoc().replaceRange(adjusted, mlu.getBol(editor), editor.getCursor());
  63. }
  64. }
  65. }
  66. /**
  67. * return adjusted pasted data by indentAndMark
  68. *
  69. * @param {string} indentAndMark
  70. * @param {string} text
  71. * @returns adjusted pasted data
  72. * returns null when adjustment is not necessary
  73. */
  74. adjustPastedData(indentAndMark, text) {
  75. let adjusted = null;
  76. // list data (starts with indent and mark)
  77. if (text.match(this.indentAndMarkRE)) {
  78. const indent = indentAndMark.match(this.indentAndMarkRE)[1];
  79. // splice to an array of line
  80. const lines = text.match(/[^\r\n]+/g);
  81. // indent
  82. const replacedLines = lines.map((line) => {
  83. return indent + line;
  84. })
  85. adjusted = replacedLines.join('\n');
  86. }
  87. // listful data
  88. else if (this.isListfulData(text)) {
  89. // do nothing (return null)
  90. }
  91. // not listful data
  92. else {
  93. // append `indentAndMark` at the beginning of all lines (except the first line)
  94. const replacedText = text.replace(/(\r\n|\r|\n)/g, "$1" + indentAndMark);
  95. // append `indentAndMark` to the first line
  96. adjusted = indentAndMark + replacedText;
  97. }
  98. return adjusted;
  99. }
  100. /**
  101. * evaluate whether `text` is list like data or not
  102. * @param {string} text
  103. */
  104. isListfulData(text) {
  105. // return false if includes at least one blank line
  106. // see https://stackoverflow.com/a/16369725
  107. if (text.match(/^\s*[\r\n]/m) != null) {
  108. return false;
  109. }
  110. const lines = text.match(/[^\r\n]+/g);
  111. // count lines that starts with indent and mark
  112. let isListful = false;
  113. let count = 0;
  114. lines.forEach((line) => {
  115. if (line.match(this.indentAndMarkRE)) {
  116. count++;
  117. }
  118. // ensure to be true if it is 50% or more
  119. if (count >= lines.length / 2) {
  120. isListful = true;
  121. return;
  122. }
  123. });
  124. return isListful;
  125. }
  126. }