Files
jinshen-website/app/utils/transliterateText.ts
R2m1liA 710a0cdc5b
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m38s
feat: 支持拼音搜索
- 使用pinyin-pro进行汉语拼音转换
- 调整搜索权重
2025-11-10 15:39:47 +08:00

18 lines
412 B
TypeScript

import { pinyin } from 'pinyin-pro';
/**
* 将汉语文本转换为拼音形式
*/
export function transliterateText(input: string): string {
if (!input) return '';
const text = input.normalize('NFKC').trim();
// 检测是否包含中文字符
if (/[\u4e00-\u9fa5]/.test(text)) {
return pinyin(text, { toneType: 'none', type: 'array' }).join('');
}
// 否则返回原文本
return text;
}