2022-03-22 20:37:21 -04:00
|
|
|
import type { NextApiHandler } from "next";
|
|
|
|
|
|
|
|
import markdown from "@lib/render-markdown";
|
|
|
|
|
|
|
|
const renderMarkdown: NextApiHandler = async (req, res) => {
|
2022-03-22 23:06:15 -04:00
|
|
|
const { content, title } = req.body
|
|
|
|
const renderAsMarkdown = ['markdown', 'md', 'mdown', 'mkdn', 'mkd', 'mdwn', 'mdtxt', 'mdtext', 'text', '']
|
2022-03-22 20:37:21 -04:00
|
|
|
const fileType = () => {
|
|
|
|
const pathParts = title.split(".")
|
|
|
|
const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
|
|
|
|
return language
|
|
|
|
}
|
|
|
|
const type = fileType()
|
2022-03-22 23:16:24 -04:00
|
|
|
let contentToRender: string = (content || '');
|
2022-03-22 20:37:21 -04:00
|
|
|
|
|
|
|
if (!renderAsMarkdown.includes(type)) {
|
|
|
|
contentToRender = `~~~${type}
|
|
|
|
${content}
|
|
|
|
~~~`
|
2022-03-22 23:16:24 -04:00
|
|
|
} else {
|
|
|
|
contentToRender = '\n' + content;
|
2022-03-22 20:37:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof contentToRender !== 'string') {
|
|
|
|
res.status(400).send('content must be a string')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.status(200).write(markdown(contentToRender))
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
export default renderMarkdown
|