MarkdownTableUtil.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import * as codemirror from 'codemirror';
  2. /**
  3. * Utility for markdown table
  4. */
  5. class MarkdownTableUtil {
  6. constructor() {
  7. // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
  8. // https://regex101.com/r/7BN2fR/7
  9. this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
  10. this.tableAlignmentLineNegRE = /^[^-:]*$/; // it is need to check to ignore empty row which is matched above RE
  11. this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
  12. this.getBot = this.getBot.bind(this);
  13. this.getEot = this.getEot.bind(this);
  14. this.getBol = this.getBol.bind(this);
  15. this.getStrFromBot = this.getStrFromBot.bind(this);
  16. this.getStrToEot = this.getStrToEot.bind(this);
  17. this.getStrFromBol = this.getStrFromBol.bind(this);
  18. this.parseFromTableStringToJSON = this.parseFromTableStringToJSON.bind(this);
  19. }
  20. /**
  21. * return the postion of the BOT(beginning of table)
  22. * (It is assumed that current line is a part of table)
  23. */
  24. getBot(editor) {
  25. const firstLine = editor.getDoc().firstLine();
  26. const curPos = editor.getCursor();
  27. let line = curPos.line - 1;
  28. for (; line >= firstLine; line--) {
  29. const strLine = editor.getDoc().getLine(line);
  30. if (!this.linePartOfTableRE.test(strLine)) {
  31. break;
  32. }
  33. }
  34. const botLine = Math.max(firstLine, line + 1);
  35. return { line: botLine, ch: 0 };
  36. }
  37. /**
  38. * return the postion of the EOT(end of table)
  39. * (It is assumed that current line is a part of table)
  40. */
  41. getEot(editor) {
  42. const lastLine = editor.getDoc().lastLine();
  43. const curPos = editor.getCursor();
  44. let line = curPos.line + 1;
  45. for (; line <= lastLine; line++) {
  46. const strLine = editor.getDoc().getLine(line);
  47. if (!this.linePartOfTableRE.test(strLine)) {
  48. break;
  49. }
  50. }
  51. const eotLine = Math.min(line - 1, lastLine);
  52. const lineLength = editor.getDoc().getLine(eotLine).length;
  53. return { line: eotLine, ch: lineLength };
  54. }
  55. /**
  56. * return the postion of the BOL(beginning of line)
  57. */
  58. getBol(editor) {
  59. const curPos = editor.getCursor();
  60. return { line: curPos.line, ch: 0 };
  61. }
  62. /**
  63. * return strings from BOT(beginning of table) to current position
  64. */
  65. getStrFromBot(editor) {
  66. const curPos = editor.getCursor();
  67. return editor.getDoc().getRange(this.getBot(editor), curPos);
  68. }
  69. /**
  70. * return strings from current position to EOT(end of table)
  71. */
  72. getStrToEot(editor) {
  73. const curPos = editor.getCursor();
  74. return editor.getDoc().getRange(curPos, this.getEot(editor));
  75. }
  76. /**
  77. * return strings from BOL(beginning of line) to current position
  78. */
  79. getStrFromBol(editor) {
  80. const curPos = editor.getCursor();
  81. return editor.getDoc().getRange(this.getBol(editor), curPos);
  82. }
  83. /**
  84. * returns JSON whose described by 'markdown-table' format
  85. * ref. https://github.com/wooorm/markdown-table
  86. * @param {string} lines all of table
  87. */
  88. parseFromTableStringToJSON(editor, posBeg, posEnd) {
  89. let contents = [];
  90. let aligns = [];
  91. for (let pos = posBeg; pos.line <= posEnd.line; pos.line++) {
  92. const line = editor.getDoc().getLine(pos.line);
  93. if (this.tableAlignmentLineRE.test(line) && !this.tableAlignmentLineNegRE.test(line)) {
  94. // parse line which described alignment
  95. const alignRuleRE = [
  96. { align: 'c', regex: /^:-+:$/ },
  97. { align: 'l', regex: /^:-+$/ },
  98. { align: 'r', regex: /^-+:$/ },
  99. ];
  100. let lineText = "";
  101. lineText = line.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
  102. lineText = lineText.replace(/\s*/g, '');
  103. aligns = lineText.split(/\|/).map(col => {
  104. const rule = alignRuleRE.find(rule => col.match(rule.regex));
  105. return (rule != undefined) ? rule.align : '';
  106. });
  107. } else {
  108. // parse line whether header or body
  109. let lineText = "";
  110. lineText = line.replace(/\s*\|\s*/g, '|');
  111. lineText = lineText.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
  112. const row = lineText.split(/\|/);
  113. contents.push(row);
  114. }
  115. }
  116. return { table: contents, align: aligns };
  117. }
  118. }
  119. // singleton pattern
  120. const instance = new MarkdownTableUtil();
  121. Object.freeze(instance);
  122. export default instance;