MarkdownTableUtil.js 6.7 KB

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