mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-03 19:44:07 +08:00
Handle tables with no headers by creating an empty Markdown header
This commit is contained in:
parent
61a981b8c6
commit
e77328fb23
@ -35,13 +35,21 @@ rules.table = {
|
|||||||
// Only convert tables with a heading row.
|
// Only convert tables with a heading row.
|
||||||
// Tables with no heading row are kept using `keep` (see below).
|
// Tables with no heading row are kept using `keep` (see below).
|
||||||
filter: function (node) {
|
filter: function (node) {
|
||||||
return node.nodeName === 'TABLE' && isHeadingRow(node.rows[0])
|
return node.nodeName === 'TABLE'
|
||||||
},
|
},
|
||||||
|
|
||||||
replacement: function (content) {
|
replacement: function (content, node) {
|
||||||
|
// If table has no heading, add an empty one so as to get a valid Markdown table
|
||||||
|
var firstRow = node.rows.length ? node.rows[0] : null
|
||||||
|
var columnCount = firstRow ? firstRow.childNodes.length : 0
|
||||||
|
var emptyHeader = ''
|
||||||
|
if (columnCount && !isHeadingRow(firstRow)) {
|
||||||
|
emptyHeader = '|' + ' |'.repeat(columnCount) + '\n' + '|' + ' --- |'.repeat(columnCount)
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure there are no blank lines
|
// Ensure there are no blank lines
|
||||||
content = content.replace('\n\n', '\n')
|
content = content.replace('\n\n', '\n')
|
||||||
return '\n\n' + content + '\n\n'
|
return '\n\n' + emptyHeader + content + '\n\n'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +99,7 @@ function cell (content, node) {
|
|||||||
|
|
||||||
export default function tables (turndownService) {
|
export default function tables (turndownService) {
|
||||||
turndownService.keep(function (node) {
|
turndownService.keep(function (node) {
|
||||||
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0])
|
return node.nodeName === 'TABLE'
|
||||||
})
|
})
|
||||||
for (var key in rules) turndownService.addRule(key, rules[key])
|
for (var key in rules) turndownService.addRule(key, rules[key])
|
||||||
}
|
}
|
||||||
|
@ -262,17 +262,20 @@
|
|||||||
| --- |</pre>
|
| --- |</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="case" data-name="non-definitive heading row (not converted)">
|
<div class="case" data-name="non-definitive heading row (converted but with empty header)">
|
||||||
<div class="input">
|
<div class="input">
|
||||||
<table>
|
<table>
|
||||||
<tr><td>Row 1 Cell 1</td><td>Row 1 Cell 2</td></tr>
|
<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>
|
<tr><td>Row 2 Cell 1</td><td>Row 2 Cell 2</td></tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<pre class="expected"><table><tbody><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></tbody></table></pre>
|
<pre class="expected">| | |
|
||||||
|
| --- | --- |
|
||||||
|
| Row 1 Cell 1 | Row 1 Cell 2 |
|
||||||
|
| Row 2 Cell 1 | Row 2 Cell 2 |</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="case" data-name="non-definitive heading row with th (not converted)">
|
<div class="case" data-name="non-definitive heading row with th (converted but with empty header)">
|
||||||
<div class="input">
|
<div class="input">
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
@ -285,7 +288,10 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<pre class="expected"><table><tbody><tr><th>Heading</th><td>Not a heading</td></tr><tr><td>Heading</td><td>Not a heading</td></tr></tbody></table></pre>
|
<pre class="expected">| | |
|
||||||
|
| --- | --- |
|
||||||
|
| Heading | Not a heading |
|
||||||
|
| Heading | Not a heading |</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="case" data-name="highlighted code block with html">
|
<div class="case" data-name="highlighted code block with html">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user