3 var whitespace = require('is-whitespace-character');
5 module.exports = table;
7 var C_BACKSLASH = '\\';
16 var MIN_TABLE_COLUMNS = 1;
17 var MIN_TABLE_ROWS = 2;
19 var TABLE_ALIGN_LEFT = 'left';
20 var TABLE_ALIGN_CENTER = 'center';
21 var TABLE_ALIGN_RIGHT = 'right';
22 var TABLE_ALIGN_NONE = null;
24 function table(eat, value, silent) {
51 /* Exit when not in gfm-mode. */
52 if (!self.options.gfm) {
57 * Detecting tables soon is hard, so there are some
58 * checks for performance here, such as the minimum
59 * number of rows, and allowed characters in the
63 length = value.length + 1;
66 while (index < length) {
67 lineIndex = value.indexOf(C_NEWLINE, index);
68 pipeIndex = value.indexOf(C_PIPE, index + 1);
70 if (lineIndex === -1) {
71 lineIndex = value.length;
74 if (pipeIndex === -1 || pipeIndex > lineIndex) {
75 if (lineCount < MIN_TABLE_ROWS) {
82 lines.push(value.slice(index, lineIndex));
84 index = lineIndex + 1;
87 /* Parse the alignment row. */
88 subvalue = lines.join(C_NEWLINE);
89 alignments = lines.splice(1, 1)[0] || [];
91 length = alignments.length;
96 while (index < length) {
97 character = alignments.charAt(index);
99 if (character === C_PIPE) {
102 if (alignment === false) {
103 if (first === false) {
107 align.push(alignment);
112 } else if (character === C_DASH) {
114 alignment = alignment || TABLE_ALIGN_NONE;
115 } else if (character === C_COLON) {
116 if (alignment === TABLE_ALIGN_LEFT) {
117 alignment = TABLE_ALIGN_CENTER;
118 } else if (hasDash && alignment === TABLE_ALIGN_NONE) {
119 alignment = TABLE_ALIGN_RIGHT;
121 alignment = TABLE_ALIGN_LEFT;
123 } else if (!whitespace(character)) {
130 if (alignment !== false) {
131 align.push(alignment);
134 /* Exit when without enough columns. */
135 if (align.length < MIN_TABLE_COLUMNS) {
139 /* istanbul ignore if - never used (yet) */
144 /* Parse the rows. */
148 table = eat(subvalue).reset({
154 while (++position < lineCount) {
155 line = lines[position];
156 row = {type: 'tableRow', children: []};
158 /* Eat a newline character when this is not the
165 eat(line).reset(row, table);
167 length = line.length + 1;
175 while (index < length) {
176 character = line.charAt(index);
178 if (character === C_TAB || character === C_SPACE) {
189 if (character === '' || character === C_PIPE) {
193 if (character && opening) {
199 if ((cell || character) && !preamble) {
202 if (queue.length > 1) {
204 subvalue += queue.slice(0, queue.length - 1);
205 queue = queue.charAt(queue.length - 1);
216 children: self.tokenizeInline(cell, now)
220 eat(queue + character);
233 if (character === C_BACKSLASH && index !== length - 2) {
234 cell += line.charAt(index + 1);
238 if (character === C_TICK) {
241 while (line.charAt(index + 1) === character) {
249 } else if (count >= opening) {
259 /* Eat the alignment row. */
261 eat(C_NEWLINE + alignments);