Merge remote-tracking branch 'kitsonk/gfm_tables'
Conflicts: lib/marked.js
This commit is contained in:
commit
1f09da120f
@ -13,6 +13,8 @@ var block = {
|
|||||||
newline: /^\n+/,
|
newline: /^\n+/,
|
||||||
code: /^( {4}[^\n]+\n*)+/,
|
code: /^( {4}[^\n]+\n*)+/,
|
||||||
fences: noop,
|
fences: noop,
|
||||||
|
table: noop,
|
||||||
|
nptable: noop,
|
||||||
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
|
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
|
||||||
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
|
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
|
||||||
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
|
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
|
||||||
@ -53,12 +55,16 @@ block.paragraph = replace(block.paragraph)
|
|||||||
|
|
||||||
block.normal = {
|
block.normal = {
|
||||||
fences: block.fences,
|
fences: block.fences,
|
||||||
paragraph: block.paragraph
|
paragraph: block.paragraph,
|
||||||
|
table: block.table,
|
||||||
|
nptable: block.nptable
|
||||||
};
|
};
|
||||||
|
|
||||||
block.gfm = {
|
block.gfm = {
|
||||||
fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
|
fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
|
||||||
paragraph: /^/
|
paragraph: /^/,
|
||||||
|
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)
|
block.gfm.paragraph = replace(block.paragraph)
|
||||||
@ -126,6 +132,44 @@ block.token = function(src, tokens, top) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// table (gfm)
|
||||||
|
if (cap = block.table.exec(src)) {
|
||||||
|
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"
|
||||||
|
: false;
|
||||||
|
}),
|
||||||
|
cells: cap[3].replace(/( *[|] *)?\n$/, '').split("\n").map(function(row){
|
||||||
|
return row.replace(/(^ *[|] *| *[|] *$)/g, '').split(/ *[|] */);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// table no leading pipe (gfm)
|
||||||
|
if (cap = block.nptable.exec(src)) {
|
||||||
|
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"
|
||||||
|
: false;
|
||||||
|
}),
|
||||||
|
cells: cap[3].replace(/\n$/, '').split("\n").map(function(row){
|
||||||
|
return row.split(/ *[|] */);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// heading
|
// heading
|
||||||
if (cap = block.heading.exec(src)) {
|
if (cap = block.heading.exec(src)) {
|
||||||
src = src.substring(cap[0].length);
|
src = src.substring(cap[0].length);
|
||||||
@ -300,7 +344,7 @@ block.token = function(src, tokens, top) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var inline = {
|
var inline = {
|
||||||
escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
|
escape: /^\\([\\`*{}\[\]()#+\-.!_>|])/,
|
||||||
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
|
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
|
||||||
url: noop,
|
url: noop,
|
||||||
tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
|
tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
|
||||||
@ -559,6 +603,48 @@ function tok() {
|
|||||||
+ token.text
|
+ token.text
|
||||||
+ '</code></pre>\n';
|
+ '</code></pre>\n';
|
||||||
}
|
}
|
||||||
|
case 'table': {
|
||||||
|
var thead = '\t<thead>\n\t\t<tr>';
|
||||||
|
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":
|
||||||
|
thead += '<th align="' + align + '">' + heading + '</td>';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
thead += '<th>' + heading + '</th>';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thead += '</tr>\n\t</thead>\n';
|
||||||
|
|
||||||
|
var tbody = '\t<tbody>\n';
|
||||||
|
token.cells.forEach(function(row){
|
||||||
|
tbody += '\t\t<tr>';
|
||||||
|
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":
|
||||||
|
tbody += '<td align="' + align + '">' + cell + '</td>';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
tbody += '<td>' + cell + '</td>';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
tbody += '</tr>\n';
|
||||||
|
});
|
||||||
|
tbody += '\t</tbody>\n';
|
||||||
|
|
||||||
|
return '<table>\n'
|
||||||
|
+ thead
|
||||||
|
+ tbody
|
||||||
|
+ '</table>\n'
|
||||||
|
}
|
||||||
case 'blockquote_start': {
|
case 'blockquote_start': {
|
||||||
var body = '';
|
var body = '';
|
||||||
|
|
||||||
@ -740,11 +826,15 @@ function setOptions(opt) {
|
|||||||
if (options.gfm) {
|
if (options.gfm) {
|
||||||
block.fences = block.gfm.fences;
|
block.fences = block.gfm.fences;
|
||||||
block.paragraph = block.gfm.paragraph;
|
block.paragraph = block.gfm.paragraph;
|
||||||
|
block.table = block.gfm.table;
|
||||||
|
block.nptable = block.gfm.nptable;
|
||||||
inline.text = inline.gfm.text;
|
inline.text = inline.gfm.text;
|
||||||
inline.url = inline.gfm.url;
|
inline.url = inline.gfm.url;
|
||||||
} else {
|
} else {
|
||||||
block.fences = block.normal.fences;
|
block.fences = block.normal.fences;
|
||||||
block.paragraph = block.normal.paragraph;
|
block.paragraph = block.normal.paragraph;
|
||||||
|
block.table = block.normal.table;
|
||||||
|
block.nptable = block.normal.table;
|
||||||
inline.text = inline.normal.text;
|
inline.text = inline.normal.text;
|
||||||
inline.url = inline.normal.url;
|
inline.url = inline.normal.url;
|
||||||
}
|
}
|
||||||
|
38
test/new/gfm_tables.html
Normal file
38
test/new/gfm_tables.html
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th>Heading 1</th><th>Heading 2</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>Cell 1</td><td>Cell 2</td></tr>
|
||||||
|
<tr><td>Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th align="center">Header 1</td><th align="right">Header 2</td><th align="left">Header 3</td><th>Header 4</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td align="center">Cell 1</td><td align="right">Cell 2</td><td align="left">Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
<tr><td align="center">Cell 5</td><td align="right">Cell 6</td><td align="left">Cell 7</td><td>Cell 8</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<pre><code>Test code</code></pre>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th>Header 1</th><th>Header 2</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>Cell 1</td><td>Cell 2</td></tr>
|
||||||
|
<tr><td>Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th align="left">Header 1</td><th align="center">Header 2</td><th align="right">Header 3</td><th>Header 4</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td align="left">Cell 1</td><td align="center">Cell 2</td><td align="right">Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
<tr><td align="left"><em>Cell 5</em></td><td align="center">Cell 6</td><td align="right">Cell 7</td><td>Cell 8</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
21
test/new/gfm_tables.text
Normal file
21
test/new/gfm_tables.text
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
| Heading 1 | Heading 2
|
||||||
|
| --------- | ---------
|
||||||
|
| Cell 1 | Cell 2
|
||||||
|
| Cell 3 | Cell 4
|
||||||
|
|
||||||
|
| Header 1 | Header 2 | Header 3 | Header 4 |
|
||||||
|
| :------: | -------: | :------- | -------- |
|
||||||
|
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
|
||||||
|
| Cell 5 | Cell 6 | Cell 7 | Cell 8 |
|
||||||
|
|
||||||
|
Test code
|
||||||
|
|
||||||
|
Header 1 | Header 2
|
||||||
|
-------- | --------
|
||||||
|
Cell 1 | Cell 2
|
||||||
|
Cell 3 | Cell 4
|
||||||
|
|
||||||
|
Header 1|Header 2|Header 3|Header 4
|
||||||
|
:-------|:------:|-------:|--------
|
||||||
|
Cell 1 |Cell 2 |Cell 3 |Cell 4
|
||||||
|
*Cell 5*|Cell 6 |Cell 7 |Cell 8
|
38
test/tests/gfm_tables.html
Normal file
38
test/tests/gfm_tables.html
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th>Heading 1</th><th>Heading 2</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>Cell 1</td><td>Cell 2</td></tr>
|
||||||
|
<tr><td>Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th align="center">Header 1</td><th align="right">Header 2</td><th align="left">Header 3</td><th>Header 4</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td align="center">Cell 1</td><td align="right">Cell 2</td><td align="left">Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
<tr><td align="center">Cell 5</td><td align="right">Cell 6</td><td align="left">Cell 7</td><td>Cell 8</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<pre><code>Test code</code></pre>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th>Header 1</th><th>Header 2</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>Cell 1</td><td>Cell 2</td></tr>
|
||||||
|
<tr><td>Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr><th align="left">Header 1</td><th align="center">Header 2</td><th align="right">Header 3</td><th>Header 4</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td align="left">Cell 1</td><td align="center">Cell 2</td><td align="right">Cell 3</td><td>Cell 4</td></tr>
|
||||||
|
<tr><td align="left"><em>Cell 5</em></td><td align="center">Cell 6</td><td align="right">Cell 7</td><td>Cell 8</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
21
test/tests/gfm_tables.text
Normal file
21
test/tests/gfm_tables.text
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
| Heading 1 | Heading 2
|
||||||
|
| --------- | ---------
|
||||||
|
| Cell 1 | Cell 2
|
||||||
|
| Cell 3 | Cell 4
|
||||||
|
|
||||||
|
| Header 1 | Header 2 | Header 3 | Header 4 |
|
||||||
|
| :------: | -------: | :------- | -------- |
|
||||||
|
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
|
||||||
|
| Cell 5 | Cell 6 | Cell 7 | Cell 8 |
|
||||||
|
|
||||||
|
Test code
|
||||||
|
|
||||||
|
Header 1 | Header 2
|
||||||
|
-------- | --------
|
||||||
|
Cell 1 | Cell 2
|
||||||
|
Cell 3 | Cell 4
|
||||||
|
|
||||||
|
Header 1|Header 2|Header 3|Header 4
|
||||||
|
:-------|:------:|-------:|--------
|
||||||
|
Cell 1 |Cell 2 |Cell 3 |Cell 4
|
||||||
|
*Cell 5*|Cell 6 |Cell 7 |Cell 8
|
Loading…
x
Reference in New Issue
Block a user