diff --git a/lib/formatter.js b/lib/formatter.js index ef2e82d..260afd6 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -98,7 +98,10 @@ function formatAnchor(elem, fn, options) { options.lineCharCount = storedCharCount; - return formatText({ data: result || href, trimLeadingSpace: elem.trimLeadingSpace }, options); + return Object.assign({}, exports, options.format).text({ + data: result || href, + trimLeadingSpace: elem.trimLeadingSpace + }, options); } function formatHorizontalLine(elem, fn, options) { @@ -128,7 +131,7 @@ function formatUnorderedList(elem, fn, options) { return child.type !== 'text' || !whiteSpaceRegex.test(child.data); }); nonWhiteSpaceChildren.forEach(function(elem) { - result += formatListItem(prefix, elem, fn, options); + result += Object.assign({}, exports, options.format).listItem(prefix, elem, fn, options); }); return result + '\n'; } @@ -163,7 +166,7 @@ function formatOrderedList(elem, fn, options) { // Calculate the needed spacing for nice indentation. var spacing = maxLength - index.toString().length; var prefix = ' ' + index + '. ' + ' '.repeat(spacing); - result += formatListItem(prefix, elem, fn, options); + result += Object.assign({}, exports, options.format).listItem(prefix, elem, fn, options); }); } return result + '\n'; @@ -220,22 +223,22 @@ function formatTable(elem, fn, options) { if (elem.type === 'tag') { switch (elem.name.toLowerCase()) { case 'th': - tokens = formatHeading(elem, fn, options).split('\n'); - rows.push(compact(tokens)); + tokens = Object.assign({}, exports, options.format).heading(elem, fn, options).split('\n'); break; - case 'td': tokens = fn(elem.children, options).split('\n'); - rows.push(compact(tokens)); - // Fill colspans with empty values - if (elem.attribs && elem.attribs.colspan) { - count = elem.attribs.colspan - 1 || 0; - times(count, function() { - rows.push(['']); - }); - } break; } + if (tokens) { + rows.push(compact(tokens)); + // Fill colspans with empty values + if (elem.attribs && elem.attribs.colspan) { + count = elem.attribs.colspan - 1 || 0; + times(count, function() { + rows.push(['']); + }); + } + } } }); rows = helper.arrayZip(rows); diff --git a/test/html-to-text.js b/test/html-to-text.js index af7f7aa..d165920 100644 --- a/test/html-to-text.js +++ b/test/html-to-text.js @@ -192,6 +192,31 @@ describe('html-to-text', function() { var result = htmlToText.fromString(html, { tables: true }); expect(result).to.equal(resultExpected); }); + it('does handle colspan on th elements correctly', function () { + var html = '\ +
header column 1 | \ +header columns 2 and 3 | \ +header column 4 | \ +|
---|---|---|---|
column 1 | \ +column 2 | \ +column 3 | \ +column 4 | \ +