MarkdownTableUtil.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.getEot = this.getEot.bind(this);
  15. this.getStrFromBot = this.getStrFromBot.bind(this);
  16. this.getStrToEot = this.getStrToEot.bind(this);
  17. this.isInTable = this.isInTable.bind(this);
  18. this.replaceFocusedMarkdownTableWithEditor = this.replaceFocusedMarkdownTableWithEditor.bind(this);
  19. this.replaceMarkdownTableWithReformed = this.replaceFocusedMarkdownTableWithEditor; // alias
  20. }
  21. /**
  22. * return the postion of the BOT(beginning of table)
  23. * (If the cursor is not in a table, return its position)
  24. */
  25. getBot(editor) {
  26. const curPos = editor.getCursor();
  27. if (!this.isInTable(editor)) {
  28. return { line: curPos.line, ch: curPos.ch };
  29. }
  30. const firstLine = editor.getDoc().firstLine();
  31. let line = curPos.line - 1;
  32. for (; line >= firstLine; line--) {
  33. const strLine = editor.getDoc().getLine(line);
  34. if (!this.linePartOfTableRE.test(strLine)) {
  35. break;
  36. }
  37. }
  38. const botLine = Math.max(firstLine, line + 1);
  39. return { line: botLine, ch: 0 };
  40. }
  41. /**
  42. * return the postion of the EOT(end of table)
  43. * (If the cursor is not in a table, return its position)
  44. */
  45. getEot(editor) {
  46. const curPos = editor.getCursor();
  47. if (!this.isInTable(editor)) {
  48. return { line: curPos.line, ch: curPos.ch };
  49. }
  50. const lastLine = editor.getDoc().lastLine();
  51. let line = curPos.line + 1;
  52. for (; line <= lastLine; line++) {
  53. const strLine = editor.getDoc().getLine(line);
  54. if (!this.linePartOfTableRE.test(strLine)) {
  55. break;
  56. }
  57. }
  58. const eotLine = Math.min(line - 1, lastLine);
  59. const lineLength = editor.getDoc().getLine(eotLine).length;
  60. return { line: eotLine, ch: lineLength };
  61. }
  62. /**
  63. * return strings from BOT(beginning of table) to the cursor position
  64. */
  65. getStrFromBot(editor) {
  66. const curPos = editor.getCursor();
  67. return editor.getDoc().getRange(this.getBot(editor), curPos);
  68. }
  69. /**
  70. * return strings from the cursor position to EOT(end of table)
  71. */
  72. getStrToEot(editor) {
  73. const curPos = editor.getCursor();
  74. return editor.getDoc().getRange(curPos, this.getEot(editor));
  75. }
  76. /**
  77. * return MarkdownTable instance of the table where the cursor is
  78. * (If the cursor is not in a table, return null)
  79. */
  80. getMarkdownTable(editor) {
  81. if (!this.isInTable(editor)) {
  82. return null;
  83. }
  84. const strFromBotToEot = editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
  85. return MarkdownTable.fromMarkdownString(strFromBotToEot);
  86. }
  87. getMarkdownTableFromLine(markdown, bol, eol) {
  88. const tableLines = markdown.split(/\r\n|\r|\n/).slice(bol - 1, eol).join('\n');
  89. return MarkdownTable.fromMarkdownString(tableLines);
  90. }
  91. /**
  92. * return boolean value whether the cursor position is end of line
  93. */
  94. isEndOfLine(editor) {
  95. const curPos = editor.getCursor();
  96. return (curPos.ch === editor.getDoc().getLine(curPos.line).length);
  97. }
  98. /**
  99. * return boolean value whether the cursor position is in a table
  100. */
  101. isInTable(editor) {
  102. const curPos = editor.getCursor();
  103. return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
  104. }
  105. /**
  106. * add a row at the end
  107. * (This function overwrite directory markdown table specified as argument.)
  108. * @param {MarkdownTable} markdown table
  109. */
  110. addRowToMarkdownTable(mdtable) {
  111. const numCol = mdtable.table.length > 0 ? mdtable.table[0].length : 1;
  112. const newRow = [];
  113. (new Array(numCol)).forEach(() => { return newRow.push('') }); // create cols
  114. mdtable.table.push(newRow);
  115. }
  116. /**
  117. * return markdown table that is merged all of markdown table in array
  118. * (The merged markdown table options are used for the first markdown table.)
  119. * @param {Array} array of markdown table
  120. */
  121. mergeMarkdownTable(mdtableList) {
  122. if (mdtableList == null || !(mdtableList instanceof Array)) {
  123. return undefined;
  124. }
  125. let newTable = [];
  126. const options = mdtableList[0].options; // use option of first markdown-table
  127. mdtableList.forEach((mdtable) => {
  128. newTable = newTable.concat(mdtable.table);
  129. });
  130. return (new MarkdownTable(newTable, options));
  131. }
  132. /**
  133. * replace focused markdown table with editor
  134. * (A replaced table is reformed by markdown-table.)
  135. * @param {MarkdownTable} table
  136. */
  137. replaceFocusedMarkdownTableWithEditor(editor, table) {
  138. const curPos = editor.getCursor();
  139. editor.getDoc().replaceRange(table.toString(), this.getBot(editor), this.getEot(editor));
  140. editor.getDoc().setCursor(curPos.line + 1, 2);
  141. }
  142. /**
  143. * return markdown where the markdown table specified by line number params is replaced to the markdown table specified by table param
  144. * @param {string} markdown
  145. * @param {MarkdownTable} table
  146. * @param beginLineNumber
  147. * @param endLineNumber
  148. */
  149. replaceMarkdownTableInMarkdown(table, markdown, beginLineNumber, endLineNumber) {
  150. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  151. const markdownBeforeTable = splitMarkdown.slice(0, beginLineNumber - 1);
  152. const markdownAfterTable = splitMarkdown.slice(endLineNumber);
  153. let newMarkdown = '';
  154. if (markdownBeforeTable.length > 0) {
  155. newMarkdown += `${markdownBeforeTable.join('\n')}\n`;
  156. }
  157. newMarkdown += table;
  158. if (markdownAfterTable.length > 0) {
  159. newMarkdown += `\n${markdownAfterTable.join('\n')}`;
  160. }
  161. return newMarkdown;
  162. }
  163. }
  164. // singleton pattern
  165. const instance = new MarkdownTableUtil();
  166. Object.freeze(instance);
  167. export default instance;