(dir: string, words: Word[])
| 488 | } |
| 489 | |
| 490 | export function patchCaptionHtml(dir: string, words: Word[]): void { |
| 491 | if (words.length === 0) return; |
| 492 | |
| 493 | // Indent to 10 spaces to match typical composition script indentation |
| 494 | const wordsJson = JSON.stringify(words, null, 2).replace(/\n/g, "\n "); |
| 495 | |
| 496 | let htmlFiles: string[]; |
| 497 | try { |
| 498 | htmlFiles = readdirSync(dir, { withFileTypes: true, recursive: true }) |
| 499 | .filter((e) => e.isFile() && e.name.endsWith(".html")) |
| 500 | .map((e) => join(e.parentPath, e.name)); |
| 501 | } catch { |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | for (const file of htmlFiles) { |
| 506 | let content = readFileSync(file, "utf-8"); |
| 507 | const scriptBlocks = content.match(/<script>[\s\S]*?<\/script>/g) ?? []; |
| 508 | let scriptMatch: RegExpMatchArray | null = null; |
| 509 | let transcriptMatch: RegExpMatchArray | null = null; |
| 510 | for (const block of scriptBlocks) { |
| 511 | scriptMatch = scriptMatch ?? block.match(/const script = \[[\s\S]*?\];/); |
| 512 | transcriptMatch = transcriptMatch ?? block.match(/const TRANSCRIPT = \[[\s\S]*?\];/); |
| 513 | } |
| 514 | const match = scriptMatch ?? transcriptMatch; |
| 515 | if (match) { |
| 516 | const varName = scriptMatch ? "script" : "TRANSCRIPT"; |
| 517 | content = content.replace(match[0], `const ${varName} = ${wordsJson};`); |
| 518 | writeFileSync(file, content, "utf-8"); |
| 519 | } |
| 520 | } |
| 521 | } |
no outgoing calls
no test coverage detected