Skip to content

Commit 1f7a5b5

Browse files
committed
fix(studio): preserve caption text when parsing transcripts
1 parent 0e9f380 commit 1f7a5b5

4 files changed

Lines changed: 149 additions & 43 deletions

File tree

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/studio/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"@mcp-b/global": "^5.0.1",
110110
"@phosphor-icons/react": "^2.1.10",
111111
"@tanstack/react-virtual": "^3.14.13",
112+
"acorn": "^8.17.0",
112113
"cn": "0.2.4",
113114
"dockview-react": "8.3.1",
114115
"gsap": "^3.13.0",

‎packages/studio/src/captions/parser.test.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @vitest-environment node
2-
import { describe, it, expect } from "vitest";
2+
import { describe, it, expect, vi } from "vitest";
33
import { extractTranscript, buildCaptionModel, TranscriptWord } from "./parser";
44
import { DEFAULT_STYLE, DEFAULT_CONTAINER, DEFAULT_ANIMATION_SET } from "./types";
55

@@ -156,6 +156,76 @@ describe("extractTranscript", () => {
156156
});
157157
});
158158

159+
it.each([
160+
["array delimiter", "'x ]; y'", "x ]; y"],
161+
["trailing-comma text", "'literal ,] and ,}'", "literal ,] and ,}"],
162+
["object-key text", "'{ text: example }'", "{ text: example }"],
163+
["escaped quotes", String.raw`'It\'s "quoted"'`, `It's "quoted"`],
164+
["escaped double quotes", String.raw`'Say \"hello\"'`, 'Say "hello"'],
165+
["escaped backslash", String.raw`'C:\\captions\\'`, "C:\\captions\\"],
166+
["hex and Unicode escapes", String.raw`'\x41\u0042\u{1D11E}'`, "AB\u{1D11E}"],
167+
["control escapes", String.raw`'one\ntwo\tthree'`, "one\ntwo\tthree"],
168+
])("preserves %s in JavaScript strings", (_name, literal, text) => {
169+
const source = `<script>const TRANSCRIPT = [{ id: 'word-a', text: ${literal}, start: 0.25, end: 1.5 }];</script>`;
170+
expect(extractTranscript(source)).toEqual([{ id: "word-a", text, start: 0.25, end: 1.5 }]);
171+
});
172+
173+
it("preserves JSON values and filters malformed word entries", () => {
174+
const source = `const TRANSCRIPT = [
175+
null, 42, {"text":"missing timing"}, {"text":"bad timing","start":"0","end":1},
176+
{"id":7,"text":"valid","start":-0.25,"end":1e1,"extra":{"items":[true,false,null]}}
177+
];`;
178+
expect(extractTranscript(source)).toEqual([{ text: "valid", start: -0.25, end: 10 }]);
179+
});
180+
181+
it("accepts comments around static values", () => {
182+
expect(
183+
extractTranscript(`const TRANSCRIPT = [
184+
// Delimiter in a comment: ];
185+
{ text: 'hello', start: 0, end: 1, },
186+
] /* trailing comment */;`),
187+
).toEqual([{ text: "hello", start: 0, end: 1 }]);
188+
});
189+
190+
it.each([
191+
"[{ text: 'unterminated, start: 0, end: 1 }];",
192+
"[{ text: 'hello', start: 0, end: 1 };",
193+
String.raw`[{ text: '\xZZ', start: 0, end: 1 }];`,
194+
"[{ text: 'hello', start: 0, end: 1 }] garbage;",
195+
"[{ text: 'hello', start: 0, end: 1 },,];",
196+
"[{ text: getText(), start: 0, end: 1 }];",
197+
"[{ text: 'hello', start: offset, end: 1 }];",
198+
"[{ text: 'hello', start: 1 + 2, end: 4 }];",
199+
"[{ text: 'hello', start: -getTime(), end: 1 }];",
200+
"[{ text: 'hello', start: 0, end: 1, extra: /pattern/ }];",
201+
"[{ text: 'hello', start: 0, end: 1, extra: 1n }];",
202+
"[{ get text() { return 'hello'; }, start: 0, end: 1 }];",
203+
"[{ text: 'hello', start: 0, end: 1, extra() {} }];",
204+
"[{ ['text']: 'hello', start: 0, end: 1 }];",
205+
"[{ text, start: 0, end: 1 }];",
206+
"[{ ...word, text: 'hello', start: 0, end: 1 }];",
207+
"[...words];",
208+
"[{ text: `hello ${getText()}`, start: 0, end: 1 }];",
209+
"[{ text: 'hello', start: 0, end: 1, extra: { run: getText() } }];",
210+
"[{ text: 'hello', start: 0, end: 1 }].map(transform);",
211+
"[{ text: 'hello', start: 0, end: 1 }], run();",
212+
])("rejects malformed or executable initializers: %s", (initializer) => {
213+
expect(extractTranscript(`const TRANSCRIPT = ${initializer}`)).toEqual([]);
214+
});
215+
216+
it("never runs code while extracting a transcript", () => {
217+
const getCaptionText = vi.fn(() => "hello");
218+
vi.stubGlobal("getCaptionText", getCaptionText);
219+
try {
220+
expect(
221+
extractTranscript("const TRANSCRIPT = [{ text: getCaptionText(), start: 0, end: 1 }];"),
222+
).toEqual([]);
223+
expect(getCaptionText).not.toHaveBeenCalled();
224+
} finally {
225+
vi.unstubAllGlobals();
226+
}
227+
});
228+
159229
describe("real-world source samples", () => {
160230
it("handles a realistic production-style TRANSCRIPT block with many words", () => {
161231
const source = `

‎packages/studio/src/captions/parser.ts‎

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
// Parses a caption composition's JavaScript source to extract the transcript word array,
33
// and builds a CaptionModel from a TranscriptWord array.
44

5+
import {
6+
parseExpressionAt,
7+
tokenizer,
8+
type Expression,
9+
type Property,
10+
type SpreadElement,
11+
type UnaryExpression,
12+
} from "acorn";
513
import {
614
CaptionModel,
715
CaptionSegment,
@@ -102,24 +110,26 @@ export function buildCaptionModel(
102110
* Looks for `const TRANSCRIPT = [...]` or `const script = [...]` (also let/var)
103111
* and parses each `{ text, start, end }` object into TranscriptWord objects.
104112
*
113+
* Supports static literals only; expressions are never evaluated.
105114
* Returns an empty array if no transcript is found or if parsing fails.
106115
*/
107116
export function extractTranscript(source: string): TranscriptWord[] {
108-
// Match: (const|let|var) (TRANSCRIPT|script) = [...]
109-
// The array may span multiple lines and contain trailing commas.
110-
// The lazy [\s\S]*? anchors on the first `];` — assumes transcript word
111-
// text never contains a literal `];` string (safe for speech transcripts).
112-
const varPattern = /(?:const|let|var)\s+(?:TRANSCRIPT|script)\s*=\s*(\[[\s\S]*?\]);/;
113-
const match = source.match(varPattern);
117+
// Locate the initializer in either JavaScript or a complete HTML composition.
118+
// Acorn owns its boundary so delimiters inside strings remain literal text.
119+
const varPattern = /\b(?:const|let|var)\s+(?:TRANSCRIPT|script)\s*=\s*(?=\[)/;
120+
const match = varPattern.exec(source);
114121

115122
if (!match) {
116123
return [];
117124
}
118125

119-
const arrayLiteral = match[1];
120-
121126
try {
122-
return parseTranscriptArray(arrayLiteral);
127+
const options = { ecmaVersion: "latest" } as const;
128+
const expression = parseExpressionAt(source, match.index + match[0].length, options);
129+
if (expression.type !== "ArrayExpression") return [];
130+
const nextToken = tokenizer(source.slice(expression.end), options).getToken();
131+
if (nextToken.type.label !== ";") return [];
132+
return parseTranscriptArray(readStaticValue(expression));
123133
} catch {
124134
return [];
125135
}
@@ -262,51 +272,75 @@ export function parseCaptionComposition(
262272
return model;
263273
}
264274

265-
/**
266-
* Parses a JS array literal containing `{ text, start, end }` objects.
267-
*
268-
* Handles:
269-
* - Double-quoted and single-quoted string values
270-
* - Trailing commas after the last element or property
271-
* - Unquoted property keys (standard JS object literal syntax)
272-
* - Numeric values for start/end
273-
*/
274-
function parseTranscriptArray(arrayLiteral: string): TranscriptWord[] {
275-
// Try parsing as-is first (handles already-valid JSON)
276-
let parsed: unknown;
277-
try {
278-
parsed = JSON.parse(arrayLiteral);
279-
} catch {
280-
// Not valid JSON — normalize single quotes, unquoted keys, trailing commas
281-
let normalized = arrayLiteral;
282-
normalized = normalized.replace(/'((?:[^'\\]|\\.)*)'/g, (_match, inner) => {
283-
const escaped = inner.replace(/\\'/g, "'").replace(/"/g, '\\"');
284-
return `"${escaped}"`;
285-
});
286-
normalized = normalized.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g, '$1"$2":');
287-
normalized = normalized.replace(/,(\s*[}\]])/g, "$1");
288-
parsed = JSON.parse(normalized);
275+
/** Decode data literals only, including metadata that is not used by captions. */
276+
function readStaticValue(node: Expression | SpreadElement | null): unknown {
277+
switch (node?.type) {
278+
case "Literal":
279+
// Acorn's remaining literals are strings, numbers, booleans, or null.
280+
if ("regex" in node || "bigint" in node) break;
281+
return node.value;
282+
case "UnaryExpression":
283+
return readSignedNumber(node);
284+
case "ArrayExpression":
285+
return node.elements.map(readStaticValue);
286+
case "ObjectExpression":
287+
return Object.fromEntries(node.properties.map(readStaticProperty));
289288
}
289+
throw new SyntaxError("Transcript values must be static literals");
290+
}
291+
292+
function readSignedNumber(node: UnaryExpression): number {
293+
if (
294+
(node.operator !== "-" && node.operator !== "+") ||
295+
node.argument.type !== "Literal" ||
296+
typeof node.argument.value !== "number"
297+
) {
298+
throw new SyntaxError("Transcript unary expressions must be signed numeric literals");
299+
}
300+
return node.operator === "-" ? -node.argument.value : node.argument.value;
301+
}
302+
303+
function readStaticProperty(property: Property | SpreadElement): [string | number, unknown] {
304+
if (
305+
property.type !== "Property" ||
306+
property.kind !== "init" ||
307+
property.method ||
308+
property.shorthand ||
309+
property.computed
310+
) {
311+
throw new SyntaxError("Transcript properties must be static data");
312+
}
313+
const key =
314+
property.key.type === "Identifier" ? property.key.name : readStaticValue(property.key);
315+
if (typeof key !== "string" && typeof key !== "number") {
316+
throw new SyntaxError("Transcript property keys must be names or literals");
317+
}
318+
return [key, readStaticValue(property.value)];
319+
}
290320

321+
function parseTranscriptArray(parsed: unknown): TranscriptWord[] {
291322
if (!Array.isArray(parsed)) {
292323
return [];
293324
}
294325

295326
const words: TranscriptWord[] = [];
296-
for (const item of parsed) {
327+
const items: unknown[] = parsed;
328+
for (const item of items) {
297329
if (
298330
item !== null &&
299331
typeof item === "object" &&
300-
typeof (item as Record<string, unknown>).text === "string" &&
301-
typeof (item as Record<string, unknown>).start === "number" &&
302-
typeof (item as Record<string, unknown>).end === "number"
332+
"text" in item &&
333+
typeof item.text === "string" &&
334+
"start" in item &&
335+
typeof item.start === "number" &&
336+
"end" in item &&
337+
typeof item.end === "number"
303338
) {
304-
const entry = item as Record<string, unknown>;
305339
words.push({
306-
...(typeof entry.id === "string" ? { id: entry.id } : {}),
307-
text: entry.text as string,
308-
start: entry.start as number,
309-
end: entry.end as number,
340+
...("id" in item && typeof item.id === "string" ? { id: item.id } : {}),
341+
text: item.text,
342+
start: item.start,
343+
end: item.end,
310344
});
311345
}
312346
}

0 commit comments

Comments
 (0)