MarkdownTableHelper.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import * as codemirror from 'codemirror';
  2. import markdownTable from 'markdown-table';
  3. class MarkdownTableHelper {
  4. constructor() {
  5. // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
  6. // https://regex101.com/r/7BN2fR/7
  7. this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
  8. this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
  9. this.isMatchedContext = this.isMatchedContext.bind(this);
  10. this.handleNewLine = this.handleNewLine.bind(this);
  11. this.newlineAndIndentContinueMarkdownTable = this.newlineAndIndentContinueMarkdownTable.bind(this);
  12. this.pasteText = this.pasteText.bind(this);
  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.parseFromTableStringToJSON = this.parseFromTableStringToJSON.bind(this);
  20. }
  21. /**
  22. * return whether context is matched by table
  23. * @param {any} editor An editor instance of CodeMirror
  24. */
  25. isMatchedContext(editor) {
  26. console.log('MarkdownTableHelper.isMatchedContext');
  27. // get strings from BOL(beginning of line) to current position
  28. const strFromBol = this.getStrFromBol(editor);
  29. console.log('strFromBol: ' + strFromBol);
  30. console.log('will return ' + (this.linePartOfTableRE.test(strFromBol) ? 'true' : 'false'));
  31. return this.linePartOfTableRE.test(strFromBol);
  32. }
  33. /**
  34. * handle new line
  35. * @param {any} editor An editor instance of CodeMirror
  36. */
  37. handleNewLine(editor) {
  38. console.log('MarkdownTableHelper.handleNewLine');
  39. this.newlineAndIndentContinueMarkdownTable(editor);
  40. }
  41. /**
  42. * insert new line with auto shaping format of Markdown table
  43. * @param {any} editor An editor instance of CodeMirror
  44. */
  45. newlineAndIndentContinueMarkdownTable(editor) {
  46. console.log('MarkdownTableHelper.newlineAndIndentContinueMarkdownTable');
  47. if (!this.isMatchedContext(editor)) return;
  48. // get lines all of table from current position to beginning of table
  49. const strTableLines = this.getStrFromBot(editor);
  50. console.log('strTableLines: ' + strTableLines);
  51. const table = this.parseFromTableStringToJSON(editor, this.getBot(editor), editor.getCursor());
  52. console.log('table: ' + JSON.stringify(table));
  53. const strTableLinesFormated = table;
  54. console.log('strTableLinesFormated: ' + strTableLinesFormated);
  55. // replace the lines to strFormatedTableLines
  56. editor.getDoc().replaceRange(strTableLinesFormated, this.getBot(editor), editor.getCursor());
  57. codemirror.commands.newlineAndIndent(editor);
  58. }
  59. /**
  60. * paste text
  61. * @param {any} editor An editor instance of CodeMirror
  62. * @param {any} event
  63. * @param {string} text
  64. */
  65. pasteText(editor, event, text) {
  66. // [TODO] replace to formated table markdown
  67. }
  68. /**
  69. * return the postion of the BOT(beginning of table)
  70. * (It is assumed that current line is a part of table)
  71. */
  72. getBot(editor) {
  73. const firstLine = editor.getDoc().firstLine();
  74. const curPos = editor.getCursor();
  75. let line = curPos.line - 1;
  76. for (; line >= firstLine; line--) {
  77. const strLine = editor.getDoc().getLine(line);
  78. if (!this.linePartOfTableRE.test(strLine)) {
  79. break;
  80. }
  81. }
  82. const botLine = Math.max(firstLine, line + 1);
  83. return { line: botLine, ch: 0 };
  84. }
  85. /**
  86. * return the postion of the EOT(end of table)
  87. * (It is assumed that current line is a part of table)
  88. */
  89. getEot(editor) {
  90. const lastLine = editor.getDoc().lastLine();
  91. const curPos = editor.getCursor();
  92. let line = curPos.line + 1;
  93. for (; line <= lastLine; line++) {
  94. const strLine = editor.getDoc().getLine(line);
  95. if (!this.linePartOfTableRE.test(strLine)) {
  96. break;
  97. }
  98. }
  99. const eotLine = Math.min(line - 1, lastLine);
  100. const lineLength = editor.getDoc().getLine(eotLine).length;
  101. return { line: eotLine, ch: lineLength };
  102. }
  103. /**
  104. * return the postion of the BOL(beginning of line)
  105. */
  106. getBol(editor) {
  107. const curPos = editor.getCursor();
  108. return { line: curPos.line, ch: 0 };
  109. }
  110. /**
  111. * return strings from BOT(beginning of table) to current position
  112. */
  113. getStrFromBot(editor) {
  114. const curPos = editor.getCursor();
  115. return editor.getDoc().getRange(this.getBot(editor), curPos);
  116. }
  117. /**
  118. * return strings from current position to EOT(end of table)
  119. */
  120. getStrToEot(editor) {
  121. const curPos = editor.getCursor();
  122. return editor.getDoc().getRange(curPos, this.getEot(editor));
  123. }
  124. /**
  125. * return strings from BOL(beginning of line) to current position
  126. */
  127. getStrFromBol(editor) {
  128. const curPos = editor.getCursor();
  129. return editor.getDoc().getRange(this.getBol(editor), curPos);
  130. }
  131. /**
  132. * returns object whose described by 'markdown-table' format
  133. * ref. https://github.com/wooorm/markdown-table
  134. * @param {string} lines all of table
  135. */
  136. parseFromTableStringToJSON(editor, posBeg, posEnd) {
  137. console.log("parseFromTableStringToJSON: posBeg.line: " + posBeg.line + ", posEnd.line: " + posEnd.line);
  138. let contents = [];
  139. let aligns = [];
  140. for (let pos = posBeg; pos.line <= posEnd.line; pos.line++) {
  141. const line = editor.getDoc().getLine(pos.line);
  142. console.log("line#" + pos.line + ": " + line);
  143. if (this.tableAlignmentLineRE.test(line)) {
  144. // parse line which described alignment
  145. const alignRuleRE = [
  146. { align: 'c', regex: /^:-+:$/ },
  147. { align: 'l', regex: /^:-+$/ },
  148. { align: 'r', regex: /^-+:$/ },
  149. ];
  150. let lineText = "";
  151. lineText = line.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
  152. lineText = lineText.replace(/\s*/g, '');
  153. aligns = lineText.split(/\|/).map(col => {
  154. const rule = alignRuleRE.find(rule => col.match(rule.regex));
  155. return (rule != undefined) ? rule.align : '';
  156. });
  157. } else {
  158. // parse line whether header or body
  159. let lineText = "";
  160. lineText = line.replace(/\s*\|\s*/g, '|');
  161. lineText = lineText.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
  162. const row = lineText.split(/\|/);
  163. console.log('row: ' + row);
  164. contents.push(row);
  165. }
  166. }
  167. console.log('contents: ' + JSON.stringify(contents));
  168. console.log('aligns: ' + JSON.stringify(aligns));
  169. return markdownTable(contents, { align: aligns } );
  170. }
  171. }
  172. // singleton pattern
  173. const instance = new MarkdownTableHelper();
  174. Object.freeze(instance);
  175. export default instance;