From 352296b98d61ac1f6e25d5bc43f4fb43c70efde7 Mon Sep 17 00:00:00 2001 From: Tom Boullay Date: Sun, 10 May 2026 00:49:19 +0100 Subject: [PATCH] fix: add config vite error srt --- vite.config.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/vite.config.ts b/vite.config.ts index cde971b..3b81b0a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url"; import type { ServerResponse } from "node:http"; import type { Plugin } from "vite"; import { parseMapNodes } from "./src/utils/map/mapNodeValidation"; +import { parseSrt } from "./src/utils/subtitles/parseSrt"; const __dirname = fileURLToPath(new URL(".", import.meta.url)); @@ -105,6 +106,11 @@ const saveSrtPlugin = (): Plugin => ({ return; } + if (!isValidSrtContent(data.content)) { + sendJson(res, 400, { error: "Invalid SRT content" }); + return; + } + const subtitlesRoot = path.resolve( __dirname, "public/sounds/dialogue/subtitles", @@ -151,6 +157,26 @@ function isSrtPayload(data: unknown): data is SrtPayload { ); } +function isValidSrtContent(content: string): boolean { + const blocks = content + .replace(/^\uFEFF/, "") + .replace(/\r/g, "") + .trim() + .split(/\n{2,}/) + .filter(Boolean); + const cues = parseSrt(content); + + if (blocks.length === 0 || cues.length !== blocks.length) return false; + + const cueIndexes = new Set(); + for (const cue of cues) { + if (cueIndexes.has(cue.index)) return false; + cueIndexes.add(cue.index); + } + + return true; +} + export default defineConfig({ plugins: [react(), saveMapPlugin(), saveSrtPlugin()], resolve: {