Skip to content

Commit

Permalink
Do not strip single spaces inside elements
Browse files Browse the repository at this point in the history
  • Loading branch information
zarathustra323 committed May 20, 2019
1 parent 661df22 commit a7e914f
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions src/utils/strip-whitespace.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
const cheerio = require('cheerio');

const cleanElementText = ($, $el) => {
const contents = $el.contents()[0];

if (contents && contents.type === 'text') {
const { data = '' } = contents;
const cleaned = data.replace(/\s\s+/g, ' ').trim();
contents.data = cleaned;
}
if ($el.children().length) {
$el.children().each(function () {
cleanElementText($, $(this));
});
}
};

module.exports = (html) => {
const str = (html || '')
.replace(/[\r\n\f\v\t\b\\]/g, ' ')
.replace(/[\r\n\f\v\t\b\\]/g, ' ')
.replace(/ /g, ' ')
.trim()
.replace(/>\s+</g, '><')
.replace(/\s+%{\[/g, '%{[')
.replace(/\]}%\s+/g, ']}%');
const $ = cheerio.load(str, { decodeEntities: false });
cleanElementText($, $('body'));
return $('body').html();
.replace(/\]}%\s+/g, ']}%')
.replace(/\s\s+/g, ' ');
return str;
};

0 comments on commit a7e914f

Please sign in to comment.