Skip to content
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
10 changes: 9 additions & 1 deletion packages/html-to-plaintext/lib/html-to-plaintext.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ const loadConverters = () => {
});

const emailSettings = mergeSettings({
preserveNewlines: false,
selectors: [
// equiv hideLinkHrefIfSameAsText: true
{selector: 'a', options: {hideLinkHrefIfSameAsText: true}},
// Don't include html .preheader in email
{selector: '.preheader', format: 'skip'}
{selector: '.preheader', format: 'skip'},
{selector: 'p', options: {leadingLineBreaks: 2, trailingLineBreaks: 1}},
{selector: 'h1', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
{selector: 'h2', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
{selector: 'h3', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
{selector: 'h4', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
{selector: 'h5', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
{selector: 'h6', options: {uppercase: false, leadingLineBreaks: 3, trailingLineBreaks: 1}}
]
});

Expand Down
30 changes: 30 additions & 0 deletions packages/html-to-plaintext/test/html-to-plaintext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ describe('Html to Plaintext', function () {
});
});

describe('New lines and format headers', function () {
it('Strips excessive new lines and formats headers', function () {
const html = '<p>Some ordinary text</p>\n\n\n\n<p>Should not be way far apart from earlier text.</p>';
const expected = 'Some ordinary text\n\nShould not be way far apart from earlier text.';
const {email} = getEmailandExcert(html);
assert.equal(email, expected);
});

it('Check header formatting', function () {
const html = '<h1>Header One</h1>\n<p>What should I even write about?</p><p>And more</p><h2>With Header Two</h2><p>What about code?<h3>And Header Three</h3><p>Good bye</p>';
const expected = 'Header One\n\nWhat should I even write about?\n\nAnd more\n\n\nWith Header Two\n\nWhat about code?\n\n\nAnd Header Three\n\nGood bye';
const {email} = getEmailandExcert(html);
assert.equal(email, expected);
});

it('Empty headers return nothing', function () {
const html = '<h1></h1>';
const expected = '';
const {email} = getEmailandExcert(html);
assert.equal(email, expected);
});

it('Non-text header contents don’t appear', function () {
const html = '<h1>Hello<!--Test-->world</h1>';
const expected = 'Helloworld';
const {email} = getEmailandExcert(html);
assert.equal(email, expected);
});
});

describe('commentSnippet converter', function () {
function testConverter({input, expected}) {
return () => {
Expand Down