CoastalCommitsPastes/server/src/lib/get-html-from-drift-file.ts
NDI Lionel b6439858df
server: add JWTDenyList table and signout route (#52)
* add models and signout route
* add migration file for JWTDenylist

Closes #25 

Co-authored-by: Max Leiter <maxwell.leiter@gmail.com>
2022-04-06 08:39:06 -07:00

40 lines
804 B
TypeScript

import markdown from "./render-markdown"
import { File } from "@lib/models/File"
/**
* returns rendered HTML from a Drift file
*/
function getHtmlFromFile({ content, title }: Pick<File, "content" | "title">) {
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}
${content}
~~~`
} else {
contentToRender = "\n" + content
}
const html = markdown(contentToRender)
return html
}
export default getHtmlFromFile