| /** Split plain text into sentences for chunked TTS (aligned with AskJerry / ExperimentPage). */ | |
| export function extractTtsSentences(text: string, includeTrailing = false): string[] { | |
| const t = (text || '').trim() | |
| if (!t) return [] | |
| const re = /[^.!?\n]+[.!?\n]+/g | |
| const sentences: string[] = [] | |
| let match: RegExpExecArray | null | |
| let lastEnd = 0 | |
| while ((match = re.exec(t)) !== null) { | |
| const s = match[0].trim() | |
| if (s) sentences.push(s) | |
| lastEnd = re.lastIndex | |
| } | |
| if (!sentences.length) return includeTrailing ? [t] : [] | |
| if (includeTrailing) { | |
| const remainder = t.slice(lastEnd).trim() | |
| if (remainder) sentences.push(remainder) | |
| } | |
| return sentences | |
| } | |