Implemented markdown preview
This commit is contained in:
parent
d5ab483e9d
commit
e4972b54d0
9 changed files with 957 additions and 183 deletions
56
index.js
56
index.js
|
@ -1,9 +1,65 @@
|
|||
"use strict"
|
||||
|
||||
import "dotenv/config"
|
||||
import express from "express"
|
||||
import got from "got"
|
||||
import fsExtra from "fs-extra"
|
||||
import prettier from "prettier"
|
||||
import handlebars from "handlebars"
|
||||
|
||||
const { pathExists, readFile } = fsExtra
|
||||
|
||||
const gfm = handlebars.compile(await readFile("./gfm.hbs", "utf8"))
|
||||
|
||||
const app = express()
|
||||
|
||||
app.get("/github-markdown.css", async (req, res, next) => {
|
||||
try {
|
||||
const path = "./node_modules/github-markdown-css/github-markdown.css"
|
||||
const exists = await pathExists(path)
|
||||
if (exists === false) {
|
||||
return next()
|
||||
}
|
||||
const css = await readFile(path, "utf8")
|
||||
res.setHeader("Content-Type", "text/css")
|
||||
return res.send(css)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
|
||||
app.get("*.md", async (req, res, next) => {
|
||||
try {
|
||||
const path = `.${req.url}`
|
||||
const exists = await pathExists(path)
|
||||
if (exists === false) {
|
||||
return next()
|
||||
}
|
||||
const markdown = await readFile(path, "utf8")
|
||||
const response = await got.post("https://api.github.com/markdown", {
|
||||
json: {
|
||||
mode: "gfm",
|
||||
text: markdown,
|
||||
},
|
||||
})
|
||||
res.setHeader("Content-Type", "text/html")
|
||||
return res.send(
|
||||
prettier.format(
|
||||
gfm({
|
||||
markdown: response.body,
|
||||
}),
|
||||
{
|
||||
parser: "html",
|
||||
}
|
||||
)
|
||||
)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
|
||||
app.use(
|
||||
express.static(".", {
|
||||
dotfiles: "ignore",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue