Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getPlainContent causes external content to be fetched #3193

Merged
merged 4 commits into from
Dec 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions js/src/common/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down