2022-12-26 01:06:33 +01:00
|
|
|
import * as htmlparser2 from 'htmlparser2'
|
2022-12-11 16:52:34 +01:00
|
|
|
|
|
|
|
const removeHTML = (text: string): string => {
|
2023-01-16 18:56:15 +01:00
|
|
|
if (!text) return ''
|
|
|
|
|
2022-12-11 16:52:34 +01:00
|
|
|
let raw: string = ''
|
|
|
|
|
|
|
|
const parser = new htmlparser2.Parser({
|
|
|
|
ontext: (text: string) => {
|
|
|
|
raw = raw + text
|
2022-12-18 17:25:18 +01:00
|
|
|
},
|
|
|
|
onclosetag: (tag: string) => {
|
|
|
|
if (['p', 'br'].includes(tag)) raw = raw + `\n`
|
2022-12-11 16:52:34 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
parser.write(text)
|
|
|
|
parser.end()
|
|
|
|
|
2022-12-31 02:06:19 +01:00
|
|
|
return raw.replace(new RegExp(/\s$/), '')
|
2022-12-11 16:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default removeHTML
|