Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -220,9 +223,8 @@ function formatTable(elem, fn, options) {
if (elem.type === 'tag') {
switch (elem.name.toLowerCase()) {
case 'th':
tokens = formatHeading(elem, fn, options).split('\n');
tokens = Object.assign({}, exports, options.format).heading(elem, fn, options).split('\n');
break;

case 'td':
tokens = fn(elem.children, options).split('\n');
break;
Expand Down
17 changes: 17 additions & 0 deletions test/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,23 @@ column 1 column 2 column 3 column 4';
});
expect(result).to.equal('====\ntest\n====');
});

it('should use custom formatting functions when nested elements are being parsed', function () {
var result = htmlToText.fromString('<ul><li>one</li><li>two</li></ul>', {
format: {
listItem: function (prefix, elem, fn, options) {
options = Object.assign({}, options);
if (options.wordwrap) {
options.wordwrap -= prefix.length;
}
var text = fn(elem.children, options);
text = text.replace(/\n/g, '\n' + ' '.repeat(prefix.length));
return prefix + text.toUpperCase() + '\n';
}
}
});
expect(result).to.equal(' * ONE\n * TWO');
});
});

describe('Base element', function () {
Expand Down