Skip to content

Commit 48495d7

Browse files
committed
fix(cli): split block extraction into focused functions and fix regex to match exact keywords
1 parent a7ebd61 commit 48495d7

File tree

1 file changed

+21
-27
lines changed
  • packages/cli/src/cli/loaders

1 file changed

+21
-27
lines changed

packages/cli/src/cli/loaders/vtt.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,36 @@ import webvtt from "node-webvtt";
22
import { ILoader } from "./_types";
33
import { createLoader } from "./_utils";
44

5-
// node-webvtt doesn't handle STYLE/REGION blocks — strip them before parsing
6-
function extractUnsupportedBlocks(input: string): {
7-
cleaned: string;
8-
blocks: string[];
9-
} {
10-
const parts = input.replace(/\r\n/g, "\n").split("\n\n");
11-
const blocks: string[] = [];
12-
const kept: string[] = [];
13-
14-
const unsupportedRegex = /^(?:STYLE|REGION)/;
5+
const UNSUPPORTED_BLOCK_REGEX = /^(?:STYLE|REGION)\s*$/;
156

16-
for (const part of parts) {
17-
const firstLine = part.trimStart().split("\n", 1)[0];
7+
function isUnsupportedBlock(block: string): boolean {
8+
const firstLine = block.trimStart().split("\n", 1)[0];
9+
return UNSUPPORTED_BLOCK_REGEX.test(firstLine);
10+
}
1811

19-
if (unsupportedRegex.test(firstLine)) {
20-
blocks.push(part);
21-
} else {
22-
kept.push(part);
23-
}
24-
}
12+
// node-webvtt doesn't handle STYLE/REGION blocks — strip them before parsing
13+
function stripUnsupportedBlocks(input: string): string {
14+
return input
15+
.replace(/\r\n/g, "\n")
16+
.split("\n\n")
17+
.filter((part) => !isUnsupportedBlock(part))
18+
.join("\n\n");
19+
}
2520

26-
return { cleaned: kept.join("\n\n"), blocks };
21+
function getUnsupportedBlocks(input: string): string[] {
22+
return input.replace(/\r\n/g, "\n").split("\n\n").filter(isUnsupportedBlock);
2723
}
2824

2925
export default function createVttLoader(): ILoader<
3026
string,
3127
Record<string, any>
3228
> {
33-
let savedBlocks: string[] = [];
34-
3529
return createLoader({
3630
async pull(locale, input) {
3731
if (!input) {
3832
return ""; // if VTT file does not exist yet we can not parse it - return empty string
3933
}
40-
const { cleaned, blocks } = extractUnsupportedBlocks(input);
41-
savedBlocks = blocks;
42-
const vtt = webvtt.parse(cleaned)?.cues;
34+
const vtt = webvtt.parse(stripUnsupportedBlocks(input))?.cues;
4335
if (Object.keys(vtt).length === 0) {
4436
return {};
4537
} else {
@@ -50,7 +42,7 @@ export default function createVttLoader(): ILoader<
5042
}, {});
5143
}
5244
},
53-
async push(locale, payload) {
45+
async push(locale, payload, originalInput) {
5446
const output = Object.entries(payload).map(([key, text]) => {
5547
const [id, timeRange, identifier] = key.split("#");
5648
const [startTime, endTime] = timeRange.split("-");
@@ -71,11 +63,13 @@ export default function createVttLoader(): ILoader<
7163
};
7264

7365
const compiled = webvtt.compile(input);
74-
if (savedBlocks.length === 0) return compiled;
7566

7667
// Re-insert STYLE/REGION blocks after the WEBVTT header
68+
const blocks = getUnsupportedBlocks(originalInput ?? "");
69+
if (blocks.length === 0) return compiled;
70+
7771
const [header, ...rest] = compiled.split("\n\n");
78-
return [header, ...savedBlocks, ...rest].join("\n\n");
72+
return [header, ...blocks, ...rest].join("\n\n");
7973
},
8074
});
8175
}

0 commit comments

Comments
 (0)