Skip to content

Commit 05e322c

Browse files
authored
Merge pull request #1262 from UziTech/table-cells
Fix table cells
2 parents ac4f2e4 + 9407cef commit 05e322c

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

lib/marked.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,22 @@ function merge(obj) {
13401340
}
13411341

13421342
function splitCells(tableRow, count) {
1343-
var cells = tableRow.replace(/([^\\])\|/g, '$1 |').split(/ +\| */),
1343+
// ensure that every cell-delimiting pipe has a space
1344+
// before it to distinguish it from an escaped pipe
1345+
var row = tableRow.replace(/\|/g, function (match, offset, str) {
1346+
var escaped = false,
1347+
curr = offset;
1348+
while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
1349+
if (escaped) {
1350+
// odd number of slashes means | is escaped
1351+
// so we leave it alone
1352+
return '|';
1353+
} else {
1354+
// add space before unescaped |
1355+
return ' |';
1356+
}
1357+
}),
1358+
cells = row.split(/ \|/),
13441359
i = 0;
13451360

13461361
if (cells.length > count) {
@@ -1350,7 +1365,8 @@ function splitCells(tableRow, count) {
13501365
}
13511366

13521367
for (; i < cells.length; i++) {
1353-
cells[i] = cells[i].replace(/\\\|/g, '|');
1368+
// leading or trailing whitespace is ignored per the gfm spec
1369+
cells[i] = cells[i].trim().replace(/\\\|/g, '|');
13541370
}
13551371
return cells;
13561372
}

test/specs/marked/marked-spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,18 @@ describe('Marked Code spans', function() {
4646
messenger.test(spec, section, ignore);
4747
});
4848
});
49+
50+
describe('Marked Table cells', function() {
51+
var section = 'Table cells';
52+
53+
// var shouldPassButFails = [];
54+
var shouldPassButFails = [];
55+
56+
var willNotBeAttemptedByCoreTeam = [];
57+
58+
var ignore = shouldPassButFails.concat(willNotBeAttemptedByCoreTeam);
59+
60+
markedSpec.forEach(function(spec) {
61+
messenger.test(spec, section, ignore);
62+
});
63+
});

test/specs/marked/marked.json

+49-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,55 @@
22
{
33
"section": "Code spans",
44
"markdown": "`[email protected]`",
5-
"html": "<p><code>someone@exmaple.com</code></p>\n",
5+
"html": "<p><code>someone@example.com</code></p>",
66
"example": 1
7+
},
8+
{
9+
"section": "Table cells",
10+
"markdown": "|1|\n|-|\n|1|",
11+
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>1</td></tr></tbody></table>",
12+
"example": 2
13+
},
14+
{
15+
"section": "Table cells",
16+
"markdown": "|1|\n|-|\n|\\||",
17+
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>|</td></tr></tbody></table>",
18+
"example": 3
19+
},
20+
{
21+
"section": "Table cells",
22+
"markdown": "|1|\n|-|\n|1\\\\1|",
23+
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>1\\1</td></tr></tbody></table>",
24+
"example": 4
25+
},
26+
{
27+
"section": "Table cells",
28+
"markdown": "|1|\n|-|\n|\\\\\\\\||",
29+
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>\\\\</td></tr></tbody></table>",
30+
"example": 5
31+
},
32+
{
33+
"section": "Table cells",
34+
"markdown": "|1|\n|-|\n|\\\\\\\\\\||",
35+
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>\\\\|</td></tr></tbody></table>",
36+
"example": 6
37+
},
38+
{
39+
"section": "Table cells",
40+
"markdown": "|1|2|\n|-|-|\n||2|",
41+
"html": "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>",
42+
"example": 7
43+
},
44+
{
45+
"section": "Table cells",
46+
"markdown": "|1|2|\n|-|-|\n|1\\|\\\\|2\\|\\\\|",
47+
"html": "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1|\\</td><td>2|\\</td></tr></tbody></table>",
48+
"example": 8
49+
},
50+
{
51+
"section": "Table cells",
52+
"markdown": "|1|2|\n|-|-|\n| |2|",
53+
"html": "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>",
54+
"example": 9
755
}
856
]

0 commit comments

Comments
 (0)