diff --git a/hook/useAudioSpeak.js b/hook/useAudioSpeak.js index 07b0d84..f7c99e8 100644 --- a/hook/useAudioSpeak.js +++ b/hook/useAudioSpeak.js @@ -468,6 +468,7 @@ export const useAudioSpeak = (config = {}) => { let match; let lastJobEndIndex = 0; let firstJobStartIndex = -1; + while ((match = jobRegex.exec(markdown)) !== null) { const jobStr = match[1]; try { @@ -481,17 +482,28 @@ export const useAudioSpeak = (config = {}) => { console.warn('JSON 解析失败', e); } } + const guideText = firstJobStartIndex > 0 ? markdown.slice(0, firstJobStartIndex).trim() : ''; const endingText = lastJobEndIndex < markdown.length ? markdown.slice(lastJobEndIndex).trim() : ''; + const jobTexts = jobs.map((job, index) => { - return `第 ${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${job.salary},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}。`; + // 处理薪资格式:将 "数字-数字" 中的 '-' 替换为 '到' + let salaryText = job.salary; + if (salaryText) { + // 匹配 "数字-数字" 的格式(如:15000-18000元/月) + salaryText = salaryText.replace(/(\d+)-(\d+)/g, '$1到$2'); + } + + return `第 ${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${salaryText},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}。`; }); + const finalTextParts = []; if (guideText) finalTextParts.push(guideText); finalTextParts.push(...jobTexts); if (endingText) finalTextParts.push(endingText); + return finalTextParts.join('\n'); } diff --git a/hook/useTTSPlayer-all-in-one.js b/hook/useTTSPlayer-all-in-one.js index 5fe2a69..8de3278 100644 --- a/hook/useTTSPlayer-all-in-one.js +++ b/hook/useTTSPlayer-all-in-one.js @@ -222,41 +222,52 @@ export function useTTSPlayer() { * 文本提取工具函数 (保持不变) */ function extractSpeechText(markdown) { - if (!markdown || typeof markdown !== 'string') return ''; // 增加类型安全检查 - if (markdown.indexOf('job-json') === -1) { - return markdown; + if (!markdown || markdown.indexOf('job-json') === -1) { + return markdown; } const jobRegex = /``` job-json\s*({[\s\S]*?})\s*```/g; const jobs = []; let match; let lastJobEndIndex = 0; let firstJobStartIndex = -1; + while ((match = jobRegex.exec(markdown)) !== null) { - const jobStr = match[1]; - try { - const job = JSON.parse(jobStr); - jobs.push(job); - if (firstJobStartIndex === -1) { - firstJobStartIndex = match.index; - } - lastJobEndIndex = jobRegex.lastIndex; - } catch (e) { - console.warn('JSON 解析失败', e); + const jobStr = match[1]; + try { + const job = JSON.parse(jobStr); + jobs.push(job); + if (firstJobStartIndex === -1) { + firstJobStartIndex = match.index; } + lastJobEndIndex = jobRegex.lastIndex; + } catch (e) { + console.warn('JSON 解析失败', e); + } } + const guideText = firstJobStartIndex > 0 ? - markdown.slice(0, firstJobStartIndex).trim() : ''; + markdown.slice(0, firstJobStartIndex).trim() : ''; const endingText = lastJobEndIndex < markdown.length ? - markdown.slice(lastJobEndIndex).trim() : ''; + markdown.slice(lastJobEndIndex).trim() : ''; + const jobTexts = jobs.map((job, index) => { - return `第 ${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${job.salary},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}。`; + // 处理薪资格式:将 "数字-数字" 中的 '-' 替换为 '到' + let salaryText = job.salary; + if (salaryText) { + // 匹配 "数字-数字" 的格式(如:15000-18000元/月) + salaryText = salaryText.replace(/(\d+)-(\d+)/g, '$1到$2'); + } + + return `第 ${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${salaryText},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}。`; }); + const finalTextParts = []; if (guideText) finalTextParts.push(guideText); finalTextParts.push(...jobTexts); if (endingText) finalTextParts.push(endingText); + return finalTextParts.join('\n'); -} + } // 使用方法 1 diff --git a/hook/useTTSPlayer-web.js b/hook/useTTSPlayer-web.js index 3fa0187..a8ca8a8 100644 --- a/hook/useTTSPlayer-web.js +++ b/hook/useTTSPlayer-web.js @@ -209,36 +209,48 @@ export function useTTSPlayer() { */ function extractSpeechText(markdown) { if (!markdown || markdown.indexOf('job-json') === -1) { - return markdown; + return markdown; } const jobRegex = /``` job-json\s*({[\s\S]*?})\s*```/g; const jobs = []; let match; let lastJobEndIndex = 0; let firstJobStartIndex = -1; + while ((match = jobRegex.exec(markdown)) !== null) { - const jobStr = match[1]; - try { - const job = JSON.parse(jobStr); - jobs.push(job); - if (firstJobStartIndex === -1) { - firstJobStartIndex = match.index; - } - lastJobEndIndex = jobRegex.lastIndex; - } catch (e) { - console.warn('JSON 解析失败', e); + const jobStr = match[1]; + try { + const job = JSON.parse(jobStr); + jobs.push(job); + if (firstJobStartIndex === -1) { + firstJobStartIndex = match.index; } + lastJobEndIndex = jobRegex.lastIndex; + } catch (e) { + console.warn('JSON 解析失败', e); + } } + const guideText = firstJobStartIndex > 0 ? - markdown.slice(0, firstJobStartIndex).trim() : ''; + markdown.slice(0, firstJobStartIndex).trim() : ''; const endingText = lastJobEndIndex < markdown.length ? - markdown.slice(lastJobEndIndex).trim() : ''; + markdown.slice(lastJobEndIndex).trim() : ''; + const jobTexts = jobs.map((job, index) => { - return `第 ${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${job.salary},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}。`; + // 处理薪资格式:将 "数字-数字" 中的 '-' 替换为 '到' + let salaryText = job.salary; + if (salaryText) { + // 匹配 "数字-数字" 的格式(如:15000-18000元/月) + salaryText = salaryText.replace(/(\d+)-(\d+)/g, '$1到$2'); + } + + return `第 ${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${salaryText},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}。`; }); + const finalTextParts = []; if (guideText) finalTextParts.push(guideText); finalTextParts.push(...jobTexts); if (endingText) finalTextParts.push(endingText); + return finalTextParts.join('\n'); -} \ No newline at end of file + } \ No newline at end of file