Skip to content

Commit

Permalink
Merge pull request #9 from domchristie/tables_with_no_heading_row
Browse files Browse the repository at this point in the history
Keep tables with no definitive heading row
  • Loading branch information
domchristie committed May 11, 2018
2 parents 7eeb773 + 93d5aed commit 22f4234
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ rules.tableRow = {
}

rules.table = {
filter: 'table',
// Only convert tables with a heading row.
// Tables with no heading row are kept using `keep` (see below).
filter: function (node) {
return node.nodeName === 'TABLE' && isHeadingRow(node.rows[0])
},

replacement: function (content) {
// Ensure there are no blank lines
content = content.replace('\n\n', '\n')
Expand Down Expand Up @@ -85,5 +90,8 @@ function cell (content, node) {
}

export default function tables (turndownService) {
turndownService.keep(function (node) {
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0])
})
for (var key in rules) turndownService.addRule(key, rules[key])
}
51 changes: 33 additions & 18 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
<div class="case" data-name="empty rows">
<div class="input">
<table>
<thead>
<td>Heading 1</td>
<td>Heading 2</td>
</thead>
<tbody>
<tr>
<td>Row 1</td>
Expand All @@ -167,7 +171,9 @@
</tbody>
</table>
</div>
<pre class="expected">| Row 1 | Row 1 |
<pre class="expected">| Heading 1 | Heading 2 |
| --- | --- |
| Row 1 | Row 1 |
| Row 3 | Row 3 |</pre>
</div>

Expand Down Expand Up @@ -233,23 +239,6 @@
| Content |</pre>
</div>

<div class="case" data-name="th in first row">
<div class="input">
<table>
<tr>
<th>Heading</th>
<td>Not a heading</td>
</tr>
<tr>
<td>Heading</td>
<td>Not a heading</td>
</tr>
</table>
</div>
<pre class="expected">| Heading | Not a heading |
| Heading | Not a heading |</pre>
</div>

<div class="case" data-name="heading cells in both thead and tbody">
<div class="input">
<table>
Expand All @@ -273,6 +262,32 @@
| --- |</pre>
</div>

<div class="case" data-name="non-definitive heading row (not converted)">
<div class="input">
<table>
<tr><td>Row 1 Cell 1</td><td>Row 1 Cell 2</td></tr>
<tr><td>Row 2 Cell 1</td><td>Row 2 Cell 2</td></tr>
</table>
</div>
<pre class="expected">&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Row 1 Cell 1&lt;/td&gt;&lt;td&gt;Row 1 Cell 2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Row 2 Cell 1&lt;/td&gt;&lt;td&gt;Row 2 Cell 2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</pre>
</div>

<div class="case" data-name="non-definitive heading row with th (not converted)">
<div class="input">
<table>
<tr>
<th>Heading</th>
<td>Not a heading</td>
</tr>
<tr>
<td>Heading</td>
<td>Not a heading</td>
</tr>
</table>
</div>
<pre class="expected">&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Heading&lt;/th&gt;&lt;td&gt;Not a heading&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Heading&lt;/td&gt;&lt;td&gt;Not a heading&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</pre>
</div>

<div class="case" data-name="highlighted code block with html">
<div class="input">
<div class="highlight highlight-text-html-basic">
Expand Down

0 comments on commit 22f4234

Please sign in to comment.