tooot/src/utils/helpers/removeHTML.ts

24 lines
454 B
TypeScript
Raw Normal View History

2022-12-26 01:06:33 +01:00
import * as htmlparser2 from 'htmlparser2'
const removeHTML = (text: string): string => {
2023-01-16 18:56:15 +01:00
if (!text) return ''
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`
}
})
parser.write(text)
parser.end()
2022-12-31 02:06:19 +01:00
return raw.replace(new RegExp(/\s$/), '')
}
export default removeHTML