17 lines
685 B
TypeScript
17 lines
685 B
TypeScript
const HTML_MIME = 'text/html'
|
|
const BLOCK_SELECTOR = 'p, h1, h2, h3, h4, h5, h6, li, blockquote, pre'
|
|
const BLANK_LINE = '\n\n'
|
|
|
|
export function bodyPlainText(html: string) {
|
|
const document = new DOMParser().parseFromString(html, HTML_MIME)
|
|
const blocks = Array.from(document.body.querySelectorAll(BLOCK_SELECTOR))
|
|
.filter((block) => !block.querySelector(BLOCK_SELECTOR))
|
|
.map((block) => (block.textContent ?? '').trim())
|
|
.filter((text) => text !== '')
|
|
return blocks.length > 0 ? blocks.join(BLANK_LINE) : (document.body.textContent ?? '').trim()
|
|
}
|
|
|
|
export function conflictDownloadText(title: string, html: string) {
|
|
return `${title}${BLANK_LINE}${bodyPlainText(html)}\n`
|
|
}
|