MarkdownTableUtil.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import markdown_table from 'markdown-table';
  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.getStrFromBol = this.getStrFromBol.bind(this);
  18. this.parseFromTableStringToMarkdownTable = this.parseFromTableStringToMarkdownTable.bind(this);
  19. this.replaceMarkdownTableWithReformed = this.replaceMarkdownTableWithReformed.bind(this);
  20. }
  21. /**
  22. * return the postion of the BOT(beginning of table)
  23. * (It is assumed that current line is a part of table)
  24. */
  25. getBot(editor) {
  26. const firstLine = editor.getDoc().firstLine();
  27. const curPos = editor.getCursor();
  28. let line = curPos.line - 1;
  29. for (; line >= firstLine; line--) {
  30. const strLine = editor.getDoc().getLine(line);
  31. if (!this.linePartOfTableRE.test(strLine)) {
  32. break;
  33. }
  34. }
  35. const botLine = Math.max(firstLine, line + 1);
  36. return { line: botLine, ch: 0 };
  37. }
  38. /**
  39. * return the postion of the EOT(end of table)
  40. * (It is assumed that current line is a part of table)
  41. */
  42. getEot(editor) {
  43. const lastLine = editor.getDoc().lastLine();
  44. const curPos = editor.getCursor();
  45. let line = curPos.line + 1;
  46. for (; line <= lastLine; line++) {
  47. const strLine = editor.getDoc().getLine(line);
  48. if (!this.linePartOfTableRE.test(strLine)) {
  49. break;
  50. }
  51. }
  52. const eotLine = Math.min(line - 1, lastLine);
  53. const lineLength = editor.getDoc().getLine(eotLine).length;
  54. return { line: eotLine, ch: lineLength };
  55. }
  56. /**
  57. * return the postion of the BOL(beginning of line)
  58. */
  59. getBol(editor) {
  60. const curPos = editor.getCursor();
  61. return { line: curPos.line, ch: 0 };
  62. }
  63. /**
  64. * return strings from BOT(beginning of table) to current position
  65. */
  66. getStrFromBot(editor) {
  67. const curPos = editor.getCursor();
  68. return editor.getDoc().getRange(this.getBot(editor), curPos);
  69. }
  70. /**
  71. * return strings from current position to EOT(end of table)
  72. */
  73. getStrToEot(editor) {
  74. const curPos = editor.getCursor();
  75. return editor.getDoc().getRange(curPos, this.getEot(editor));
  76. }
  77. /**
  78. * return strings from BOL(beginning of line) to current position
  79. */
  80. getStrFromBol(editor) {
  81. const curPos = editor.getCursor();
  82. return editor.getDoc().getRange(this.getBol(editor), curPos);
  83. }
  84. /**
  85. * returns markdown table whose described by 'markdown-table' format
  86. * ref. https://github.com/wooorm/markdown-table
  87. * @param {string} lines all of table
  88. */
  89. parseFromTableStringToMarkdownTable(strMDTable) {
  90. const arrMDTableLines = strMDTable.split(/(\r\n|\r|\n)/);
  91. let contents = [];
  92. let aligns = [];
  93. for (let n = 0; n < arrMDTableLines.length; n++) {
  94. const line = arrMDTableLines[n];
  95. if (this.tableAlignmentLineRE.test(line) && !this.tableAlignmentLineNegRE.test(line)) {
  96. // parse line which described alignment
  97. const alignRuleRE = [
  98. { align: 'c', regex: /^:-+:$/ },
  99. { align: 'l', regex: /^:-+$/ },
  100. { align: 'r', regex: /^-+:$/ },
  101. ];
  102. let lineText = "";
  103. lineText = line.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
  104. lineText = lineText.replace(/\s*/g, '');
  105. aligns = lineText.split(/\|/).map(col => {
  106. const rule = alignRuleRE.find(rule => col.match(rule.regex));
  107. return (rule != undefined) ? rule.align : '';
  108. });
  109. } else if (this.linePartOfTableRE.test(line)) {
  110. // parse line whether header or body
  111. let lineText = "";
  112. lineText = line.replace(/\s*\|\s*/g, '|');
  113. lineText = lineText.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
  114. const row = lineText.split(/\|/);
  115. contents.push(row);
  116. }
  117. }
  118. return (new MarkdownTable(contents, { align: aligns }));
  119. }
  120. /**
  121. * return boolean value whether the current position of cursor is end of line
  122. */
  123. isEndOfLine(editor) {
  124. const curPos = editor.getCursor();
  125. return (curPos.ch == editor.getDoc().getLine(curPos.line).length);
  126. }
  127. /**
  128. * add a row at the end
  129. * (This function overwrite directory markdown table specified as argument.)
  130. * @param {MarkdownTable} markdown table
  131. */
  132. addRowToMarkdownTable(mdtable) {
  133. const numCol = mdtable.table.length > 0 ? mdtable.table[0].length : 1;
  134. let newRow = [];
  135. (new Array(numCol)).forEach(() => newRow.push('')); // create cols
  136. mdtable.table.push(newRow);
  137. }
  138. /**
  139. * returns markdown table that is merged all of markdown table in array
  140. * (The merged markdown table options are used for the first markdown table.)
  141. * @param {Array} array of markdown table
  142. */
  143. mergeMarkdownTable(mdtable_list) {
  144. if (mdtable_list == undefined
  145. || !(mdtable_list instanceof Array)) {
  146. return undefined;
  147. }
  148. let newTable = [];
  149. const options = mdtable_list[0].options; // use option of first markdown-table
  150. mdtable_list.forEach((mdtable) => {
  151. newTable = newTable.concat(mdtable.table)
  152. });
  153. return (new MarkdownTable(newTable, options));
  154. }
  155. /**
  156. * replace markdown table which is reformed by markdown-table
  157. * @param {MarkdownTable} markdown table
  158. */
  159. replaceMarkdownTableWithReformed(editor, table) {
  160. const curPos = editor.getCursor();
  161. // replace the lines to strTableLinesFormated
  162. const strTableLinesFormated = table.toString();
  163. editor.getDoc().replaceRange(strTableLinesFormated, this.getBot(editor), this.getEot(editor));
  164. // set cursor to first column
  165. editor.getDoc().setCursor(curPos.line + 1, 2);
  166. }
  167. }
  168. /**
  169. * markdown table class for markdown-table module
  170. * ref. https://github.com/wooorm/markdown-table
  171. */
  172. class MarkdownTable {
  173. constructor(table, options) {
  174. this.table = table || [];
  175. this.options = options || {};
  176. this.toString = this.toString.bind(this);
  177. }
  178. toString() {
  179. return markdown_table(this.table, this.options);
  180. }
  181. }
  182. // singleton pattern
  183. const instance = new MarkdownTableUtil();
  184. Object.freeze(instance);
  185. export default instance;