2023-08-24 11:51:35 +00:00
|
|
|
export function getPlainText(html: string): string {
|
2023-09-20 15:07:27 +00:00
|
|
|
// 使用正则表达式匹配所有HTML标签并删除
|
|
|
|
const plainText = html.replace(/<[^>]*>/g, '')
|
|
|
|
|
|
|
|
// 使用实体编码映射表将HTML实体编码还原
|
|
|
|
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
|
|
|
}
|