table.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // GFM table, https://github.github.com/gfm/#tables-extension-
  2. 'use strict';
  3. var isSpace = require('../common/utils').isSpace;
  4. function getLine(state, line) {
  5. var pos = state.bMarks[line] + state.tShift[line],
  6. max = state.eMarks[line];
  7. return state.src.slice(pos, max);
  8. }
  9. function escapedSplit(str) {
  10. var result = [],
  11. pos = 0,
  12. max = str.length,
  13. ch,
  14. isEscaped = false,
  15. lastPos = 0,
  16. current = '';
  17. ch = str.charCodeAt(pos);
  18. while (pos < max) {
  19. if (ch === 0x7c/* | */) {
  20. if (!isEscaped) {
  21. // pipe separating cells, '|'
  22. result.push(current + str.substring(lastPos, pos));
  23. current = '';
  24. lastPos = pos + 1;
  25. } else {
  26. // escaped pipe, '\|'
  27. current += str.substring(lastPos, pos - 1);
  28. lastPos = pos;
  29. }
  30. }
  31. isEscaped = (ch === 0x5c/* \ */);
  32. pos++;
  33. ch = str.charCodeAt(pos);
  34. }
  35. result.push(current + str.substring(lastPos));
  36. return result;
  37. }
  38. module.exports = function table(state, startLine, endLine, silent) {
  39. var ch, lineText, pos, i, l, nextLine, columns, columnCount, token,
  40. aligns, t, tableLines, tbodyLines, oldParentType, terminate,
  41. terminatorRules, firstCh, secondCh;
  42. // should have at least two lines
  43. if (startLine + 2 > endLine) { return false; }
  44. nextLine = startLine + 1;
  45. if (state.sCount[nextLine] < state.blkIndent) { return false; }
  46. // if it's indented more than 3 spaces, it should be a code block
  47. if (state.sCount[nextLine] - state.blkIndent >= 4) { return false; }
  48. // first character of the second line should be '|', '-', ':',
  49. // and no other characters are allowed but spaces;
  50. // basically, this is the equivalent of /^[-:|][-:|\s]*$/ regexp
  51. pos = state.bMarks[nextLine] + state.tShift[nextLine];
  52. if (pos >= state.eMarks[nextLine]) { return false; }
  53. firstCh = state.src.charCodeAt(pos++);
  54. if (firstCh !== 0x7C/* | */ && firstCh !== 0x2D/* - */ && firstCh !== 0x3A/* : */) { return false; }
  55. if (pos >= state.eMarks[nextLine]) { return false; }
  56. secondCh = state.src.charCodeAt(pos++);
  57. if (secondCh !== 0x7C/* | */ && secondCh !== 0x2D/* - */ && secondCh !== 0x3A/* : */ && !isSpace(secondCh)) {
  58. return false;
  59. }
  60. // if first character is '-', then second character must not be a space
  61. // (due to parsing ambiguity with list)
  62. if (firstCh === 0x2D/* - */ && isSpace(secondCh)) { return false; }
  63. while (pos < state.eMarks[nextLine]) {
  64. ch = state.src.charCodeAt(pos);
  65. if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */ && ch !== 0x3A/* : */ && !isSpace(ch)) { return false; }
  66. pos++;
  67. }
  68. lineText = getLine(state, startLine + 1);
  69. columns = lineText.split('|');
  70. aligns = [];
  71. for (i = 0; i < columns.length; i++) {
  72. t = columns[i].trim();
  73. if (!t) {
  74. // allow empty columns before and after table, but not in between columns;
  75. // e.g. allow ` |---| `, disallow ` ---||--- `
  76. if (i === 0 || i === columns.length - 1) {
  77. continue;
  78. } else {
  79. return false;
  80. }
  81. }
  82. if (!/^:?-+:?$/.test(t)) { return false; }
  83. if (t.charCodeAt(t.length - 1) === 0x3A/* : */) {
  84. aligns.push(t.charCodeAt(0) === 0x3A/* : */ ? 'center' : 'right');
  85. } else if (t.charCodeAt(0) === 0x3A/* : */) {
  86. aligns.push('left');
  87. } else {
  88. aligns.push('');
  89. }
  90. }
  91. lineText = getLine(state, startLine).trim();
  92. if (lineText.indexOf('|') === -1) { return false; }
  93. if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
  94. columns = escapedSplit(lineText);
  95. if (columns.length && columns[0] === '') columns.shift();
  96. if (columns.length && columns[columns.length - 1] === '') columns.pop();
  97. // header row will define an amount of columns in the entire table,
  98. // and align row should be exactly the same (the rest of the rows can differ)
  99. columnCount = columns.length;
  100. if (columnCount === 0 || columnCount !== aligns.length) { return false; }
  101. if (silent) { return true; }
  102. oldParentType = state.parentType;
  103. state.parentType = 'table';
  104. // use 'blockquote' lists for termination because it's
  105. // the most similar to tables
  106. terminatorRules = state.md.block.ruler.getRules('blockquote');
  107. token = state.push('table_open', 'table', 1);
  108. token.map = tableLines = [ startLine, 0 ];
  109. token = state.push('thead_open', 'thead', 1);
  110. token.map = [ startLine, startLine + 1 ];
  111. token = state.push('tr_open', 'tr', 1);
  112. token.map = [ startLine, startLine + 1 ];
  113. for (i = 0; i < columns.length; i++) {
  114. token = state.push('th_open', 'th', 1);
  115. if (aligns[i]) {
  116. token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ];
  117. }
  118. token = state.push('inline', '', 0);
  119. token.content = columns[i].trim();
  120. token.children = [];
  121. token = state.push('th_close', 'th', -1);
  122. }
  123. token = state.push('tr_close', 'tr', -1);
  124. token = state.push('thead_close', 'thead', -1);
  125. for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
  126. if (state.sCount[nextLine] < state.blkIndent) { break; }
  127. terminate = false;
  128. for (i = 0, l = terminatorRules.length; i < l; i++) {
  129. if (terminatorRules[i](state, nextLine, endLine, true)) {
  130. terminate = true;
  131. break;
  132. }
  133. }
  134. if (terminate) { break; }
  135. lineText = getLine(state, nextLine).trim();
  136. if (!lineText) { break; }
  137. if (state.sCount[nextLine] - state.blkIndent >= 4) { break; }
  138. columns = escapedSplit(lineText);
  139. if (columns.length && columns[0] === '') columns.shift();
  140. if (columns.length && columns[columns.length - 1] === '') columns.pop();
  141. if (nextLine === startLine + 2) {
  142. token = state.push('tbody_open', 'tbody', 1);
  143. token.map = tbodyLines = [ startLine + 2, 0 ];
  144. }
  145. token = state.push('tr_open', 'tr', 1);
  146. token.map = [ nextLine, nextLine + 1 ];
  147. for (i = 0; i < columnCount; i++) {
  148. token = state.push('td_open', 'td', 1);
  149. if (aligns[i]) {
  150. token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ];
  151. }
  152. token = state.push('inline', '', 0);
  153. token.content = columns[i] ? columns[i].trim() : '';
  154. token.children = [];
  155. token = state.push('td_close', 'td', -1);
  156. }
  157. token = state.push('tr_close', 'tr', -1);
  158. }
  159. if (tbodyLines) {
  160. token = state.push('tbody_close', 'tbody', -1);
  161. tbodyLines[1] = nextLine;
  162. }
  163. token = state.push('table_close', 'table', -1);
  164. tableLines[1] = nextLine;
  165. state.parentType = oldParentType;
  166. state.line = nextLine;
  167. return true;
  168. };