phanpy/src/utils/getHTMLText.jsx

31 lines
708 B
React
Raw Normal View History

2023-10-19 22:57:56 +08:00
import mem from './mem';
2023-03-29 01:12:59 +08:00
const div = document.createElement('div');
2024-05-29 15:26:58 +08:00
function getHTMLText(html, opts) {
2023-03-29 01:12:59 +08:00
if (!html) return '';
2024-05-29 15:26:58 +08:00
const { preProcess } = opts || {};
2023-03-29 01:12:59 +08:00
div.innerHTML = html
.replace(/<\/p>/g, '</p>\n\n')
.replace(/<\/li>/g, '</li>\n');
div.querySelectorAll('br').forEach((br) => {
br.replaceWith('\n');
});
2024-01-10 01:48:20 +08:00
2024-05-29 15:26:58 +08:00
preProcess?.(div);
2024-01-10 01:48:20 +08:00
// MASTODON-SPECIFIC classes
// Remove .invisible
div.querySelectorAll('.invisible').forEach((el) => {
el.remove();
});
// Add … at end of .ellipsis
div.querySelectorAll('.ellipsis').forEach((el) => {
el.append('...');
});
2023-03-29 01:12:59 +08:00
return div.innerText.replace(/[\r\n]{3,}/g, '\n\n').trim();
}
2023-10-19 22:57:56 +08:00
export default mem(getHTMLText);