MarkdownTableUtil.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // https://regex101.com/r/7BN2fR/7
  9. this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
  10. this.tableAlignmentLineNegRE = /^[^-:]*$/; // it is need to check to ignore empty row which is matched above RE
  11. this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^|\r\n]+\|[^|\r\n]*)+/; // own idea
  12. this.getBot = this.getBot.bind(this);
  13. this.getEot = this.getEot.bind(this);
  14. this.getBol = this.getBol.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 the postion of the BOL(beginning of line)
  64. */
  65. getBol(editor) {
  66. const curPos = editor.getCursor();
  67. return { line: curPos.line, ch: 0 };
  68. }
  69. /**
  70. * return strings from BOT(beginning of table) to the cursor position
  71. */
  72. getStrFromBot(editor) {
  73. const curPos = editor.getCursor();
  74. return editor.getDoc().getRange(this.getBot(editor), curPos);
  75. }
  76. /**
  77. * return strings from the cursor position to EOT(end of table)
  78. */
  79. getStrToEot(editor) {
  80. const curPos = editor.getCursor();
  81. return editor.getDoc().getRange(curPos, this.getEot(editor));
  82. }
  83. /**
  84. * return MarkdownTable instance of the table where the cursor is
  85. * (If the cursor is not in a table, return null)
  86. */
  87. getMarkdownTable(editor) {
  88. if (!this.isInTable(editor)) {
  89. return null;
  90. }
  91. const strFromBotToEot = editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
  92. return MarkdownTable.fromMarkdownString(strFromBotToEot);
  93. }
  94. /**
  95. * return boolean value whether the cursor position is end of line
  96. */
  97. isEndOfLine(editor) {
  98. const curPos = editor.getCursor();
  99. return (curPos.ch === editor.getDoc().getLine(curPos.line).length);
  100. }
  101. /**
  102. * return boolean value whether the cursor position is in a table
  103. */
  104. isInTable(editor) {
  105. const curPos = editor.getCursor();
  106. return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
  107. }
  108. /**
  109. * add a row at the end
  110. * (This function overwrite directory markdown table specified as argument.)
  111. * @param {MarkdownTable} markdown table
  112. */
  113. addRowToMarkdownTable(mdtable) {
  114. const numCol = mdtable.table.length > 0 ? mdtable.table[0].length : 1;
  115. const newRow = [];
  116. (new Array(numCol)).forEach(() => { return newRow.push('') }); // create cols
  117. mdtable.table.push(newRow);
  118. }
  119. /**
  120. * return markdown table that is merged all of markdown table in array
  121. * (The merged markdown table options are used for the first markdown table.)
  122. * @param {Array} array of markdown table
  123. */
  124. mergeMarkdownTable(mdtableList) {
  125. if (mdtableList == null || !(mdtableList instanceof Array)) {
  126. return undefined;
  127. }
  128. let newTable = [];
  129. const options = mdtableList[0].options; // use option of first markdown-table
  130. mdtableList.forEach((mdtable) => {
  131. newTable = newTable.concat(mdtable.table);
  132. });
  133. return (new MarkdownTable(newTable, options));
  134. }
  135. /**
  136. * replace focused markdown table with editor
  137. * (A replaced table is reformed by markdown-table.)
  138. * @param {MarkdownTable} table
  139. */
  140. replaceFocusedMarkdownTableWithEditor(editor, table) {
  141. const curPos = editor.getCursor();
  142. editor.getDoc().replaceRange(table.toString(), this.getBot(editor), this.getEot(editor));
  143. editor.getDoc().setCursor(curPos.line + 1, 2);
  144. }
  145. /**
  146. * return markdown where the markdown table specified by line number params is replaced to the markdown table specified by table param
  147. * @param {string} markdown
  148. * @param {MarkdownTable} table
  149. * @param beginLineNumber
  150. * @param endLineNumber
  151. */
  152. replaceMarkdownTableInMarkdown(table, markdown, beginLineNumber, endLineNumber) {
  153. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  154. const markdownBeforeTable = splitMarkdown.slice(0, beginLineNumber - 1);
  155. const markdownAfterTable = splitMarkdown.slice(endLineNumber);
  156. let newMarkdown = '';
  157. if (markdownBeforeTable.length > 0) {
  158. newMarkdown += `${markdownBeforeTable.join('\n')}\n`;
  159. }
  160. newMarkdown += table;
  161. if (markdownAfterTable.length > 0) {
  162. newMarkdown += `\n${markdownAfterTable.join('\n')}`;
  163. }
  164. return newMarkdown;
  165. }
  166. }
  167. // singleton pattern
  168. const instance = new MarkdownTableUtil();
  169. Object.freeze(instance);
  170. export default instance;