next/lib/server/storyHelpers/bodyHandler.ts
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ dd74c8417a
fix(server/utils): fix mammoth and sanitize-html imports
(make it so that we're not importing a namespace instead of a default export)
2023-10-10 22:04:42 -04:00

39 lines
1.0 KiB
TypeScript

import { extname, resolve } from "path";
import { readFileSync } from "fs";
import { marked } from "marked";
import mammoth from "mammoth";
import 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);
}