2
0

MarkdownTableUtil.js 4.3 KB

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