diff --git a/src/utils/text/fixtures/html.js b/src/utils/text/fixtures/html.js index 97dc6148a..0045efa79 100644 --- a/src/utils/text/fixtures/html.js +++ b/src/utils/text/fixtures/html.js @@ -610,6 +610,16 @@ const HTML = { `, after: 'What do you think?', }, + normalizeSpacesPreserve: { + before: ` +
+

What do you think?

+
  What     happens to        spaces?    
+
+ `, + after: + '

What do you think?

  What     happens to        spaces?    
', + }, // cleanHeaders cleanFirstHeds: { diff --git a/src/utils/text/normalize-spaces.js b/src/utils/text/normalize-spaces.js index f0c42b665..ba82a9841 100644 --- a/src/utils/text/normalize-spaces.js +++ b/src/utils/text/normalize-spaces.js @@ -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(); diff --git a/src/utils/text/normalize-spaces.test.js b/src/utils/text/normalize-spaces.test.js index 95cd02a6a..c41ce6ca8 100644 --- a/src/utils/text/normalize-spaces.test.js +++ b/src/utils/text/normalize-spaces.test.js @@ -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); + }); });