Skip to content

Commit a1aa4f7

Browse files
committed
Fix to create empty cells for nest tables
While tables in tables have no nice mdast representation, their previous behavior was significantly broken, and this commit improves that. Closes GH-65.
1 parent 4d9cc95 commit a1aa4f7

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

lib/handlers/table.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*/
1313

1414
import {convertElement} from 'hast-util-is-element'
15-
import {visit} from 'unist-util-visit'
15+
import {toText} from 'hast-util-to-text'
16+
import {visit, SKIP} from 'unist-util-visit'
17+
import {wrapText} from '../util/wrap-text.js'
1618
import {all} from '../all.js'
1719

1820
const thead = convertElement('thead')
@@ -24,6 +26,12 @@ const cell = convertElement(['th', 'td'])
2426
* @param {Element} node
2527
*/
2628
export function table(h, node) {
29+
if (h.inTable) {
30+
return h(node, 'text', wrapText(h, toText(node)))
31+
}
32+
33+
h.inTable = true
34+
2735
const {headless, align} = inspect(node)
2836
const rows = toRows(all(h, node), headless)
2937
let columns = 1
@@ -91,6 +99,8 @@ export function table(h, node) {
9199
align.push(null)
92100
}
93101

102+
h.inTable = false
103+
94104
return h(node, 'table', {align}, rows)
95105
}
96106

@@ -108,6 +118,10 @@ function inspect(node) {
108118
const align = [null]
109119

110120
visit(node, 'element', (child) => {
121+
if (child.tagName === 'table' && node !== child) {
122+
return SKIP
123+
}
124+
111125
// If there is a `thead`, assume there is a header row.
112126
if (cell(child) && child.properties) {
113127
if (!align[cellIndex]) {

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function toMdast(tree, options = {}) {
8181
{
8282
nodeById: byId,
8383
baseFound: false,
84+
inTable: false,
8485
wrapText: true,
8586
/** @type {string|null} */
8687
frozenBaseUrl: null,

lib/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @property {boolean} baseFound
3838
* @property {string|null} frozenBaseUrl
3939
* @property {boolean} wrapText
40+
* @property {boolean} inTable
4041
* @property {number} qNesting
4142
* @property {Object.<string, Handle>} handlers
4243
* @property {boolean|undefined} document
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<table>
2+
<tr>
3+
<th>outer 1</th>
4+
<th>outer 2</th>
5+
</tr>
6+
<tr>
7+
<td>
8+
<table>
9+
<tr>
10+
<td>inner a</td>
11+
<td>inner b</td>
12+
<td>inner c</td>
13+
</tr>
14+
</table>
15+
</td>
16+
<td>outer 3</td>
17+
</tr>
18+
</table>
19+
20+
<table>
21+
<tr>
22+
<td>
23+
<table>
24+
<tr>
25+
<td>inner a</td>
26+
<td>inner b</td>
27+
</tr>
28+
<tr>
29+
<td>inner c</td>
30+
<td>inner d</td>
31+
</tr>
32+
<tr>
33+
<td>inner e</td>
34+
<td>inner f</td>
35+
</tr>
36+
</table>
37+
</td>
38+
<td>outer 1</td>
39+
</tr>
40+
</table>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"fragment": true
3+
}

test/fixtures/table-in-table/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| outer 1 | outer 2 |
2+
| ----------------------- | ------- |
3+
| inner a inner b inner c | outer 3 |
4+
5+
| | |
6+
| ----------------------------------------------- | ------- |
7+
| inner a inner b inner c inner d inner e inner f | outer 1 |

0 commit comments

Comments
 (0)