Skip to content

Commit

Permalink
Fix to create empty cells for nest tables
Browse files Browse the repository at this point in the history
While tables in tables have no nice mdast representation, their
previous behavior was significantly broken, and this commit improves
that.

Closes GH-65.
  • Loading branch information
wooorm committed Sep 18, 2021
1 parent 4d9cc95 commit a1aa4f7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/handlers/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/

import {convertElement} from 'hast-util-is-element'
import {visit} from 'unist-util-visit'
import {toText} from 'hast-util-to-text'
import {visit, SKIP} from 'unist-util-visit'
import {wrapText} from '../util/wrap-text.js'
import {all} from '../all.js'

const thead = convertElement('thead')
Expand All @@ -24,6 +26,12 @@ const cell = convertElement(['th', 'td'])
* @param {Element} node
*/
export function table(h, node) {
if (h.inTable) {
return h(node, 'text', wrapText(h, toText(node)))
}

h.inTable = true

const {headless, align} = inspect(node)
const rows = toRows(all(h, node), headless)
let columns = 1
Expand Down Expand Up @@ -91,6 +99,8 @@ export function table(h, node) {
align.push(null)
}

h.inTable = false

return h(node, 'table', {align}, rows)
}

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

visit(node, 'element', (child) => {
if (child.tagName === 'table' && node !== child) {
return SKIP
}

// If there is a `thead`, assume there is a header row.
if (cell(child) && child.properties) {
if (!align[cellIndex]) {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function toMdast(tree, options = {}) {
{
nodeById: byId,
baseFound: false,
inTable: false,
wrapText: true,
/** @type {string|null} */
frozenBaseUrl: null,
Expand Down
1 change: 1 addition & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @property {boolean} baseFound
* @property {string|null} frozenBaseUrl
* @property {boolean} wrapText
* @property {boolean} inTable
* @property {number} qNesting
* @property {Object.<string, Handle>} handlers
* @property {boolean|undefined} document
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/table-in-table/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<table>
<tr>
<th>outer 1</th>
<th>outer 2</th>
</tr>
<tr>
<td>
<table>
<tr>
<td>inner a</td>
<td>inner b</td>
<td>inner c</td>
</tr>
</table>
</td>
<td>outer 3</td>
</tr>
</table>

<table>
<tr>
<td>
<table>
<tr>
<td>inner a</td>
<td>inner b</td>
</tr>
<tr>
<td>inner c</td>
<td>inner d</td>
</tr>
<tr>
<td>inner e</td>
<td>inner f</td>
</tr>
</table>
</td>
<td>outer 1</td>
</tr>
</table>
3 changes: 3 additions & 0 deletions test/fixtures/table-in-table/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"fragment": true
}
7 changes: 7 additions & 0 deletions test/fixtures/table-in-table/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| outer 1 | outer 2 |
| ----------------------- | ------- |
| inner a inner b inner c | outer 3 |

| | |
| ----------------------------------------------- | ------- |
| inner a inner b inner c inner d inner e inner f | outer 1 |

0 comments on commit a1aa4f7

Please sign in to comment.