import cheerio from 'cheerio' import { decode } from 'html-entities' // Given a piece of HTML return it without HTML. E.g. // `
Foo & bar
` becomes `Foo & bar` // and `A link andcode` becomes `A link and code`.
// Take advantage of the subtle fact that a lot of the times, the html value
// we get here is a single line that starts with `` and ends with `
` // and contains no longer HTML tags. export function fastTextOnly(html: string): string { if (!html) return '' if (html.startsWith('') && html.endsWith('
')) { const middle = html.slice(3, -4) if (!middle.includes('<')) return decode(middle.trim()) } const $ = cheerio.load(html, { xmlMode: true }) return $.root().text().trim() }