Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch-all formatter callback for arbitrary tags #158

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ You can configure the behaviour of html-to-text with the following options:

By using the `format` option, you can specify formatting for these elements: `text`, `image`, `lineBreak`, `paragraph`, `anchor`, `heading`, `table`, `orderedList`, `unorderedList`, `listItem`, `horizontalLine`.

A catch-all key `otherTag` may be used to specify a formatter for tags that aren't covered by one of the other keys.

Each key must be a function which eventually receive `elem` (the current elem), `fn` (the next formatting function) and `options` (the options passed to html-to-text).

```js
Expand Down
5 changes: 5 additions & 0 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ function formatBlockquote(elem, fn, options) {
return '> ' + fn(elem.children, options) + '\n';
}

function formatOtherTag(elem, fn, options) {
return fn(elem.children, options);
}

exports.text = formatText;
exports.image = formatImage;
exports.lineBreak = formatLineBreak;
Expand All @@ -266,3 +270,4 @@ exports.unorderedList = formatUnorderedList;
exports.listItem = formatListItem;
exports.horizontalLine = formatHorizontalLine;
exports.blockquote = formatBlockquote;
exports.otherTag = formatOtherTag;
2 changes: 1 addition & 1 deletion lib/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function walk(dom, options, result) {
result += format.blockquote(elem, walk, options);
break;
default:
result = walk(elem.children || [], options, result);
result += format.otherTag(elem, walk, options);
}
break;
case 'text':
Expand Down
11 changes: 8 additions & 3 deletions test/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,20 @@ describe('html-to-text', function() {

describe('custom formatting', function () {
it('should allow to pass custom formatting functions', function () {
var result = htmlToText.fromString('<h1>TeSt</h1>', {
var result = htmlToText.fromString('<h1>TeSt</h1><div>tag</div>', {
format: {
heading: function (elem, fn, options) {
var h = fn(elem.children, options);
return '====\n' + h.toLowerCase() + '\n====';
return '====\n' + h.toLowerCase() + '\n====\n';
},

otherTag: function (elem, fn, options) {
var div = fn(elem.children, options);
return '----\n' + div.toUpperCase() + '\n----';
}
}
});
expect(result).to.equal('====\ntest\n====');
expect(result).to.equal('====\ntest\n====\n----\nTAG\n----');
});
});

Expand Down