2025-08-12 13:36:42 +08:00
|
|
|
|
import 'dart:ui';
|
|
|
|
|
|
|
|
|
|
|
|
class CommonUtil {
|
|
|
|
|
|
static const Color commonColor = Color(0xFF6F72F1);
|
|
|
|
|
|
|
|
|
|
|
|
static bool containChinese(String text) {
|
|
|
|
|
|
return RegExp(r'[\u4E00-\u9FFF]').hasMatch(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static String cleanText(String text, bool forTts) {
|
|
|
|
|
|
String cleanedText = text
|
|
|
|
|
|
// 修正:使用 replaceAllMapped 确保安全提取内容
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'\*\*(.*?)\*\*'), (m) => m.group(1) ?? '') // 粗体
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'\*(.*?)\*'), (m) => m.group(1) ?? '') // 斜体
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'`([^`]+)`'), (m) => m.group(1) ?? '') // 行内代码
|
|
|
|
|
|
|
|
|
|
|
|
// 修正:处理不完整的代码块
|
|
|
|
|
|
.replaceAll(RegExp(r'```[^`]*```', multiLine: true), '') // 完整代码块
|
|
|
|
|
|
.replaceAll(RegExp(r'```[^`]*$', multiLine: true), '') // 不完整代码块开始
|
|
|
|
|
|
.replaceAll(RegExp(r'^[^`]*```', multiLine: true), '') // 不完整代码块结束
|
|
|
|
|
|
|
|
|
|
|
|
// 新增:处理 markdown 表格
|
|
|
|
|
|
.replaceAll(RegExp(r'^\s*\|.*\|\s*$', multiLine: true), '') // 表格行:| 内容 | 内容 |
|
|
|
|
|
|
.replaceAll(RegExp(r'^\s*[\|\-\:\+\=\s]+\s*$', multiLine: true), '') // 表格分隔符行:|---|---|
|
|
|
|
|
|
.replaceAll(RegExp(r'\|'), ' ') // 清理残留的竖线
|
|
|
|
|
|
|
|
|
|
|
|
// 修正:使用 replaceAllMapped 避免 $1 问题
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'^#{1,6}\s+(.*)$', multiLine: true),
|
|
|
|
|
|
(m) => m.group(1) ?? '')
|
2025-08-18 12:02:32 +08:00
|
|
|
|
.replaceAll(RegExp(r'\[([^\]]+)\]\([^\)]+\)'), '') // 过滤掉完整超链接
|
2025-08-12 13:36:42 +08:00
|
|
|
|
// 修正:处理不完整的链接
|
|
|
|
|
|
.replaceAll(RegExp(r'\[([^\]]*)\](?!\()'), r'$1') // 只有方括号的链接
|
|
|
|
|
|
.replaceAll(RegExp(r'\]\([^\)]*\)'), '') // 只有圆括号部分
|
|
|
|
|
|
|
|
|
|
|
|
.replaceAll(RegExp(r'!\[([^\]]*)\]\([^\)]+\)'), '') // 图片链接
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'^>\s*(.*)$', multiLine: true), (m) => m.group(1) ?? '')
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'^\s*[–—\-*+-]+\s*(.*)$', multiLine: true), (m) => m.group(1) ?? '')
|
|
|
|
|
|
.replaceAllMapped(RegExp(r'^\s*\d+\.\s+(.*)$', multiLine: true), (m) => m.group(1) ?? '')
|
|
|
|
|
|
.replaceAll(RegExp(r'^\s*[-*]{3,}\s*$', multiLine: true), '')
|
|
|
|
|
|
|
|
|
|
|
|
// 修正:清理残留的 markdown 符号
|
|
|
|
|
|
.replaceAll(RegExp(r'\*+'), '') // 清理残留星号
|
|
|
|
|
|
.replaceAll(RegExp(r'`+'), '') // 清理残留反引号
|
|
|
|
|
|
.replaceAll(RegExp(r'#+'), '') // 清理残留井号
|
|
|
|
|
|
|
|
|
|
|
|
.replaceAll(RegExp(r'\n\s*\n'), '\n')
|
|
|
|
|
|
.replaceAll(RegExp(r'\n'), ' ')
|
|
|
|
|
|
.replaceAll(RegExp(r'!\$\d'), '') // 清理占位符
|
|
|
|
|
|
.replaceAll(RegExp(r'\$\d+'), '') // 清理所有 $数字 占位符
|
|
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
|
|
|
|
if (forTts) {
|
2025-08-18 13:34:57 +08:00
|
|
|
|
if (cleanedText.contains("ID.UNYX")) {
|
|
|
|
|
|
cleanedText = cleanedText.replaceAllMapped(
|
|
|
|
|
|
RegExp(r'ID\.UNYX', caseSensitive: false), (m) => 'ID Unix');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cleanedText = cleanedText.replaceAllMapped(
|
|
|
|
|
|
RegExp(r'UNYX', caseSensitive: false), (m) => 'Unix');
|
|
|
|
|
|
}
|
2025-08-18 16:30:53 +08:00
|
|
|
|
cleanedText = cleanedText.replaceAllMapped(
|
|
|
|
|
|
RegExp(r'400-023-4567', caseSensitive: false), (m) => '4 0 0 - 0 2 3 - 4 5 6 7');
|
2025-08-12 13:36:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cleanedText;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|