fix: getPlainContent causes external content to be fetched (#3193)

This commit is contained in:
Ian Morland 2021-12-12 19:30:56 +00:00 committed by GitHub
parent 1a420828aa
commit 325b9afca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,11 +28,16 @@ export function slug(string: string): string {
export function getPlainContent(string: string): string {
const html = string.replace(/(<\/p>|<br>)/g, '$1 &nbsp;').replace(/<img\b[^>]*>/gi, ' ');
const dom = $('<div/>').html(html);
const element = new DOMParser().parseFromString(html, 'text/html').documentElement;
dom.find(getPlainContent.removeSelectors.join(',')).remove();
getPlainContent.removeSelectors.forEach((selector) => {
const el = element.querySelectorAll(selector);
el.forEach((e) => {
e.remove();
});
});
return dom.text().replace(/\s+/g, ' ').trim();
return element.innerText.replace(/\s+/g, ' ').trim();
}
/**