From fd4de6489faaad1c604e1f23c13a75eeba763615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Mon, 11 Dec 2023 21:13:22 -0500 Subject: [PATCH] refactor(server/utils): move html normalizer into its own importable function --- lib/functions.ts | 8 ++++++++ lib/server/dbHelpers/chapterTransformer.ts | 8 ++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/functions.ts b/lib/functions.ts index 5298d2b..c956a97 100644 --- a/lib/functions.ts +++ b/lib/functions.ts @@ -57,3 +57,11 @@ export function stringifyStream( stream.on("end", () => res(Buffer.concat(chunks).toString("utf-8"))); }); } + +export function norm(text: string): string { + return text + .replace(/\n/g, "
") + .replace(/

/gm, "

") + .replace(/

 <\/p>/gm, "") + .replace(/

<\/p>/gm, ""); +} diff --git a/lib/server/dbHelpers/chapterTransformer.ts b/lib/server/dbHelpers/chapterTransformer.ts index 722e89a..c1361fc 100644 --- a/lib/server/dbHelpers/chapterTransformer.ts +++ b/lib/server/dbHelpers/chapterTransformer.ts @@ -1,7 +1,7 @@ import { H3Event, EventHandlerRequest } from "h3"; import { GridFSBucket } from "mongodb"; import mongoose, { Document } from "mongoose"; -import { stringifyStream } from "~/lib/functions"; +import { norm, stringifyStream } from "~/lib/functions"; import { IStory } from "~/models/stories"; import { IChapter } from "~/models/stories/chapter"; import getBucket from "../storyHelpers/getBucket"; @@ -24,11 +24,7 @@ export default async function ( let stream = await stringifyStream(ds); finObj.currentChapter = { ...cloned.chapters[cindex || event.context.chapterIndex || 0], - text: stream - .replace(/\n/g, "
") - .replace(/

/gm, "

") - .replace(/

 <\/p>/gm, "") - .replace(/

<\/p>/gm, ""), + text: norm(stream), }; finObj.totalChapters = story.chapters.length; finObj.chapterNames = story.chapters.map((a) => a.title);