Updated to always be string[]

This commit is contained in:
qwsdcvghyu89
2026-06-17 08:24:02 +10:00
parent e190a5411f
commit e570974f06
+6 -9
View File
@@ -1,18 +1,15 @@
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import type { ChapterView, TextFragment } from '../types.ts'; import type { ChapterView } from '../types.ts';
interface ChapterRow { interface ChapterRow {
chapter: number; chapter: number;
contents: string[] | TextFragment[]; contents: string[];
} }
function readChapterContents(filePath: string, hasStyle: boolean): string[] | TextFragment[] { function readChapterContents(filePath: string): string[] {
try { try {
const raw = readFileSync(filePath, 'utf-8'); const fragments = JSON.parse(readFileSync(filePath, 'utf-8')) as Array<{ body: string }>;
if (hasStyle) { return fragments.map(f => f.body).filter(Boolean);
return JSON.parse(raw) as TextFragment[];
}
return raw.split(/[\n\r]+/).map(l => l.trim()).filter(Boolean);
} catch { } catch {
return []; return [];
} }
@@ -21,7 +18,7 @@ function readChapterContents(filePath: string, hasStyle: boolean): string[] | Te
export function renderJson(view: ChapterView): string { export function renderJson(view: ChapterView): string {
const rows: ChapterRow[] = view.chapters.map((chapter, i) => ({ const rows: ChapterRow[] = view.chapters.map((chapter, i) => ({
chapter: view.min + i, chapter: view.min + i,
contents: readChapterContents(chapter.path, chapter.hasStyle), contents: readChapterContents(chapter.path),
})); }));
return JSON.stringify(rows); return JSON.stringify(rows);
} }