ReformMarkdownTableInterceptor.js 6.0 KB

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