Updated to always be string[]
This commit is contained in:
@@ -1,18 +1,15 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import type { ChapterView, TextFragment } from '../types.ts';
|
||||
import type { ChapterView } from '../types.ts';
|
||||
|
||||
interface ChapterRow {
|
||||
chapter: number;
|
||||
contents: string[] | TextFragment[];
|
||||
contents: string[];
|
||||
}
|
||||
|
||||
function readChapterContents(filePath: string, hasStyle: boolean): string[] | TextFragment[] {
|
||||
function readChapterContents(filePath: string): string[] {
|
||||
try {
|
||||
const raw = readFileSync(filePath, 'utf-8');
|
||||
if (hasStyle) {
|
||||
return JSON.parse(raw) as TextFragment[];
|
||||
}
|
||||
return raw.split(/[\n\r]+/).map(l => l.trim()).filter(Boolean);
|
||||
const fragments = JSON.parse(readFileSync(filePath, 'utf-8')) as Array<{ body: string }>;
|
||||
return fragments.map(f => f.body).filter(Boolean);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
@@ -21,7 +18,7 @@ function readChapterContents(filePath: string, hasStyle: boolean): string[] | Te
|
||||
export function renderJson(view: ChapterView): string {
|
||||
const rows: ChapterRow[] = view.chapters.map((chapter, i) => ({
|
||||
chapter: view.min + i,
|
||||
contents: readChapterContents(chapter.path, chapter.hasStyle),
|
||||
contents: readChapterContents(chapter.path),
|
||||
}));
|
||||
return JSON.stringify(rows);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user