refactor(server/utils): move html normalizer into its own importable function

This commit is contained in:
parent be0107b415
commit fd4de6489f
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
2 changed files with 10 additions and 6 deletions

@ -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, "<br>")
.replace(/<br><p>/gm, "<p>")
.replace(/<p>&nbsp;<\/p>/gm, "")
.replace(/<p><br\s\/><b\s\/><\/p>/gm, "");
}

@ -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, "<br>")
.replace(/<br><p>/gm, "<p>")
.replace(/<p>&nbsp;<\/p>/gm, "")
.replace(/<p><br\s\/><b\s\/><\/p>/gm, ""),
text: norm(stream),
};
finObj.totalChapters = story.chapters.length;
finObj.chapterNames = story.chapters.map((a) => a.title);