From 11225771b657ea0c5ace66c6fff9e502f9c44f8c Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 9 Jun 2018 19:40:57 +0100 Subject: [PATCH] Don't render table outline if it contains only one cell, or no cell at all --- src/tables.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/tables.js b/src/tables.js index f60d5dd77..980ed1e3d 100644 --- a/src/tables.js +++ b/src/tables.js @@ -5,7 +5,7 @@ var rules = {} rules.tableCell = { filter: ['th', 'td'], replacement: function (content, node) { - if (nodeContainsTable(nodeParentTable(node))) return content; + if (tableShouldBeSkipped(nodeParentTable(node))) return content; return cell(content, node) } } @@ -14,7 +14,7 @@ rules.tableRow = { filter: 'tr', replacement: function (content, node) { const parentTable = nodeParentTable(node); - if (nodeContainsTable(parentTable)) return content; + if (tableShouldBeSkipped(parentTable)) return content; var borderCells = '' var alignMap = { left: ':--', right: '--:', center: ':-:' } @@ -47,7 +47,7 @@ rules.table = { }, replacement: function (content, node) { - if (nodeContainsTable(node)) return content; + if (tableShouldBeSkipped(node)) return content; // 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 @@ -121,6 +121,16 @@ function nodeContainsTable(node) { return false; } +// Various conditions under which a table should be skipped - i.e. each cell +// will be rendered one after the other as if they were paragraphs. +function tableShouldBeSkipped(tableNode) { + if (!tableNode) return true; + if (!tableNode.rows) return true; + if (tableNode.rows.length <= 1 && tableNode.rows[0].childNodes.length <= 1) return true; // Table with only one cell + if (nodeContainsTable(tableNode)) return true; + return false; +} + function nodeParentTable(node) { let parent = node.parentNode; while (parent.nodeName !== 'TABLE') {