kun-galgame-vue/src/utils/getPlainText.ts

13 lines
337 B
TypeScript
Raw Normal View History

2023-08-24 11:51:35 +00:00
export function getPlainText(html: string): string {
2023-10-12 15:29:10 +00:00
// 去除 HTML 标记
2023-09-20 15:07:27 +00:00
const plainText = html.replace(/<[^>]*>/g, '')
2023-10-12 15:29:10 +00:00
// 解码 HTML 实体
const textWithEntitiesDecoded = new DOMParser().parseFromString(
plainText,
'text/html'
).body.textContent
2023-08-24 11:51:35 +00:00
2023-10-12 15:29:10 +00:00
return textWithEntitiesDecoded ? textWithEntitiesDecoded : ''
2023-08-24 11:51:35 +00:00
}