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

24 lines
598 B
TypeScript
Raw Normal View History

2023-08-24 11:51:35 +00:00
export function getPlainText(html: string): string {
2023-09-30 13:49:37 +00:00
// 使用正则表达式匹配所有 HTML 标签并删除
2023-09-20 15:07:27 +00:00
const plainText = html.replace(/<[^>]*>/g, '')
2023-09-30 13:49:37 +00:00
// 使用实体编码映射表将 HTML 实体编码还原
2023-09-20 15:07:27 +00:00
const entityMap: Record<string, string> = {
2023-08-24 11:51:35 +00:00
lt: '<',
gt: '>',
nbsp: ' ',
amp: '&',
quot: '"',
ldquo: '“',
mdash: '—',
rdquo: '”',
}
2023-09-20 15:07:27 +00:00
// 使用正则表达式匹配实体编码并还原
const decodedText = plainText.replace(/&(\w+);/g, function (match, entity) {
return entityMap[entity] || match
})
2023-08-24 11:51:35 +00:00
2023-09-20 15:07:27 +00:00
return decodedText
2023-08-24 11:51:35 +00:00
}