Skip to content

Handle lists with line breaks between items #165

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function formatUnorderedList(elem, fn, options) {
var result = '';
var prefix = options.unorderedListItemPrefix;
var nonWhiteSpaceChildren = (elem.children || []).filter(function(child) {
return child.type !== 'text' || !whiteSpaceRegex.test(child.data);
return child.name === 'li' && (child.type !== 'text' || !whiteSpaceRegex.test(child.data));
});
nonWhiteSpaceChildren.forEach(function(elem) {
result += formatListItem(prefix, elem, fn, options);
Expand All @@ -136,7 +136,7 @@ function formatUnorderedList(elem, fn, options) {
function formatOrderedList(elem, fn, options) {
var result = '';
var nonWhiteSpaceChildren = (elem.children || []).filter(function(child) {
return child.type !== 'text' || !whiteSpaceRegex.test(child.data);
return child.name === 'li' && (child.type !== 'text' || !whiteSpaceRegex.test(child.data));
});
// Return different functions for different OL types
var typeFunction = (function() {
Expand Down
10 changes: 10 additions & 0 deletions test/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ describe('html-to-text', function() {
expect(htmlToText.fromString(testString)).to.equal(' * foo\n * bar');
});

it('should handle an unordered list with line break', function() {
var testString = '<ul><br><li>foo</li><li>bar</li></ul>';
expect(htmlToText.fromString(testString)).to.equal(' * foo\n * bar');
});

it('should handle an unordered list prefix option', function() {
var testString = '<ul><li>foo</li><li>bar</li></ul>';
var options = {unorderedListItemPrefix: ' test '};
Expand All @@ -262,6 +267,11 @@ describe('html-to-text', function() {
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});

it('should handle an ordered list with line break', function() {
var testString = '<ol><br><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});

it('should support the ordered list type="1" attribute', function() {
var testString = '<ol type="1"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
Expand Down