Improve table heading row conversion

This commit is contained in:
Dom Christie
2017-11-24 11:18:27 +00:00
parent 42e4a09b57
commit a4ef6870b7
2 changed files with 137 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
var indexOf = Array.prototype.indexOf
var every = Array.prototype.every
var rules = {}
rules.tableCell = {
@@ -13,10 +15,10 @@ rules.tableRow = {
var borderCells = ''
var alignMap = { left: ':--', right: '--:', center: ':-:' }
if (node.parentNode.nodeName === 'THEAD') {
if (isHeadingRow(node)) {
for (var i = 0; i < node.childNodes.length; i++) {
var align = node.childNodes[i].attributes.align
var border = '---'
var align = node.childNodes[i].attributes.align
if (align) border = alignMap[align.value] || border
@@ -43,8 +45,38 @@ rules.tableSection = {
}
}
// A tr is a heading row if:
// - the parent is a THEAD
// - or if its the first child of the TABLE or the first TBODY (possibly
// following a blank THEAD)
// - and every cell is a TH
function isHeadingRow (tr) {
var parentNode = tr.parentNode
return (
parentNode.nodeName === 'THEAD' ||
(
parentNode.firstChild === tr &&
(parentNode.nodeName === 'TABLE' || isFirstTbody(parentNode)) &&
every.call(tr.childNodes, function (n) { return n.nodeName === 'TH' })
)
)
}
function isFirstTbody (element) {
var previousSibling = element.previousSibling
return (
element.nodeName === 'TBODY' && (
!previousSibling ||
(
previousSibling.nodeName === 'THEAD' &&
/^\s*$/i.test(previousSibling.textContent)
)
)
)
}
function cell (content, node) {
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node)
var index = indexOf.call(node.parentNode.childNodes, node)
var prefix = ' '
if (index === 0) prefix = '| '
return prefix + content + ' |'