CoastalCommitsPastes/client/lib/server/get-html-from-drift-file.ts

44 lines
829 B
TypeScript
Raw Normal View History

import type { File } from "@lib/server/prisma"
import markdown from "@wcj/markdown-to-html"
2022-11-09 21:38:05 -05:00
/**
* returns rendered HTML from a Drift file
*/
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)) {
contentToRender = `
~~~${type}
2022-11-09 21:38:05 -05:00
${content}
~~~
`
2022-11-09 21:38:05 -05:00
} else {
contentToRender = "\n" + content
}
const html = markdown(contentToRender, {
showLineNumbers: false
})
2022-11-09 21:38:05 -05:00
return html
}