清理文本(emoji/不在白名单的符号;合并连续标点)
(text: string)
| 33 | } catch { } |
| 34 | } |
| 35 | |
| 36 | /** 清理文本(emoji/不在白名单的符号;合并连续标点) */ |
| 37 | function cleanTextForTTS(text: string): string { |
| 38 | if (!text) return ""; |
| 39 | let cleanedText = text; |
| 40 | // 移除各类 Emoji 和特殊符号 |
| 41 | const broadSymbolRegex = new RegExp( |
| 42 | "[" + |
| 43 | "\u{1F600}-\u{1F64F}" + // Emoticons |
| 44 | "\u{1F300}-\u{1F5FF}" + // Misc Symbols and Pictographs |
| 45 | "\u{1F680}-\u{1F6FF}" + // Transport and Map |
| 46 | "\u{2600}-\u{26FF}" + // Misc symbols |
| 47 | "\u{2700}-\u{27BF}" + // Dingbats |
| 48 | "\u{FE0F}" + // Variation Selectors |
| 49 | "\u{200D}" + // Zero-Width Joiner |
| 50 | "]", |
| 51 | "gu" |
| 52 | ); |
| 53 | cleanedText = cleanedText.replace(broadSymbolRegex, ""); |
| 54 | |
| 55 | // 仅保留中文、英文、数字和常见标点 |
| 56 | // const whitelistRegex = /[^\u4e00-\u9fa5a-zA-Z0-9\s,。?!、,?!.]/g; |
| 57 | // cleanedText = cleanedText.replace(whitelistRegex, ""); |
| 58 | |
| 59 | // 合并连续标点 |
| 60 | cleanedText = cleanedText.replace(/([,。?!、,?!.])\1+/g, "$1"); |
| 61 | // 移除 markdown 链接格式 [text](url) -> text |
| 62 | cleanedText = cleanedText.replace(/\[([^\]]+)\]\([^\)]+\)/g, "$1"); |
| 63 | |
| 64 | return cleanedText.trim(); |
| 65 | } |
| 66 |