UNPKG

9.14 kBJavaScriptView Raw
1/*! markdown-it-multimd-table 3.0.0 https://github.com//markdown-it/markdown-it-multimd-table @license MIT */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitDeflist = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2'use strict';
3
4module.exports = function multimd_table_plugin(md) {
5 function getLine(state, line) {
6 var pos = state.bMarks[line] + state.blkIndent,
7 max = state.eMarks[line];
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
103 rowInfo = { colspans: null, columns: null };
104 rowInfo.columns = columns.filter(Boolean);
105 rowInfo.colspans = countColspan(columns);
106
107 token = state.push('tr_open', 'tr', 1);
108 token.map = [ lineNum, lineNum + 1 ];
109
110 for (i = 0, col = 0; i < rowInfo.columns.length && col < seperatorInfo.aligns.length;
111 col += rowInfo.colspans[i], i++) {
112 token = state.push(rowType + '_open', rowType, 1);
113 token.map = [ lineNum, lineNum + 1 ];
114 token.attrs = [];
115 if (seperatorInfo.aligns[col]) {
116 token.attrs.push([ 'style', 'text-align:' + seperatorInfo.aligns[col] ]);
117 }
118 if (seperatorInfo.wraps[col]) {
119 token.attrs.push([ 'class', 'extend' ]);
120 }
121 if (rowInfo.colspans[i] > 1) {
122 token.attrs.push([ 'colspan', rowInfo.colspans[i] ]);
123 }
124
125 token = state.push('inline', '', 0);
126 token.content = rowInfo.columns[i].trim();
127 token.map = [ lineNum, lineNum + 1 ];
128 token.children = [];
129
130 token = state.push(rowType + '_close', rowType, -1);
131 }
132
133 token = state.push('tr_close', 'tr', -1);
134
135 return rowInfo;
136 }
137
138 function seperator(state, lineText, lineNum, silent) {
139 var columns, seperatorInfo, i, t;
140
141 // lineText have code indentation
142 if (state.sCount[lineNum] - state.blkIndent >= 4) { return false; }
143
144 // lineText does not contain valid pipe character
145 columns = escapedSplit(lineText.replace(/^\||([^\\])\|$/g, '$1'));
146 if (columns.length === 1 && !/^\||[^\\]\|$/.test(lineText)) { return false; }
147
148 seperatorInfo = { aligns: [], wraps: [] };
149
150 for (i = 0; i < columns.length; i++) {
151 t = columns[i].trim();
152 if (!/^:?(-+|=+):?\+?$/.test(t)) { return false; }
153
154 seperatorInfo.wraps.push(t.charCodeAt(t.length - 1) === 0x2B/* + */);
155 if (seperatorInfo.wraps[i]) {
156 t = t.slice(0, -1);
157 }
158
159 switch (((t.charCodeAt(0) === 0x3A/* : */) << 4) +
160 (t.charCodeAt(t.length - 1) === 0x3A/* : */)) {
161 case 0x00: seperatorInfo.aligns.push(''); break;
162 case 0x01: seperatorInfo.aligns.push('right'); break;
163 case 0x10: seperatorInfo.aligns.push('left'); break;
164 case 0x11: seperatorInfo.aligns.push('center'); break;
165 }
166 }
167
168 return silent || seperatorInfo;
169 }
170
171 function table(state, startLine, endLine, silent) {
172 /* Regex pseudo code for table:
173 * caption? tableRow+ seperator (tableRow+ | empty)* caption?
174 */
175 var seperatorLine, captionAtFirst, captionAtLast, lineText, nextLine,
176 seperatorInfo, token, tableLines, tbodyLines, emptyTBody;
177
178 if (startLine + 2 > endLine) { return false; }
179
180 captionAtFirst = captionAtLast = false;
181
182 // first line
183 lineText = getLine(state, startLine).trim();
184 if (caption(state, lineText, startLine, true)) {
185 captionAtFirst = true;
186 } else if (!tableRow(state, lineText, startLine, true, null, 'tr')) {
187 return false;
188 }
189
190 // second line ~ seperator line
191 for (nextLine = startLine + 1; nextLine < endLine; nextLine++) {
192 lineText = getLine(state, nextLine).trim();
193
194 if (seperator(state, lineText, nextLine, true)) {
195 seperatorLine = nextLine;
196 break;
197 } else if (tableRow(state, lineText, nextLine, true, null, 'th')) {
198 continue;
199 } else {
200 return false;
201 }
202 }
203 if (!seperatorLine) { return false; }
204 if (silent) { return true; }
205
206 token = state.push('table_open', 'table', 1);
207 token.map = tableLines = [ startLine, 0 ];
208
209 seperatorInfo = seperator(state, lineText, seperatorLine, false);
210
211 if (captionAtFirst) {
212 lineText = getLine(state, startLine).trim();
213 caption(state, lineText, startLine, false);
214 }
215
216 token = state.push('thead_open', 'thead', 1);
217 token.map = [ startLine + captionAtFirst, seperatorLine ];
218
219 for (nextLine = startLine + captionAtFirst; nextLine < seperatorLine; nextLine++) {
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 token = state.push('tbody_open', 'tbody', 1);
227 token.map = tbodyLines = [ seperatorLine + 1, 0 ];
228 emptyTBody = true;
229
230 for (nextLine = seperatorLine + 1; nextLine < endLine; nextLine++) {
231 lineText = getLine(state, nextLine).trim();
232
233 if (!captionAtFirst && caption(state, lineText, nextLine, true)) {
234 captionAtLast = true;
235 break;
236 } else if (tableRow(state, lineText, nextLine, false, seperatorInfo, 'td')) {
237 emptyTBody = false;
238 } else if (!emptyTBody && !lineText) {
239 tbodyLines[1] = nextLine - 1;
240 token = state.push('tbody_close', 'tbody', -1);
241 token = state.push('tbody_open', 'tbody', 1);
242 token.map = tbodyLines = [ nextLine + 1, 0 ];
243 emptyTBody = true;
244 } else {
245 break;
246 }
247 }
248 token = state.push('tbody_close', 'tbody', -1);
249
250 if (captionAtLast) {
251 caption(state, lineText, nextLine, false);
252 nextLine++;
253 }
254
255 token = state.push('table_close', 'table', -1);
256
257 tableLines[1] = tbodyLines[1] = nextLine;
258 state.line = nextLine;
259 return true;
260 }
261
262 md.block.ruler.at('table', table, { alt: [ 'paragraph', 'reference' ] });
263
264};
265
266/* vim: set ts=2 sw=2 et: */
267
268},{}]},{},[1])(1)
269});
\No newline at end of file