MarkdownTableHelper.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { BasicInterceptor } from 'crowi-pluginkit';
  2. import * as codemirror from 'codemirror';
  3. import markdownTable from 'markdown-table';
  4. import mtu from '../../util/interceptor/MarkdownTableUtil';
  5. /**
  6. * Utility for markdown table
  7. */
  8. export default class MarkdownTableUtil extends BasicInterceptor {
  9. constructor() {
  10. super();
  11. // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
  12. // https://regex101.com/r/7BN2fR/7
  13. this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
  14. this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
  15. }
  16. /**
  17. * @inheritdoc
  18. */
  19. isInterceptWhen(contextName) {
  20. return (
  21. contextName === 'preHandleEnter'
  22. );
  23. }
  24. /**
  25. * return boolean value whether processable parallel
  26. */
  27. isProcessableParallel() {
  28. return false;
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. process(contextName, ...args) {
  34. console.log(performance.now() + ': ReformMarkdownTableInterceptor.process is started');
  35. const orgContext = args[0];
  36. const editor = orgContext.editor;
  37. const curPos = editor.getCursor();
  38. const isEndOfLine = (curPos.ch == editor.getDoc().getLine(curPos.line).length);
  39. console.log(performance.now() + ': curPos.ch=' + curPos.ch + ', curPos.line=' + curPos.line);
  40. // get strings from BOL(beginning of line) to current position
  41. const strFromBol = mtu.getStrFromBol(editor);
  42. if (isEndOfLine && this.linePartOfTableRE.test(strFromBol)) {
  43. const context = Object.assign(args[0]); // clone
  44. const editor = context.editor;
  45. console.log('MarkdownTableHelper.process');
  46. // get lines all of table from current position to beginning of table
  47. const strTableLines = mtu.getStrFromBot(editor);
  48. console.log('strTableLines: ' + strTableLines);
  49. const table = mtu.parseFromTableStringToJSON(editor, mtu.getBot(editor), editor.getCursor());
  50. console.log('table: ' + JSON.stringify(table));
  51. let newRow = [];
  52. table.table[0].forEach(() => { newRow.push(' ') });
  53. console.log('empty: ' + JSON.stringify(newRow));
  54. table.table.push(newRow);
  55. console.log('table: ' + JSON.stringify(table));
  56. const curPos = editor.getCursor();
  57. const nextLineHead = { line: curPos.line + 1, ch: 0 };
  58. const tableBottom = mtu.parseFromTableStringToJSON(editor, nextLineHead, mtu.getEot(editor));
  59. console.log('tableBottom: ' + JSON.stringify(tableBottom));
  60. if (tableBottom.table.length > 0) {
  61. table.table = table.table.concat(tableBottom.table);
  62. }
  63. console.log('table: ' + JSON.stringify(table));
  64. const strTableLinesFormated = markdownTable(table.table, { align: table.align });
  65. console.log('strTableLinesFormated: ' + strTableLinesFormated);
  66. // replace the lines to strFormatedTableLines
  67. editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), mtu.getEot(editor));
  68. editor.getDoc().setCursor(curPos.line + 1, 2);
  69. // report to manager that handling was done
  70. context.handlers.push(this.className);
  71. }
  72. console.log(performance.now() + ': ReformMarkdownTableInterceptor.process is finished');
  73. // resolve
  74. // return Promise.resolve(context);
  75. return Promise.resolve(orgContext);
  76. }
  77. }