2022-11-11 22:17:44 -05:00
|
|
|
import type { File } from "@lib/server/prisma"
|
2022-11-12 19:06:23 -05:00
|
|
|
import markdown from "@wcj/markdown-to-html"
|
2022-11-09 21:38:05 -05:00
|
|
|
/**
|
|
|
|
* returns rendered HTML from a Drift file
|
|
|
|
*/
|
2022-11-12 19:06:23 -05:00
|
|
|
export async function getHtmlFromFile({
|
2022-11-10 02:11:36 -05:00
|
|
|
content,
|
|
|
|
title
|
|
|
|
}: Pick<File, "content" | "title">) {
|
2022-11-09 21:38:05 -05:00
|
|
|
const renderAsMarkdown = [
|
|
|
|
"markdown",
|
|
|
|
"md",
|
|
|
|
"mdown",
|
|
|
|
"mkdn",
|
|
|
|
"mkd",
|
|
|
|
"mdwn",
|
|
|
|
"mdtxt",
|
|
|
|
"mdtext",
|
|
|
|
"text",
|
|
|
|
""
|
|
|
|
]
|
|
|
|
const fileType = () => {
|
|
|
|
const pathParts = title.split(".")
|
|
|
|
const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
|
|
|
|
return language
|
|
|
|
}
|
|
|
|
const type = fileType()
|
|
|
|
let contentToRender: string = content || ""
|
|
|
|
if (!renderAsMarkdown.includes(type)) {
|
2022-11-12 02:59:33 -05:00
|
|
|
contentToRender = `
|
|
|
|
~~~${type}
|
2022-11-09 21:38:05 -05:00
|
|
|
${content}
|
2022-11-12 02:59:33 -05:00
|
|
|
~~~
|
|
|
|
`
|
2022-11-09 21:38:05 -05:00
|
|
|
} else {
|
|
|
|
contentToRender = "\n" + content
|
|
|
|
}
|
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const html = markdown(contentToRender, {
|
|
|
|
showLineNumbers: false
|
|
|
|
})
|
2022-11-09 21:38:05 -05:00
|
|
|
return html
|
|
|
|
}
|