|
|
@ -54,7 +54,7 @@ function escapedSplit(str) { |
|
|
|
|
|
|
|
|
|
|
|
module.exports = function table(state, startLine, endLine, silent) { |
|
|
|
var ch, lineText, pos, i, nextLine, rows, token, |
|
|
|
var ch, lineText, pos, i, nextLine, columns, columnCount, token, |
|
|
|
aligns, t, tableLines, tbodyLines; |
|
|
|
|
|
|
|
// should have at least three lines
|
|
|
@ -75,15 +75,14 @@ module.exports = function table(state, startLine, endLine, silent) { |
|
|
|
lineText = getLine(state, startLine + 1); |
|
|
|
if (!/^[-:| ]+$/.test(lineText)) { return false; } |
|
|
|
|
|
|
|
rows = lineText.split('|'); |
|
|
|
if (rows.length < 2) { return false; } |
|
|
|
columns = lineText.split('|'); |
|
|
|
aligns = []; |
|
|
|
for (i = 0; i < rows.length; i++) { |
|
|
|
t = rows[i].trim(); |
|
|
|
for (i = 0; i < columns.length; i++) { |
|
|
|
t = columns[i].trim(); |
|
|
|
if (!t) { |
|
|
|
// allow empty columns before and after table, but not in between columns;
|
|
|
|
// e.g. allow ` |---| `, disallow ` ---||--- `
|
|
|
|
if (i === 0 || i === rows.length - 1) { |
|
|
|
if (i === 0 || i === columns.length - 1) { |
|
|
|
continue; |
|
|
|
} else { |
|
|
|
return false; |
|
|
@ -102,8 +101,13 @@ module.exports = function table(state, startLine, endLine, silent) { |
|
|
|
|
|
|
|
lineText = getLine(state, startLine).trim(); |
|
|
|
if (lineText.indexOf('|') === -1) { return false; } |
|
|
|
rows = escapedSplit(lineText.replace(/^\||\|$/g, '')); |
|
|
|
if (aligns.length !== rows.length) { return false; } |
|
|
|
columns = escapedSplit(lineText.replace(/^\||\|$/g, '')); |
|
|
|
|
|
|
|
// header row will define an amount of columns in the entire table,
|
|
|
|
// and align row shouldn't be smaller than that (the rest of the rows can)
|
|
|
|
columnCount = columns.length; |
|
|
|
if (columnCount > aligns.length) { return false; } |
|
|
|
|
|
|
|
if (silent) { return true; } |
|
|
|
|
|
|
|
token = state.push('table_open', 'table', 1); |
|
|
@ -115,7 +119,7 @@ module.exports = function table(state, startLine, endLine, silent) { |
|
|
|
token = state.push('tr_open', 'tr', 1); |
|
|
|
token.map = [ startLine, startLine + 1 ]; |
|
|
|
|
|
|
|
for (i = 0; i < rows.length; i++) { |
|
|
|
for (i = 0; i < columns.length; i++) { |
|
|
|
token = state.push('th_open', 'th', 1); |
|
|
|
token.map = [ startLine, startLine + 1 ]; |
|
|
|
if (aligns[i]) { |
|
|
@ -123,7 +127,7 @@ module.exports = function table(state, startLine, endLine, silent) { |
|
|
|
} |
|
|
|
|
|
|
|
token = state.push('inline', '', 0); |
|
|
|
token.content = rows[i].trim(); |
|
|
|
token.content = columns[i].trim(); |
|
|
|
token.map = [ startLine, startLine + 1 ]; |
|
|
|
token.children = []; |
|
|
|
|
|
|
@ -141,20 +145,17 @@ module.exports = function table(state, startLine, endLine, silent) { |
|
|
|
|
|
|
|
lineText = getLine(state, nextLine).trim(); |
|
|
|
if (lineText.indexOf('|') === -1) { break; } |
|
|
|
rows = escapedSplit(lineText.replace(/^\||\|$/g, '')); |
|
|
|
|
|
|
|
// set number of columns to number of columns in header row
|
|
|
|
rows.length = aligns.length; |
|
|
|
columns = escapedSplit(lineText.replace(/^\||\|$/g, '')); |
|
|
|
|
|
|
|
token = state.push('tr_open', 'tr', 1); |
|
|
|
for (i = 0; i < rows.length; i++) { |
|
|
|
for (i = 0; i < columnCount; i++) { |
|
|
|
token = state.push('td_open', 'td', 1); |
|
|
|
if (aligns[i]) { |
|
|
|
token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ]; |
|
|
|
} |
|
|
|
|
|
|
|
token = state.push('inline', '', 0); |
|
|
|
token.content = rows[i] ? rows[i].trim() : ''; |
|
|
|
token.content = columns[i] ? columns[i].trim() : ''; |
|
|
|
token.children = []; |
|
|
|
|
|
|
|
token = state.push('td_close', 'td', -1); |
|
|
|