2023-10-03 00:37:46 -04:00
|
|
|
import { extname, resolve } from "path";
|
|
|
|
import { readFileSync } from "fs";
|
|
|
|
import { marked } from "marked";
|
2023-10-10 22:04:42 -04:00
|
|
|
import mammoth from "mammoth";
|
|
|
|
import san from "sanitize-html";
|
2023-10-03 00:37:46 -04:00
|
|
|
import { sanitizeConf } from "../constants";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { FormChapter } from "@client/types/form/story";
|
2023-10-12 22:58:10 -04:00
|
|
|
import { log } from "../logger";
|
2023-10-03 00:37:46 -04:00
|
|
|
|
|
|
|
export default async function (bodyObj: FormChapter): Promise<string> {
|
|
|
|
let str: string = "";
|
2023-10-12 22:58:10 -04:00
|
|
|
log.debug(JSON.stringify(bodyObj), { label: "bodyhandler" });
|
2023-10-03 00:37:46 -04:00
|
|
|
if (bodyObj.content) {
|
|
|
|
str = bodyObj.content;
|
|
|
|
} else if (bodyObj.file) {
|
|
|
|
let ext = extname(bodyObj.file).toLowerCase();
|
2023-12-29 20:53:29 -05:00
|
|
|
if (ext === ".md" || ext === ".markdown") str = marked.parse(readFileSync(resolve(`tmp/${bodyObj.file}`)).toString());
|
2023-10-12 22:58:10 -04:00
|
|
|
else if (ext === ".doc" || ext === ".docx")
|
2023-12-29 20:53:29 -05:00
|
|
|
str = (await mammoth.convertToHtml({ path: resolve(`tmp/${bodyObj.file}`) }, { styleMap: ["b => b", "i => i", "u => u"] })).value;
|
2023-10-03 00:37:46 -04:00
|
|
|
else
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: "bad file type",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: "no content",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return san(str, sanitizeConf);
|
|
|
|
}
|