21 lines
782 B
TypeScript
21 lines
782 B
TypeScript
export function htmlToText(html: string): string {
|
|
if (!html) return "";
|
|
let text = html.replace(/<script[^>]*>.*?<\/script>/gis, "");
|
|
text = text.replace(/<style[^>]*>.*?<\/style>/gis, "");
|
|
text = text.replace(/<br\s*\/?>/gi, "\n");
|
|
text = text.replace(/<p[^>]*>/gi, "\n");
|
|
text = text.replace(/<\/p>/gi, "");
|
|
text = text.replace(/<div[^>]*>/gi, "\n");
|
|
text = text.replace(/<\/div>/gi, "");
|
|
text = text.replace(/<[^>]+>/g, "");
|
|
text = text.replace(/ /g, " ");
|
|
text = text.replace(/&/g, "&");
|
|
text = text.replace(/</g, "<");
|
|
text = text.replace(/>/g, ">");
|
|
text = text.replace(/"/g, "\"");
|
|
text = text.replace(/'/g, "'");
|
|
text = text.replace(/\n\s*\n/g, "\n\n");
|
|
text = text.replace(/[ \t]+/g, " ");
|
|
return text.trim();
|
|
}
|