MarkdownTableUtil.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import MarkdownTable from '../../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. /**
  88. * return boolean value whether the cursor position is end of line
  89. */
  90. isEndOfLine(editor) {
  91. const curPos = editor.getCursor();
  92. return (curPos.ch === editor.getDoc().getLine(curPos.line).length);
  93. }
  94. /**
  95. * return boolean value whether the cursor position is in a table
  96. */
  97. isInTable(editor) {
  98. const curPos = editor.getCursor();
  99. return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
  100. }
  101. /**
  102. * add a row at the end
  103. * (This function overwrite directory markdown table specified as argument.)
  104. * @param {MarkdownTable} markdown table
  105. */
  106. addRowToMarkdownTable(mdtable) {
  107. const numCol = mdtable.table.length > 0 ? mdtable.table[0].length : 1;
  108. const newRow = [];
  109. (new Array(numCol)).forEach(() => { return newRow.push('') }); // create cols
  110. mdtable.table.push(newRow);
  111. }
  112. /**
  113. * return markdown table that is merged all of markdown table in array
  114. * (The merged markdown table options are used for the first markdown table.)
  115. * @param {Array} array of markdown table
  116. */
  117. mergeMarkdownTable(mdtableList) {
  118. if (mdtableList == null || !(mdtableList instanceof Array)) {
  119. return undefined;
  120. }
  121. let newTable = [];
  122. const options = mdtableList[0].options; // use option of first markdown-table
  123. mdtableList.forEach((mdtable) => {
  124. newTable = newTable.concat(mdtable.table);
  125. });
  126. return (new MarkdownTable(newTable, options));
  127. }
  128. /**
  129. * replace focused markdown table with editor
  130. * (A replaced table is reformed by markdown-table.)
  131. * @param {MarkdownTable} table
  132. */
  133. replaceFocusedMarkdownTableWithEditor(editor, table) {
  134. const curPos = editor.getCursor();
  135. editor.getDoc().replaceRange(table.toString(), this.getBot(editor), this.getEot(editor));
  136. editor.getDoc().setCursor(curPos.line + 1, 2);
  137. }
  138. /**
  139. * return markdown where the markdown table specified by line number params is replaced to the markdown table specified by table param
  140. * @param {string} markdown
  141. * @param {MarkdownTable} table
  142. * @param beginLineNumber
  143. * @param endLineNumber
  144. */
  145. replaceMarkdownTableInMarkdown(table, markdown, beginLineNumber, endLineNumber) {
  146. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  147. const markdownBeforeTable = splitMarkdown.slice(0, beginLineNumber - 1);
  148. const markdownAfterTable = splitMarkdown.slice(endLineNumber);
  149. let newMarkdown = '';
  150. if (markdownBeforeTable.length > 0) {
  151. newMarkdown += `${markdownBeforeTable.join('\n')}\n`;
  152. }
  153. newMarkdown += table;
  154. if (markdownAfterTable.length > 0) {
  155. newMarkdown += `\n${markdownAfterTable.join('\n')}`;
  156. }
  157. return newMarkdown;
  158. }
  159. }
  160. // singleton pattern
  161. const instance = new MarkdownTableUtil();
  162. Object.freeze(instance);
  163. export default instance;