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