☙◦ The Tablet ❀ GamerGirlandCo ◦❧
c8bdcc0ec3
`bodyHandler` takes a request body and returns the chapter content as a string `formChapterTransform` serializes a chapter-like object supplied in a request body to an actual proper chapter object `getBucket` retrieves the story content gridfs bucket `replaceGridFS` either creates or updates (deletes + reuploads) a gridfs file
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { extname, resolve } from "path";
|
|
import { readFileSync } from "fs";
|
|
import { marked } from "marked";
|
|
import * as mammoth from "mammoth";
|
|
import * as san from "sanitize-html";
|
|
import { sanitizeConf } from "../constants";
|
|
import { FormChapter } from "~/lib/client/types/form/story";
|
|
|
|
export default async function (bodyObj: FormChapter): Promise<string> {
|
|
let str: string = "";
|
|
if (bodyObj.content) {
|
|
str = bodyObj.content;
|
|
} else if (bodyObj.file) {
|
|
let ext = extname(bodyObj.file).toLowerCase();
|
|
if (ext === "md" || ext === "markdown")
|
|
str = marked.parse(
|
|
readFileSync(resolve(`tmp/${bodyObj.file}`)).toString(),
|
|
);
|
|
else if (ext === "doc" || ext === "docx")
|
|
str = (
|
|
await mammoth.convertToHtml(
|
|
{ path: resolve(`tmp/${bodyObj.file}`) },
|
|
{ styleMap: ["b => b", "i => i", "u => u"] },
|
|
)
|
|
).value;
|
|
else
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "bad file type",
|
|
});
|
|
} else {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "no content",
|
|
});
|
|
}
|
|
return san(str, sanitizeConf);
|
|
}
|