Skip to content

Commit 8545249

Browse files
committed
🎨 Removed extra new lines and improved header formatting in plain text.
see https://forum.ghost.org/t/unnecessary-and-excessive-newlines-in-plain-text-part-of-newsletters/60267/ see https://www.w3.org/WAI/WCAG22/Techniques/text/T3 - Previously relied on presence of \n in source HTML. - Now sets reasonable new lines based on HTML elements. - Formats headers using WCAG 2.2 Techniques suggestions - Tests added for all changes.
1 parent 7753c94 commit 8545249

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

‎packages/html-to-plaintext/lib/html-to-plaintext.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@ const loadConverters = () => {
6161
});
6262

6363
const emailSettings = mergeSettings({
64+
preserveNewlines: false,
6465
selectors: [
6566
// equiv hideLinkHrefIfSameAsText: true
6667
{selector: 'a', options: {hideLinkHrefIfSameAsText: true}},
6768
// Don't include html .preheader in email
68-
{selector: '.preheader', format: 'skip'}
69+
{selector: '.preheader', format: 'skip'},
70+
{selector: 'p', options: {leadingLineBreaks: 2, trailingLineBreaks: 1}},
71+
{selector: 'h1', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
72+
{selector: 'h2', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
73+
{selector: 'h3', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
74+
{selector: 'h4', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
75+
{selector: 'h5', options: {leadingLineBreaks: 3, trailingLineBreaks: 1}},
76+
{selector: 'h6', options: {uppercase: false, leadingLineBreaks: 3, trailingLineBreaks: 1}}
6977
]
7078
});
7179

‎packages/html-to-plaintext/test/html-to-plaintext.test.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ describe('Html to Plaintext', function () {
8888
});
8989
});
9090

91+
describe('New lines and format headers', function () {
92+
it('Strips excessive new lines and formats headers', function () {
93+
const html = '<p>Some ordinary text</p>\n\n\n\n<p>Should not be way far apart from earlier text.</p>';
94+
const expected = 'Some ordinary text\n\nShould not be way far apart from earlier text.';
95+
const {email} = getEmailandExcert(html);
96+
assert.equal(email, expected);
97+
});
98+
99+
it('Check header formatting', function () {
100+
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>';
101+
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';
102+
const {email} = getEmailandExcert(html);
103+
assert.equal(email, expected);
104+
});
105+
106+
it('Empty headers return nothing', function () {
107+
const html = '<h1></h1>';
108+
const expected = '';
109+
const {email} = getEmailandExcert(html);
110+
assert.equal(email, expected);
111+
});
112+
113+
it('Non-text header contents don’t appear', function () {
114+
const html = '<h1>Hello<!--Test-->world</h1>';
115+
const expected = 'Helloworld';
116+
const {email} = getEmailandExcert(html);
117+
assert.equal(email, expected);
118+
});
119+
});
120+
91121
describe('commentSnippet converter', function () {
92122
function testConverter({input, expected}) {
93123
return () => {

0 commit comments

Comments
 (0)