The HTML string to format
The formatted plain text string
formatFromHtml("<ul><li>Item 1</li><li>Item 2</li></ul>") // Returns "- Item 1\n- Item 2"
export function formatFromHtml(html: string): string {
const tempDiv = document.createElement('DIV');
tempDiv.innerHTML = html;
if (tempDiv.children.length === 0) {
return tempDiv.textContent || '';
}
const result = Array.from(tempDiv.children).flatMap((child) => {
switch (child.nodeName) {
// Paragraphs just get their own lines.
case 'P':
return `${child.textContent}\n`;
case 'UL':
return Array.from(child.children).map((e) => `- ${e.textContent}`);
case 'OL':
return Array.from(child.children).map((e, idx) => `${idx + 1}) ${e.textContent}`);
case 'A':
return `${child.textContent} (${child.getAttribute('href')})`;
default:
return child.textContent;
}
});
return result.filter((x) => x !== undefined).join('\n');
}
Formats HTML into a readable plain text string, handling paragraphs, lists, and links.