Updated to always be string[]
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user