-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (63 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
74 lines (63 loc) · 2.05 KB
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
72
73
74
"use strict";
// Dependencies
const compression = require("compression")
const { parse } = require("smol-toml")
const express = require("express")
const helmet = require("helmet")
const yaml = require("js-yaml")
const path = require("path")
const fs = require("fs")
// Variables
const config = parse(fs.readFileSync(path.join(__dirname, "./config.toml"), "utf8"))
const web = express()
const port = config.web.port
// Functions
const getKnowledge = (dir) => {
// Variables
const list = fs.readdirSync(dir)
var results = []
// Core
list.forEach((file) => {
const fullPath = path.join(dir, file)
const stat = fs.statSync(fullPath)
if (stat && stat.isDirectory()) {
results = results.concat(getKnowledge(fullPath))
} else if (file.endsWith(".md")) {
try {
const raw = fs.readFileSync(fullPath, 'utf8')
const match = raw.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/)
var doc = {}
var content = raw
if (match) {
doc = yaml.load(match[1]) || {}
content = match[2]
}
results.push({
path: path.relative(path.join(__dirname, "knowledge"), fullPath).replace(/\\/g, "/").replace(".md", ""),
...doc,
content: content.trim()
})
} catch{
console.error("Failed to parse Markdown:", fullPath)
}
}
})
return results
}
// Configurations
//* Express
web.use(compression({ level: 1 }))
web.use(helmet({ contentSecurityPolicy: false }))
// Main
web.use(express.static(path.join(__dirname, "public")))
web.get("/api/knowledge", (_, res) => {
// Core
try {
const concepts = getKnowledge(path.join(__dirname, "knowledge"))
res.json(concepts)
} catch (e) {
res.json({ error: e.message })
}
});
web.use("/{*any}", (_, res) => res.redirect("/"))
web.listen(port, () => console.log(`IPKA is running on port ${port}`))