UNPKG

8.31 kBJavaScriptView Raw
1'use strict';
2
3module.exports = function multimd_table_plugin(md) {
4 function getLine(state, line) {
5 var pos = state.bMarks[line] + state.blkIndent,
6 max = state.eMarks[line];
7
8 return state.src.slice(pos, max);
9 }
10
11 function escapedSplit(str) {
12 var result = [],
13 pos = 0,
14 max = str.length,
15 lastPos = 0,
16 escaped = false,
17 backTicked = false;
18
19 for (pos = 0; pos < max; pos++) {
20 switch (str.charCodeAt(pos)) {
21 case 0x5c/* \ */:
22 escaped = true;
23 break;
24 case 0x60/* ` */:
25 if (backTicked || !escaped) {
26 // make \` close code sequence, but not open it;
27 // the reason is: `\` is correct code block
28 backTicked = !backTicked;
29 }
30 escaped = false;
31 break;
32 case 0x7c/* | */:
33 if (!backTicked && !escaped) {
34 result.push(str.slice(lastPos, pos));
35 lastPos = pos + 1;
36 }
37 escaped = false;
38 break;
39 default:
40 escaped = false;
41 break;
42 }
43 }
44
45 result.push(str.slice(lastPos));
46
47 return result;
48 }
49
50 function countColspan(columns) {
51 var i, emptyCount, colspans;
52
53 emptyCount = 0;
54 colspans = [];
55 for (i = columns.length - 1; i >= 0; i--) {
56 if (columns[i]) {
57 colspans.unshift(emptyCount + 1);
58 emptyCount = 0;
59 } else {
60 emptyCount++;
61 }
62 }
63 if (emptyCount > 0) {
64 colspans.unshift(emptyCount + 1);
65 }
66
67 return colspans;
68 }
69
70 function caption(state, lineText, lineNum, silent) {
71 var captionInfo, result, token;
72
73 result = lineText.match(/^\[([^[\]]+)\](\[([^[\]]+)\])?\s*$/);
74 if (!result) { return false; }
75 if (silent) { return true; }
76
77 captionInfo = { caption: null, label: null };
78 captionInfo.content = result[1];
79 captionInfo.label = result[2] || result[1];
80
81 token = state.push('caption_open', 'caption', 1);
82 token.map = [ lineNum, lineNum + 1 ];
83 token.attrs = [ [ 'id', captionInfo.label.toLowerCase().replace(/\W+/g, '') ] ];
84
85 token = state.push('inline', '', 0);
86 token.content = captionInfo.content;
87 token.map = [ lineNum, lineNum + 1 ];
88 token.children = [];
89
90 token = state.push('caption_close', 'caption', -1);
91
92 return captionInfo;
93 }
94
95 function tableRow(state, lineText, lineNum, silent, seperatorInfo, rowType) {
96 var rowInfo, columns, token, i, col;
97
98 columns = escapedSplit(lineText.replace(/^\||([^\\])\|$/g, '$1'));
99 // lineText does not contain valid pipe character
100 if (columns.length === 1 && !/^\||[^\\]\|$/.test(lineText)) { return false; }
101 if (silent) { return true; }
102 // console.log(lineText + ': ' + columns.length);
103
104 rowInfo = { colspans: null, columns: null };
105 rowInfo.columns = columns.filter(Boolean);
106 rowInfo.colspans = countColspan(columns);
107
108 token = state.push('tr_open', 'tr', 1);
109 token.map = [ lineNum, lineNum + 1 ];
110
111 for (i = 0, col = 0; i < rowInfo.columns.length && col < seperatorInfo.aligns.length;
112 col += rowInfo.colspans[i], i++) {
113 // console.log(col)
114 token = state.push(rowType + '_open', rowType, 1);
115 token.map = [ lineNum, lineNum + 1 ];
116 token.attrs = [];
117 if (seperatorInfo.aligns[col]) {
118 token.attrs.push([ 'style', 'text-align:' + seperatorInfo.aligns[col] ]);
119 }
120 if (seperatorInfo.wraps[col]) {
121 token.attrs.push([ 'class', 'extend' ]);
122 }
123 if (rowInfo.colspans[i] > 1) {
124 token.attrs.push([ 'colspan', rowInfo.colspans[i] ]);
125 }
126
127 token = state.push('inline', '', 0);
128 token.content = rowInfo.columns[i].trim();
129 token.map = [ lineNum, lineNum + 1 ];
130 token.children = [];
131
132 token = state.push(rowType + '_close', rowType, -1);
133 }
134
135 token = state.push('tr_close', 'tr', -1);
136
137 return rowInfo;
138 }
139
140 function seperator(state, lineText, lineNum, silent) {
141 var columns, seperatorInfo, i, t;
142
143 columns = escapedSplit(lineText.replace(/^\||([^\\])\|$/g, '$1'));
144 // lineText does not contain valid pipe character
145 if (columns.length === 1 && !/^\||[^\\]\|$/.test(lineText)) { return false; }
146
147 seperatorInfo = { aligns: [], wraps: [] };
148
149 for (i = 0; i < columns.length; i++) {
150 t = columns[i].trim();
151 // console.log(t);
152 if (!/^:?(-+|=+):?\+?$/.test(t)) { return false; }
153
154 seperatorInfo.wraps.push(t.charCodeAt(t.length - 1) === 0x2B/* + */);
155 if (seperatorInfo.wraps[i]) { t = t.slice(0, -1); }
156
157 switch (((t.charCodeAt(0) === 0x3A/* : */) << 4) +
158 (t.charCodeAt(t.length - 1) === 0x3A/* : */)) {
159 case 0x00: seperatorInfo.aligns.push(''); break;
160 case 0x01: seperatorInfo.aligns.push('right'); break;
161 case 0x10: seperatorInfo.aligns.push('left'); break;
162 case 0x11: seperatorInfo.aligns.push('center'); break;
163 }
164 }
165
166 return silent || seperatorInfo;
167 }
168
169 function table(state, startLine, endLine, silent) {
170 // Regex pseudo code for table:
171 // caption? tableRow+ seperator (tableRow+ | empty)* caption?
172 var seperatorLine, captionAtFirst, captionAtLast, lineText, nextLine,
173 seperatorInfo, token, tableLines,
174 tbodyLines, emptyTBody;
175
176 if (startLine + 2 > endLine) { return false; }
177 if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
178
179 captionAtFirst = captionAtLast = false;
180
181 // first line
182 lineText = getLine(state, startLine).trim();
183 if (caption(state, lineText, startLine, true)) {
184 captionAtFirst = true;
185 } else if (!tableRow(state, lineText, startLine, true, null, 'tr')) {
186 return false;
187 }
188
189 // second line ~ seperator line
190 for (nextLine = startLine + 1; nextLine < endLine; nextLine++) {
191 if (state.sCount[nextLine] - state.blkIndent >= 4) { return false; }
192 lineText = getLine(state, nextLine).trim();
193 if (seperator(state, lineText, nextLine, true)) {
194 seperatorLine = nextLine;
195 break;
196 } else if (tableRow(state, lineText, nextLine, true, null, 'th')) {
197 continue;
198 } else {
199 return false;
200 }
201 }
202 if (!seperatorLine) { return false; }
203 if (silent) { return true; }
204
205 token = state.push('table_open', 'table', 1);
206 token.map = tableLines = [ startLine, 0 ];
207
208 seperatorInfo = seperator(state, lineText, seperatorLine, false);
209
210 if (captionAtFirst) {
211 lineText = getLine(state, startLine).trim();
212 caption(state, lineText, startLine, false);
213 }
214
215 token = state.push('thead_open', 'thead', 1);
216 token.map = [ startLine + captionAtFirst, seperatorLine ];
217
218 for (nextLine = startLine + captionAtFirst; nextLine < seperatorLine; nextLine++) {
219
220 lineText = getLine(state, nextLine).trim();
221 tableRow(state, lineText, nextLine, false, seperatorInfo, 'th');
222 }
223
224 token = state.push('thead_close', 'thead', -1);
225
226 emptyTBody = true;
227
228 token = state.push('tbody_open', 'tbody', 1);
229 token.map = tbodyLines = [ seperatorLine + 1, 0 ];
230
231 for (nextLine = seperatorLine + 1; nextLine < endLine; nextLine++) {
232 lineText = getLine(state, nextLine).trim();
233
234 if (state.sCount[nextLine] - state.blkIndent >= 4) {
235 break;
236 } else if (!captionAtFirst && caption(state, lineText, nextLine, true)) {
237 captionAtLast = true;
238 break;
239 } else if (tableRow(state, lineText, nextLine, false, seperatorInfo, 'td')) {
240 emptyTBody = false;
241 } else if (!emptyTBody && !lineText) {
242 tbodyLines[1] = nextLine - 1;
243 token = state.push('tbody_close', 'tbody', -1);
244 token = state.push('tbody_open', 'tbody', 1);
245 token.map = tbodyLines = [ nextLine + 1, 0 ];
246 emptyTBody = true;
247 } else {
248 break;
249 }
250 }
251 token = state.push('tbody_close', 'tbody', -1);
252
253 if (captionAtLast) {
254 caption(state, lineText, nextLine, false);
255 nextLine++;
256 }
257
258 token = state.push('table_close', 'table', -1);
259
260 tableLines[1] = tbodyLines[1] = nextLine;
261 state.line = nextLine;
262 return true;
263 }
264
265 md.block.ruler.at('table', table, { alt: [ 'paragraph', 'reference' ] });
266};