-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (42 loc) · 1.2 KB
/
server.js
File metadata and controls
54 lines (42 loc) · 1.2 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
const express = require("express");
const { createHighlighter } = require("shiki");
const fs = require("fs");
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.static("./test"));
const morGrammar = JSON.parse(fs.readFileSync("mor.tmLanguage.json", "utf8"));
let highlighter;
async function initHighlighter() {
highlighter = await createHighlighter({
themes: ["monokai", "github-dark", "github-light", "dracula"],
langs: [morGrammar],
});
console.log("Shiki highlighter initialized");
}
app.post("/highlight", async (req, res) => {
try {
const { code, theme } = req.body;
if (!highlighter) {
await initHighlighter();
}
const highlighted = highlighter.codeToHtml(code, {
lang: "mor",
theme: theme,
});
res.json({ highlighted });
} catch (error) {
console.error("Highlight error:", error);
res.status(500).json({ error: error.message });
}
});
app.get("/themes", (_, res) => {
res.json(["monokai", "github-dark", "github-light", "dracula"]);
});
async function startServer() {
await initHighlighter();
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
}
startServer();