MarkdownTableUtil.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import MarkdownTable from '~/client/models/MarkdownTable';
  2. /**
  3. * Utility for markdown table
  4. */
  5. class MarkdownTableUtil {
  6. constructor() {
  7. // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
  8. this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
  9. this.tableAlignmentLineNegRE = /^[^-:]*$/; // it is need to check to ignore empty row which is matched above RE
  10. // https://regex101.com/r/7BN2fR/10
  11. this.linePartOfTableRE = /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/;
  12. // https://regex101.com/r/1UuWBJ/3
  13. this.emptyLineOfTableRE = /^([^\r\n|]*)\|((\s*\|)+)$/;
  14. this.curPos = this.curPos.bind(this);
  15. this.getBot = this.getBot.bind(this);
  16. this.getEot = this.getEot.bind(this);
  17. this.getStrFromBot = this.getStrFromBot.bind(this);
  18. this.getStrToEot = this.getStrToEot.bind(this);
  19. this.isInTable = this.isInTable.bind(this);
  20. this.replaceFocusedMarkdownTableWithEditor = this.replaceFocusedMarkdownTableWithEditor.bind(this);
  21. this.replaceMarkdownTableWithReformed = this.replaceFocusedMarkdownTableWithEditor; // alias
  22. }
  23. curPos(editor) {
  24. return editor.state.selection.main.head;
  25. }
  26. /**
  27. * return the postion of the BOT(beginning of table)
  28. * (If the cursor is not in a table, return its position)
  29. */
  30. getBot(editor) {
  31. if (!this.isInTable(editor)) {
  32. return this.curPos(editor);
  33. }
  34. const doc = editor.state.doc;
  35. const firstLine = 1;
  36. let line = doc.lineAt(this.curPos(editor)).number - 1;
  37. for (; line >= firstLine; line--) {
  38. const strLine = doc.line(line).text;
  39. if (!this.linePartOfTableRE.test(strLine)) {
  40. break;
  41. }
  42. }
  43. const botLine = Math.max(firstLine, line + 1);
  44. return doc.line(botLine).from;
  45. }
  46. /**
  47. * return the postion of the EOT(end of table)
  48. * (If the cursor is not in a table, return its position)
  49. */
  50. getEot(editor) {
  51. if (!this.isInTable(editor)) {
  52. return this.curPos(editor);
  53. }
  54. const doc = editor.state.doc;
  55. const lastLine = doc.lines;
  56. let line = doc.lineAt(this.curPos(editor)).number + 1;
  57. for (; line <= lastLine; line++) {
  58. const strLine = doc.line(line).text;
  59. if (!this.linePartOfTableRE.test(strLine)) {
  60. break;
  61. }
  62. }
  63. const eotLine = Math.min(line - 1, lastLine);
  64. return doc.line(eotLine).to;
  65. }
  66. /**
  67. * return strings from BOT(beginning of table) to the cursor position
  68. */
  69. getStrFromBot(editor) {
  70. return editor.state.sliceDoc(this.getBot(editor), this.curPos(editor));
  71. }
  72. /**
  73. * return strings from the cursor position to EOT(end of table)
  74. */
  75. getStrToEot(editor) {
  76. return editor.state.sliceDoc(this.curPos(editor), this.getEot(editor));
  77. }
  78. /**
  79. * return MarkdownTable instance of the table where the cursor is
  80. * (If the cursor is not in a table, return null)
  81. */
  82. getMarkdownTable(editor) {
  83. if (!this.isInTable(editor)) {
  84. return null;
  85. }
  86. const strFromBotToEot = editor.state.sliceDoc(this.getBot(editor), this.getEot(editor));
  87. return MarkdownTable.fromMarkdownString(strFromBotToEot);
  88. }
  89. getMarkdownTableFromLine(markdown, bol, eol) {
  90. const tableLines = markdown.split(/\r\n|\r|\n/).slice(bol - 1, eol).join('\n');
  91. return MarkdownTable.fromMarkdownString(tableLines);
  92. }
  93. /**
  94. * return boolean value whether the cursor position is end of line
  95. */
  96. isEndOfLine(editor) {
  97. return this.curPos(editor) === editor.state.doc.lineAt(this.curPos(editor)).to;
  98. }
  99. /**
  100. * return boolean value whether the cursor position is in a table
  101. */
  102. isInTable(editor) {
  103. const lineText = editor.state.doc.lineAt(this.curPos(editor)).text;
  104. return this.linePartOfTableRE.test(lineText);
  105. }
  106. /**
  107. * add a row at the end
  108. * (This function overwrite directory markdown table specified as argument.)
  109. * @param {MarkdownTable} markdown table
  110. */
  111. addRowToMarkdownTable(mdtable) {
  112. const numCol = mdtable.table.length > 0 ? mdtable.table[0].length : 1;
  113. const newRow = [];
  114. (new Array(numCol)).forEach(() => { return newRow.push('') }); // create cols
  115. mdtable.table.push(newRow);
  116. }
  117. /**
  118. * return markdown table that is merged all of markdown table in array
  119. * (The merged markdown table options are used for the first markdown table.)
  120. * @param {Array} array of markdown table
  121. */
  122. mergeMarkdownTable(mdtableList) {
  123. if (mdtableList == null || !(mdtableList instanceof Array)) {
  124. return undefined;
  125. }
  126. let newTable = [];
  127. const options = mdtableList[0].options; // use option of first markdown-table
  128. mdtableList.forEach((mdtable) => {
  129. newTable = newTable.concat(mdtable.table);
  130. });
  131. return new MarkdownTable(newTable, options);
  132. }
  133. /**
  134. * replace focused markdown table with editor
  135. * (A replaced table is reformed by markdown-table.)
  136. * @param {MarkdownTable} table
  137. */
  138. replaceFocusedMarkdownTableWithEditor(editor, table) {
  139. const botPos = this.getBot(editor);
  140. const eotPos = this.getEot(editor);
  141. editor.dispatch({
  142. changes: {
  143. from: botPos,
  144. to: eotPos,
  145. insert: table.toString(),
  146. },
  147. });
  148. editor.dispatch({
  149. selection: { anchor: editor.state.doc.lineAt(eotPos).to },
  150. });
  151. editor.focus();
  152. }
  153. /**
  154. * return markdown where the markdown table specified by line number params is replaced to the markdown table specified by table param
  155. * @param {string} markdown
  156. * @param {MarkdownTable} table
  157. * @param beginLineNumber
  158. * @param endLineNumber
  159. */
  160. replaceMarkdownTableInMarkdown(table, markdown, beginLineNumber, endLineNumber) {
  161. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  162. const markdownBeforeTable = splitMarkdown.slice(0, beginLineNumber - 1);
  163. const markdownAfterTable = splitMarkdown.slice(endLineNumber);
  164. let newMarkdown = '';
  165. if (markdownBeforeTable.length > 0) {
  166. newMarkdown += `${markdownBeforeTable.join('\n')}\n`;
  167. }
  168. newMarkdown += table;
  169. if (markdownAfterTable.length > 0) {
  170. newMarkdown += `\n${markdownAfterTable.join('\n')}`;
  171. }
  172. return newMarkdown;
  173. }
  174. }
  175. // singleton pattern
  176. const instance = new MarkdownTableUtil();
  177. Object.freeze(instance);
  178. export default instance;