Skip to content

Commit 52b12b2

Browse files
wooormsethvincent
authored andcommitted
Add support for <table> (#17)
1 parent 93c2a0e commit 52b12b2

File tree

8 files changed

+232
-2
lines changed

8 files changed

+232
-2
lines changed

handlers/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
'use strict';
22

3+
var all = require('../all');
4+
35
exports.root = require('./root');
46
exports.text = require('./text');
57
exports.comment = require('./comment');
68

7-
exports.head = exports.math = exports.script = exports.style = exports.svg =
8-
exports.template = exports.title = ignore;
9+
exports.caption =
10+
exports.colgroup =
11+
exports.head =
12+
exports.math =
13+
exports.script =
14+
exports.style =
15+
exports.svg =
16+
exports.template =
17+
exports.title = ignore;
18+
19+
exports.thead =
20+
exports.tbody =
21+
exports.tfoot = children;
922

1023
exports.ol = exports.ul = require('./list');
24+
exports.table = require('./table');
25+
exports.tr = require('./table-row');
26+
exports.th = exports.td = require('./table-cell');
1127
exports.li = require('./list-item');
1228
exports.strong = exports.b = require('./strong');
1329
exports.em = exports.i = require('./emphasis');
@@ -27,3 +43,7 @@ exports.h1 = exports.h2 = exports.h3 =
2743
exports.a = require('./link');
2844

2945
function ignore() {}
46+
47+
function children(h, node) {
48+
return all(h, node);
49+
}

handlers/table-cell.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = cell;
4+
5+
var all = require('../all');
6+
7+
function cell(h, node) {
8+
return h(node, 'tableCell', all(h, node));
9+
}

handlers/table-row.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = row;
4+
5+
var all = require('../all');
6+
7+
function row(h, node) {
8+
return h(node, 'tableRow', all(h, node));
9+
}

handlers/table.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
module.exports = table;
4+
5+
var visit = require('unist-util-visit');
6+
var all = require('../all');
7+
8+
function table(h, node) {
9+
var align = alignment(node);
10+
return h(node, 'table', {align: align}, patch(all(h, node), align.length));
11+
}
12+
13+
/* Infer the alignment of the table. */
14+
function alignment(node) {
15+
var align = [];
16+
17+
visit(node, visitor);
18+
19+
return align;
20+
21+
function visitor(child, index, parent) {
22+
var pos;
23+
24+
if (cell(child)) {
25+
pos = cellsBefore(parent, child);
26+
if (!align[pos]) {
27+
align[pos] = infer(child) || null;
28+
}
29+
}
30+
}
31+
}
32+
33+
/* Get the alignment of a cell. */
34+
function infer(node) {
35+
return node.properties.align;
36+
}
37+
38+
/* Count cells in `parent` before `node`. */
39+
function cellsBefore(parent, node) {
40+
var children = parent.children;
41+
var length = children.length;
42+
var index = -1;
43+
var child;
44+
var pos = 0;
45+
46+
while (++index < length) {
47+
child = children[index];
48+
49+
if (child === node) {
50+
break;
51+
}
52+
53+
if (cell(child)) {
54+
pos++;
55+
}
56+
}
57+
58+
return pos;
59+
}
60+
61+
/* Check if `node` is a cell. */
62+
function cell(node) {
63+
return node.tagName === 'th' || node.tagName === 'td';
64+
}
65+
66+
/* Ensure the amount of cells in a row matches `align.left`. */
67+
function patch(rows, count) {
68+
var length = rows.length;
69+
var index = -1;
70+
71+
while (++index < length) {
72+
one(rows[index], count);
73+
}
74+
75+
return rows;
76+
}
77+
78+
function one(row, count) {
79+
var children = row.children;
80+
var length = count + 1;
81+
var index = children.length;
82+
83+
while (++index < length) {
84+
children.push({type: 'tableCell', children: []});
85+
}
86+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"hast-util-to-string": "^1.0.0",
3333
"trim-lines": "^1.1.0",
3434
"unist-builder": "^1.0.2",
35+
"unist-util-visit": "^1.1.1",
3536
"xtend": "^4.0.1"
3637
},
3738
"devDependencies": {

tests/fixtures/table/index.html

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>Heading 1</th>
5+
<th>Heading 2</th>
6+
<th>Heading 3</th>
7+
<th>Heading 4</th>
8+
</tr>
9+
</thead>
10+
<tbody>
11+
<tr>
12+
<td>Cell 1</td>
13+
<td>Cell 2</td>
14+
<td>Cell 3</td>
15+
<td>Cell 4</td>
16+
</tr>
17+
<tr>
18+
<td>Cell 5</td>
19+
<td>Cell 6</td>
20+
<td>Cell 7</td>
21+
<td>Cell 8</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
26+
<table>
27+
<thead>
28+
<tr>
29+
<th>Heading 1</th>
30+
<th>Heading 2</th>
31+
<th>Heading 3</th>
32+
</tr>
33+
</thead>
34+
<tbody>
35+
<tr>
36+
<td>Cell 1</td>
37+
<td>Cell 2</td>
38+
<td>Cell 3</td>
39+
<td>Cell 4</td>
40+
</tr>
41+
<tr>
42+
<td>Cell 5</td>
43+
<td>Cell 6</td>
44+
</tr>
45+
</tbody>
46+
</table>
47+
48+
<table>
49+
<thead>
50+
<tr>
51+
<th align="left">Heading 1</th>
52+
<th align="right">Heading 2</th>
53+
<th align="center">Heading 3</th>
54+
</tr>
55+
</thead>
56+
<tbody>
57+
<tr>
58+
<td align="left">Cell 1</td>
59+
<td align="right">Cell 2</td>
60+
<td align="center">Cell 3</td>
61+
</tr>
62+
<tr>
63+
<td align="left">Cell 4</td>
64+
<td align="right">Cell 5</td>
65+
<td align="center">Cell 6</td>
66+
</tr>
67+
</tbody>
68+
</table>
69+
70+
<table>
71+
<tr>
72+
<th align="left">Heading 1</th>
73+
<th align="right">Heading 2</th>
74+
</tr>
75+
<tr>
76+
<td align="left">Cell 1</td>
77+
<td align="right">Cell 2</td>
78+
</tr>
79+
<tr>
80+
<td align="left">Cell 3</td>
81+
<td align="right">Cell 4</td>
82+
</tr>
83+
</table>

tests/fixtures/table/index.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"fragment": true
3+
}

tests/fixtures/table/index.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
| Heading 1 | Heading 2 | Heading 3 | Heading 4 |
2+
| --------- | --------- | --------- | --------- |
3+
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
4+
| Cell 5 | Cell 6 | Cell 7 | Cell 8 |
5+
6+
| Heading 1 | Heading 2 | Heading 3 | |
7+
| --------- | --------- | --------- | ------ |
8+
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
9+
| Cell 5 | Cell 6 | | |
10+
11+
| Heading 1 | Heading 2 | Heading 3 |
12+
| :-------- | --------: | :-------: |
13+
| Cell 1 | Cell 2 | Cell 3 |
14+
| Cell 4 | Cell 5 | Cell 6 |
15+
16+
| Heading 1 | Heading 2 |
17+
| :-------- | --------: |
18+
| Cell 1 | Cell 2 |
19+
| Cell 3 | Cell 4 |

0 commit comments

Comments
 (0)