-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (66 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require("dotenv").config()
require("toml-require").install({
toml: require('toml')
})
const fs = require("fs");
const glob = require("glob")
const frontMatter = require("front-matter");
const hljs = require('highlight.js');
const express = require("express");
const path = require('path');
const mustache = require("mustache")
const app = express();
const http = require("http");
const config = require("./config.toml")
const md = require("markdown-it")({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return '<pre class="hljs"><code>' +
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
'</code></pre>';
} catch (__) {}
}
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
},
html: true
});
app.use(express.static(path.join(__dirname, "public")))
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.disable("x-powered-by")
md.use(require("markdown-it-task-lists"), { label: true, labelAfter: true })
md.use(require("markdown-it-emoji/light"))
let cwd = `${process.cwd()}/views/markdown`
let dir = glob.sync(`${process.cwd()}/views/markdown/**/*.md`)
dir.forEach(path => {
app.get(path.split(".")[0].slice(cwd.length), (req, res) => {
let file = fs.readFileSync(path, "utf8")
let pageConfig = frontMatter(file)
let parsedFile = mustache.render(file, { config, attr: pageConfig.attributes })
let data = frontMatter(parsedFile)
let result = md.render(data.body)
res.render("index", {
data: result,
darkmode: config.info.darkmode,
title: data.attributes.title,
attr: data.attributes
})
})
})
app.get("/", (req, res) => {
let file = fs.readFileSync(`README.raw.md`, {encoding: "utf8"})
let pageConfig = frontMatter(file)
let parsedFile = mustache.render(file, { config, attr: pageConfig.attributes })
let data = frontMatter(parsedFile)
let result = md.render(data.body)
res.render("index", {
title: `${data.attributes.title} - ${config.info.sitename} `,
data: result,
darkmode: config.info.darkmode,
attr: data.attributes
})
})
app.listen(process.env.PORT || 3000, () => {
console.log(`Running server on port ${process.env.PORT || 3000}`)
})