Skip to content

Commit 1fac5fb

Browse files
fix(cli): preserve literal WebVTT cue text (#5236)
1 parent fe38e8f commit 1fac5fb

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎packages/cli/src/whisper/normalize.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,33 @@ Short format
281281
});
282282

283283
describe("caption formatting", () => {
284+
it.each([
285+
["R&D <config> next", "R&amp;D &lt;config&gt; next"],
286+
["Literal &lt; &amp; references", "Literal &amp;lt; &amp;amp; references"],
287+
["3 < 4 and 5 > 2", "3 &lt; 4 and 5 &gt; 2"],
288+
["R&D --> next", "R&amp;D --&gt; next"],
289+
])("preserves literal WebVTT text %j on export and reimport", (text, payload) => {
290+
const words = [{ text, start: 1, end: 2, id: "w0" }];
291+
const output = formatVtt(words);
292+
expect(output).toBe(`WEBVTT\n\n00:00:01.000 --> 00:00:02.000\n${payload}\n`);
293+
expect(loadTranscript(tmpFile("literal.vtt", output)).words).toEqual(words);
294+
});
295+
296+
it.each([
297+
["R&amp;D &lt;config&gt; next", "R&D <config> next"],
298+
["&quot;quoted&quot; &apos;text&apos;", "\"quoted\" 'text'"],
299+
["&#38; &#x3c; &#60;", "& < <"],
300+
["Literal &amp;lt; &amp;amp; references", "Literal &lt; &amp; references"],
301+
["<b>R&amp;D</b> &lt;config&gt;", "R&D <config>"],
302+
["A&nbsp;B&lrm;&rlm;", "A\u00a0B\u200e\u200f"],
303+
["Keep &unknown;", "Keep &unknown;"],
304+
])("decodes the WebVTT cue payload %j once", (payload, text) => {
305+
const input = `WEBVTT\n\n00:00:01.000 --> 00:00:02.000\n${payload}\n`;
306+
expect(loadTranscript(tmpFile("entities.vtt", input)).words).toEqual([
307+
{ text, start: 1, end: 2, id: "w0" },
308+
]);
309+
});
310+
284311
it("round-trips SRT cues through normalized words", () => {
285312
const srt = `1
286313
00:00:01,000 --> 00:00:03,500

‎packages/cli/src/whisper/normalize.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
22
import { extname, join } from "node:path";
3+
import { parseHTML } from "linkedom";
34

45
export interface Word {
56
/** Stable identifier for referencing this word in overrides and compositions.
@@ -217,6 +218,14 @@ function parseSrt(content: string): Word[] {
217218
return words;
218219
}
219220

221+
function decodeVttText(text: string): string {
222+
if (!text.includes("&")) return text;
223+
const { document } = parseHTML("<html><body></body></html>");
224+
const element = document.createElement("span");
225+
element.innerHTML = text.replace(/</g, "&lt;");
226+
return element.textContent ?? "";
227+
}
228+
220229
function parseVtt(content: string): Word[] {
221230
const body = content.replace(/^WEBVTT[^\n]*\n/, "");
222231
// VTT is structurally similar to SRT (without numeric indices)
@@ -239,7 +248,7 @@ function parseVtt(content: string): Word[] {
239248
if (!text) continue;
240249

241250
words.push({
242-
text,
251+
text: decodeVttText(text),
243252
start: parseVttTimestamp(startStr),
244253
end: parseVttTimestamp(endStr),
245254
});
@@ -468,7 +477,8 @@ export function formatVtt(words: Word[], opts?: WordsToCuesOptions): string {
468477
"WEBVTT\n\n" +
469478
cues
470479
.map(
471-
(cue) => `${formatVttTimestamp(cue.start)} --> ${formatVttTimestamp(cue.end)}\n${cue.text}`,
480+
(cue) =>
481+
`${formatVttTimestamp(cue.start)} --> ${formatVttTimestamp(cue.end)}\n${cue.text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")}`,
472482
)
473483
.join("\n\n") +
474484
"\n"

0 commit comments

Comments
 (0)