mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-23 02:41:50 +03:00
cff6789a35
no issue - default param was not protecting against null value being passed via helper params
33 lines
800 B
JavaScript
33 lines
800 B
JavaScript
import {helper} from '@ember/component/helper';
|
|
import {isArray} from '@ember/array';
|
|
|
|
export function cleanBasicHtml(html = '') {
|
|
if (isArray(html)) {
|
|
html = html[0] || '';
|
|
}
|
|
|
|
let cleanHtml = html
|
|
.replace(/<br>/g, ' ')
|
|
.replace(/(\s| ){2,}/g, ' ')
|
|
.trim()
|
|
.replace(/^ | $/g, '')
|
|
.trim();
|
|
|
|
// remove any elements that have a blank textContent
|
|
if (cleanHtml) {
|
|
let doc = new DOMParser().parseFromString(cleanHtml, 'text/html');
|
|
|
|
doc.body.querySelectorAll('*').forEach((element) => {
|
|
if (!element.textContent.trim()) {
|
|
element.remove();
|
|
}
|
|
});
|
|
|
|
cleanHtml = doc.body.innerHTML;
|
|
}
|
|
|
|
return cleanHtml;
|
|
}
|
|
|
|
export default helper(cleanBasicHtml);
|