Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally force MD for tables without THEAD #15

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,26 @@ rules.tableRow = {
}

rules.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])
// Only convert tables with a heading row, unless they are forced.
// Otherwise tables with no heading row are kept using `keep` (see below).
filter: function (content, node, options) {
return node.nodeName === 'TABLE' && (isHeadingRow(node.rows[0]) || options.forceThead)
},

replacement: function (content) {
replacement: function (content, node, options) {
var emptyHeader = ''

if (options.forceThead) {
var firstRow = node.rows.length ? node.rows[0] : null
var columnCount = firstRow ? firstRow.childNodes.length : 0
if (columnCount && !isHeadingRow(firstRow)) {
emptyHeader = '|' + ' |'.repeat(columnCount) + '\n' + '|' + ' --- |'.repeat(columnCount)
}
}

// Ensure there are no blank lines
content = content.replace('\n\n', '\n')
return '\n\n' + content + '\n\n'
return '\n\n' + emptyHeader + content + '\n\n'
}
}

Expand Down Expand Up @@ -90,8 +100,8 @@ function cell (content, node) {
}

export default function tables (turndownService) {
turndownService.keep(function (node) {
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0])
turndownService.keep(function (content, node, options) {
return node.nodeName === 'TABLE' && (!isHeadingRow(node.rows[0]) || !options.forceThead)
})
for (var key in rules) turndownService.addRule(key, rules[key])
}