import React from 'react' /** * 高亮文本中的关键词 * @param text 原始文本 * @param keyword 关键词 * @returns ReactNode */ export function highlightText(text: string, keyword: string): React.ReactNode { if (!keyword || !text || !keyword.trim()) return text // 转义正则特殊字符 const escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') try { const regex = new RegExp(`(${escapeRegex(keyword.trim())})`, 'gi') const parts = text.split(regex) return ( <> {parts.map((part, i) => regex.test(part) ? ( {part} ) : ( part ) )} ) } catch (e) { console.error('Highlight error:', e) return text } }