refactor(server/utils): add chapter index argument to chapterTransformer

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2023-10-11 16:37:18 -04:00
parent 8d162c8029
commit 64f3bf23ea
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

View File

@ -9,17 +9,20 @@ import getBucket from "../storyHelpers/getBucket";
export default async function (
story: Document<number, {}, IStory> & IStory,
event: H3Event<EventHandlerRequest>,
cindex?: number,
) {
const finObj: any = story.toObject();
const cloned: any & { chapters: IChapter[] } = { ...finObj };
delete finObj.chapters;
const bucket = getBucket();
let ds = bucket.openDownloadStreamByName(
`/stories/${cloned.chapters[event.context.chapterIndex || 0].id}.txt`,
`/stories/${
cloned.chapters[cindex || event.context.chapterIndex || 0].id
}.txt`,
);
let stream = await stringifyStream(ds);
finObj.currentChapter = {
...cloned.chapters[event.context.chapterIndex || 0],
...cloned.chapters[cindex || event.context.chapterIndex || 0],
text: stream
.replace(/\n/g, "<br>")
.replace(/<br><p>/gm, "<p>")