refactor table implementation.
This commit is contained in:
parent
1f09da120f
commit
a412613f06
@ -63,8 +63,8 @@ block.normal = {
|
||||
block.gfm = {
|
||||
fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
|
||||
paragraph: /^/,
|
||||
table: /^ {0,3}[|](.+)\n {0,3}[|]( *[-:]+[-| :]*)\n((?: *[|].*\n)*)\n*/,
|
||||
nptable: /^ {0,3}(\S.*[|].*)\n {0,3}([-:]+ *[|][-| :]*)\n((?:.*[|].*\n)*)\n*/
|
||||
table: /^ {0,3}\|(.+)\n {0,3}\|( *[-:]+[-| :]*)\n((?: *\|.*\n)*)\n*/,
|
||||
nptable: /^ {0,3}(\S.*\|.*)\n {0,3}([-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*)\n*/
|
||||
};
|
||||
|
||||
block.gfm.paragraph = replace(block.paragraph)
|
||||
@ -137,15 +137,18 @@ block.token = function(src, tokens, top) {
|
||||
src = src.substring(cap[0].length);
|
||||
tokens.push({
|
||||
type: 'table',
|
||||
header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */),
|
||||
align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){
|
||||
return row.match(/^ *-+: *$/) ? "right"
|
||||
: row.match(/^ *:-+: *$/) ? "center"
|
||||
: row.match(/^ *:-+ *$/) ? "left"
|
||||
header: cap[1].replace(/(^ *| *\| *$)/g, '').split(/ *\| */),
|
||||
align: cap[2].replace(/(^ *|\| *$)/g, '').split(/ *\| */).map(function(row) {
|
||||
return /^ *-+: *$/.test(row)
|
||||
? 'right'
|
||||
: /^ *:-+: *$/.test(row)
|
||||
? 'center'
|
||||
: /^ *:-+ *$/.test(row)
|
||||
? 'left'
|
||||
: false;
|
||||
}),
|
||||
cells: cap[3].replace(/( *[|] *)?\n$/, '').split("\n").map(function(row){
|
||||
return row.replace(/(^ *[|] *| *[|] *$)/g, '').split(/ *[|] */);
|
||||
cells: cap[3].replace(/( *\| *)?\n$/, '').split('\n').map(function(row) {
|
||||
return row.replace(/(^ *\| *| *\| *$)/g, '').split(/ *\| */);
|
||||
})
|
||||
});
|
||||
continue;
|
||||
@ -156,15 +159,18 @@ block.token = function(src, tokens, top) {
|
||||
src = src.substring(cap[0].length);
|
||||
tokens.push({
|
||||
type: 'table',
|
||||
header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */),
|
||||
align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){
|
||||
return row.match(/^ *-+: *$/) ? "right"
|
||||
: row.match(/^ *:-+: *$/) ? "center"
|
||||
: row.match(/^ *:-+ *$/) ? "left"
|
||||
header: cap[1].replace(/(^ *| *\| *$)/g, '').split(/ *\| */),
|
||||
align: cap[2].replace(/(^ *|\| *$)/g, '').split(/ *\| */).map(function(row) {
|
||||
return /^ *-+: *$/.test(row)
|
||||
? 'right'
|
||||
: /^ *:-+: *$/.test(row)
|
||||
? 'center'
|
||||
: /^ *:-+ *$/.test(row)
|
||||
? 'left'
|
||||
: false;
|
||||
}),
|
||||
cells: cap[3].replace(/\n$/, '').split("\n").map(function(row){
|
||||
return row.split(/ *[|] */);
|
||||
cells: cap[3].replace(/\n$/, '').split('\n').map(function(row) {
|
||||
return row.split(/ *\| */);
|
||||
})
|
||||
});
|
||||
continue;
|
||||
@ -605,13 +611,17 @@ function tok() {
|
||||
}
|
||||
case 'table': {
|
||||
var thead = '\t<thead>\n\t\t<tr>';
|
||||
token.header.forEach(function(heading, i){
|
||||
token.header.forEach(function(heading, i) {
|
||||
heading = inline.lexer(heading);
|
||||
var align = i < token.align.length ? token.align[i] : false;
|
||||
switch(align){
|
||||
case "left":
|
||||
case "right":
|
||||
case "center":
|
||||
|
||||
var align = i < token.align.length
|
||||
? token.align[i]
|
||||
: false;
|
||||
|
||||
switch (align) {
|
||||
case 'left':
|
||||
case 'right':
|
||||
case 'center':
|
||||
thead += '<th align="' + align + '">' + heading + '</td>';
|
||||
break;
|
||||
default:
|
||||
@ -621,15 +631,19 @@ function tok() {
|
||||
thead += '</tr>\n\t</thead>\n';
|
||||
|
||||
var tbody = '\t<tbody>\n';
|
||||
token.cells.forEach(function(row){
|
||||
token.cells.forEach(function(row) {
|
||||
tbody += '\t\t<tr>';
|
||||
row.forEach(function(cell, i){
|
||||
row.forEach(function(cell, i) {
|
||||
cell = inline.lexer(cell);
|
||||
var align = i < token.align.length ? token.align[i] : false;
|
||||
switch(align){
|
||||
case "left":
|
||||
case "right":
|
||||
case "center":
|
||||
|
||||
var align = i < token.align.length
|
||||
? token.align[i]
|
||||
: false;
|
||||
|
||||
switch (align) {
|
||||
case 'left':
|
||||
case 'right':
|
||||
case 'center':
|
||||
tbody += '<td align="' + align + '">' + cell + '</td>';
|
||||
break;
|
||||
default:
|
||||
|
Loading…
x
Reference in New Issue
Block a user