MarkdownTableInterceptor.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { BasicInterceptor } from 'growi-pluginkit';
  2. import mtu from './MarkdownTableUtil';
  3. /**
  4. * Interceptor for markdown table
  5. */
  6. export default class MarkdownTableInterceptor extends BasicInterceptor {
  7. constructor() {
  8. super();
  9. }
  10. /**
  11. * @inheritdoc
  12. */
  13. isInterceptWhen(contextName) {
  14. return (
  15. contextName === 'preHandleEnter'
  16. );
  17. }
  18. /**
  19. * return boolean value whether processable parallel
  20. */
  21. isProcessableParallel() {
  22. return false;
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. process(contextName, ...args) {
  28. const context = Object.assign(args[0]); // clone
  29. const editor = context.editor; // AbstractEditor instance
  30. // do nothing if editor is not a CodeMirrorEditor
  31. if (editor == null || editor.getCodeMirror() == null) {
  32. return Promise.resolve(context);
  33. }
  34. const cm = editor.getCodeMirror();
  35. // get strings from BOL(beginning of line) to current position
  36. const strFromBol = editor.getStrFromBol();
  37. if (mtu.isEndOfLine(cm) && mtu.linePartOfTableRE.test(strFromBol)) {
  38. // get lines all of table from current position to beginning of table
  39. const strFromBot = mtu.getStrFromBot(cm);
  40. let table = mtu.parseFromTableStringToMarkdownTable(strFromBot);
  41. mtu.addRowToMarkdownTable(table);
  42. const strToEot = mtu.getStrToEot(cm);
  43. const tableBottom = mtu.parseFromTableStringToMarkdownTable(strToEot);
  44. if (tableBottom.table.length > 0) {
  45. table = mtu.mergeMarkdownTable([table, tableBottom]);
  46. }
  47. mtu.replaceMarkdownTableWithReformed(cm, table);
  48. // report to manager that handling was done
  49. context.handlers.push(this.className);
  50. }
  51. // resolve
  52. return Promise.resolve(context);
  53. }
  54. }