fix: Preserve whitespace in certain HTML elements (#333)

pull/325/head^2
Toufic Mouallem 5 years ago committed by Adam Pash
parent 2a3ade706d
commit a250f403f5

@ -610,6 +610,16 @@ const HTML = {
`,
after: 'What do you think?',
},
normalizeSpacesPreserve: {
before: `
<div>
<p>What do you think?</p>
<pre> What happens to spaces? </pre>
</div>
`,
after:
'<div> <p>What do you think?</p> <pre> What happens to spaces? </pre> </div>',
},
// cleanHeaders
cleanFirstHeds: {

@ -1,4 +1,4 @@
const NORMALIZE_RE = /\s{2,}/g;
const NORMALIZE_RE = /\s{2,}(?![^<>]*<\/(pre|code|textarea)>)/g;
export default function normalizeSpaces(text) {
return text.replace(NORMALIZE_RE, ' ').trim();

@ -16,4 +16,11 @@ describe('normalizeSpaces(text)', () => {
);
assert.equal(result, HTML.normalizeSpaces.after);
});
it('preserves spaces in preformatted text blocks', () => {
const $ = cheerio.load(HTML.normalizeSpacesPreserve.before);
const result = normalizeSpaces($.html());
assert.equal(result, HTML.normalizeSpacesPreserve.after);
});
});

Loading…
Cancel
Save