Skip to content

Commit 05c5496

Browse files
Allow recursive custom formatters (#2)
* Allow recursive custom formatters * Resolves html-to-text#147 * When format functions are called internally within formatter.js it is not possible to override the custom logic. This changes that functionality and allows users to choose whether they want the default formatter to be used internally or their own formatters supplied in the options object. * fixup - reduce line length
1 parent 734a5fc commit 05c5496

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/formatter.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ function formatAnchor(elem, fn, options) {
9898

9999
options.lineCharCount = storedCharCount;
100100

101-
return formatText({ data: result || href, trimLeadingSpace: elem.trimLeadingSpace }, options);
101+
return Object.assign({}, exports, options.format).text({
102+
data: result || href,
103+
trimLeadingSpace: elem.trimLeadingSpace
104+
}, options);
102105
}
103106

104107
function formatHorizontalLine(elem, fn, options) {
@@ -128,7 +131,7 @@ function formatUnorderedList(elem, fn, options) {
128131
return child.type !== 'text' || !whiteSpaceRegex.test(child.data);
129132
});
130133
nonWhiteSpaceChildren.forEach(function(elem) {
131-
result += formatListItem(prefix, elem, fn, options);
134+
result += Object.assign({}, exports, options.format).listItem(prefix, elem, fn, options);
132135
});
133136
return result + '\n';
134137
}
@@ -163,7 +166,7 @@ function formatOrderedList(elem, fn, options) {
163166
// Calculate the needed spacing for nice indentation.
164167
var spacing = maxLength - index.toString().length;
165168
var prefix = ' ' + index + '. ' + ' '.repeat(spacing);
166-
result += formatListItem(prefix, elem, fn, options);
169+
result += Object.assign({}, exports, options.format).listItem(prefix, elem, fn, options);
167170
});
168171
}
169172
return result + '\n';
@@ -220,9 +223,8 @@ function formatTable(elem, fn, options) {
220223
if (elem.type === 'tag') {
221224
switch (elem.name.toLowerCase()) {
222225
case 'th':
223-
tokens = formatHeading(elem, fn, options).split('\n');
226+
tokens = Object.assign({}, exports, options.format).heading(elem, fn, options).split('\n');
224227
break;
225-
226228
case 'td':
227229
tokens = fn(elem.children, options).split('\n');
228230
break;

test/html-to-text.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,23 @@ column 1 column 2 column 3 column 4';
402402
});
403403
expect(result).to.equal('====\ntest\n====');
404404
});
405+
406+
it('should use custom formatting functions when nested elements are being parsed', function () {
407+
var result = htmlToText.fromString('<ul><li>one</li><li>two</li></ul>', {
408+
format: {
409+
listItem: function (prefix, elem, fn, options) {
410+
options = Object.assign({}, options);
411+
if (options.wordwrap) {
412+
options.wordwrap -= prefix.length;
413+
}
414+
var text = fn(elem.children, options);
415+
text = text.replace(/\n/g, '\n' + ' '.repeat(prefix.length));
416+
return prefix + text.toUpperCase() + '\n';
417+
}
418+
}
419+
});
420+
expect(result).to.equal(' * ONE\n * TWO');
421+
});
405422
});
406423

407424
describe('Base element', function () {

0 commit comments

Comments
 (0)