( vttContent: string, entryId: number, newText: string, )
| 3 | } |
| 4 | |
| 5 | export function updateVttEntryText( |
| 6 | vttContent: string, |
| 7 | entryId: number, |
| 8 | newText: string, |
| 9 | ): { content: string; updated: boolean } { |
| 10 | const normalizedText = normalizeTranscriptCueText(newText); |
| 11 | const lines = vttContent.split(/\r?\n/); |
| 12 | const updatedLines: string[] = []; |
| 13 | let index = 0; |
| 14 | let updated = false; |
| 15 | |
| 16 | while (index < lines.length) { |
| 17 | const line = lines[index] ?? ""; |
| 18 | const trimmedLine = line.trim(); |
| 19 | |
| 20 | if (!/^\d+$/.test(trimmedLine)) { |
| 21 | updatedLines.push(line); |
| 22 | index++; |
| 23 | continue; |
| 24 | } |
| 25 | |
| 26 | const cueId = parseInt(trimmedLine, 10); |
| 27 | const cueStart = index; |
| 28 | let cueEnd = cueStart + 1; |
| 29 | |
| 30 | while (cueEnd < lines.length && (lines[cueEnd] ?? "").trim() !== "") { |
| 31 | cueEnd++; |
| 32 | } |
| 33 | |
| 34 | if (cueId !== entryId) { |
| 35 | updatedLines.push(...lines.slice(cueStart, cueEnd)); |
| 36 | if (cueEnd < lines.length) { |
| 37 | updatedLines.push(lines[cueEnd] ?? ""); |
| 38 | } |
| 39 | index = cueEnd + 1; |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | const cueLines = lines.slice(cueStart, cueEnd); |
| 44 | const timingIndex = cueLines.findIndex((cueLine) => |
| 45 | cueLine.includes("-->"), |
| 46 | ); |
| 47 | |
| 48 | if (timingIndex === -1) { |
| 49 | updatedLines.push(...cueLines); |
| 50 | if (cueEnd < lines.length) { |
| 51 | updatedLines.push(lines[cueEnd] ?? ""); |
| 52 | } |
| 53 | index = cueEnd + 1; |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | updatedLines.push(...cueLines.slice(0, timingIndex + 1), normalizedText); |
| 58 | if (cueEnd < lines.length) { |
| 59 | updatedLines.push(lines[cueEnd] ?? ""); |
| 60 | } |
| 61 | updated = true; |
| 62 | index = cueEnd + 1; |
no test coverage detected