2022-04-24 12:28:07 -07:00
|
|
|
/** Convert HTML to a plaintext representation, preserving whitespace. */
|
2020-03-27 13:59:38 -07:00
|
|
|
// NB: This function can still return unsafe HTML
|
2024-05-12 16:18:04 -07:00
|
|
|
const unescapeHTML = (html: string = ''): string => {
|
2020-03-27 13:59:38 -07:00
|
|
|
const wrapper = document.createElement('div');
|
2022-01-28 12:52:22 -08:00
|
|
|
wrapper.innerHTML = html.replace(/<br\s*\/?>/g, '\n').replace(/<\/p><[^>]*>/g, '\n\n').replace(/<[^>]*>/g, '');
|
2022-04-24 12:28:07 -07:00
|
|
|
return wrapper.textContent || '';
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
2022-01-24 18:39:22 -08:00
|
|
|
|
2022-09-29 09:23:49 -07:00
|
|
|
/** Convert HTML to plaintext. */
|
|
|
|
// https://stackoverflow.com/a/822486
|
2024-05-12 16:18:04 -07:00
|
|
|
const stripHTML = (html: string) => {
|
2022-09-29 09:23:49 -07:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = html;
|
|
|
|
return div.textContent || div.innerText || '';
|
|
|
|
};
|
2024-05-12 16:18:04 -07:00
|
|
|
|
|
|
|
export {
|
|
|
|
unescapeHTML,
|
|
|
|
stripHTML,
|
|
|
|
};
|