-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
320 lines (293 loc) · 9.62 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import fileNameExtractor from "./middlewares/extract-filename-middleware.js";
import { initKeycloak, checkAuthenticated, refreshAccessToken, getUserAttributes, setUserAttribute } from "./middlewares/keycloak-middleware.js";
import express from "express";
import { Marked } from "marked";
import markedShiki from "marked-shiki";
import { bundledLanguages, getHighlighter } from "shiki";
import createDOMPurify from "dompurify";
import { JSDOM } from "jsdom";
import {
transformerNotationDiff,
transformerNotationHighlight,
transformerNotationWordHighlight,
transformerNotationFocus,
transformerNotationErrorLevel,
transformerMetaHighlight,
transformerMetaWordHighlight,
} from "@shikijs/transformers";
const lightTheme = "material-theme-lighter";
const darkTheme = "one-dark-pro";
const highlighter = await getHighlighter({
langs: Object.keys(bundledLanguages),
themes: [darkTheme, lightTheme]
});
// Set options
const markedLight = new Marked();
markedLight.use({
async: true,
breaks: true,
gfm: true,
pedantic: false,
});
markedLight.use(
markedShiki({
highlight(code, lang, props) {
return highlighter.codeToHtml(code, {
lang,
theme: lightTheme,
meta: { __raw: props.join(" ") }, // required by `transformerMeta*`
transformers: [
transformerNotationDiff(),
transformerNotationHighlight(),
transformerNotationWordHighlight(),
transformerNotationFocus(),
transformerNotationErrorLevel(),
transformerMetaHighlight(),
transformerMetaWordHighlight(),
],
});
},
})
);
const markedDark = new Marked();
markedDark.use({
async: true,
breaks: true,
gfm: true,
pedantic: false,
});
markedDark.use(
markedShiki({
highlight(code, lang, props) {
return highlighter.codeToHtml(code, {
lang,
theme: darkTheme,
meta: { __raw: props.join(" ") }, // required by `transformerMeta*`
transformers: [
transformerNotationDiff(),
transformerNotationHighlight(),
transformerNotationWordHighlight(),
transformerNotationFocus(),
transformerNotationErrorLevel(),
transformerMetaHighlight(),
transformerMetaWordHighlight(),
],
});
},
})
);
import axios from "axios";
import { config } from "dotenv";
import path from "node:path";
import fs from "fs";
const __dirname = import.meta.dirname;
config();
const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window);
const app = express();
app.set("trust proxy", true);
import { scanFiles, scanFonts, preParse, manipulateHtml, wrapInPage, wrapInReveal, splitForReveal, parseFirstLineForPermissions, wrapAsDocument } from "./obsidian.js";
import { hasSomeRoles } from "./utils.js";
async function sanitizeAndParseMarkdown(data, req) {
try {
await refreshAccessToken(req);
let d = data.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, "");
// Look if the first line contains a permission directive
// First, get the first line of d
const firstLine = d.split("\n")[0];
const permissions = parseFirstLineForPermissions(firstLine);
if (permissions !== null) {
if (!await hasSomeRoles(req, permissions, true)) {
throw new Error("You do not have the required permissions to view this content.");
}
// Strip the first line since it held the permissions
d = d.split("\n").slice(1).join("\n");
}
d = await preParse(d, req);
let dm = 0;
if (req.user.accessTokenDecoded.config) {
const a = JSON.parse(req.user.accessTokenDecoded.config);
dm = a.dm;
}
const marked = dm == 1 ? markedDark : markedLight;
let html = await marked.parse(d);
html = manipulateHtml(html, req);
return DOMPurify.sanitize(html);
} catch (error) {
console.error(`Error parsing markdown: ${error}`);
return `Error parsing markdown: ${error}`;
}
}
async function mdGetToHtml(url, req) {
let headers = {};
if (req.headers.cookie) {
headers = {
cookie: req.headers.cookie,
};
}
const response = await axios.get(url, {
responseType: "document",
headers: headers,
});
return sanitizeAndParseMarkdown(response.data, req);
}
function getStartPage() {
let url = process.env.NEXT_PUBLIC_START_PAGE;
if (url === undefined || url === null || url === "") {
url = "/test-md-file.md";
}
return url;
}
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
initKeycloak(app).then(() => {
// Protect all routes and serve them statically after authentication.
// Order matters when dealing with middleware!
// For example the next() method will just pass the request on to the next middleware in line.
app.use(checkAuthenticated);
app.use(checkAuthenticated, (req, res, next) => {
if (req.url === "/" || req.url === "") {
let startPage = getStartPage();
getUserAttributes(req).then((attributes) => {
if (attributes && attributes.config) {
const a = JSON.parse(attributes.config);
if (a.sl == 1 && attributes.lastVisitedUrl) {
startPage = attributes.lastVisitedUrl;
}
}
res.redirect(startPage);
});
} else {
next();
}
});
app.use(fileNameExtractor);
app.use(checkAuthenticated, (req, res, next) => {
let filePath = path.join(__dirname, decodeURIComponent(req.path));
if (path.extname(filePath) === ".md") {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}`, err);
res.redirect(getStartPage());
return;
}
const reveal = req.query.reveal;
if (reveal) {
sanitizeAndParseMarkdown(data, req).then((html) => {
const h = splitForReveal(html);
wrapInReveal(h, req).then((r) => {
res.send(r);
});
});
} else {
const doc = req.query.document;
if (doc) {
sanitizeAndParseMarkdown(data, req).then((html) => {
setUserAttribute(req, "lastVisitedUrl", req.originalUrl);
wrapAsDocument(html, req).then((r) => {
res.send(r);
});
});
} else {
sanitizeAndParseMarkdown(data, req).then((html) => {
setUserAttribute(req, "lastVisitedUrl", req.originalUrl);
wrapInPage(html, getStartPage(), req).then((r) => {
res.send(r);
});
});
}
}
});
} else {
next();
}
});
app.use(checkAuthenticated, express.static(path.join(__dirname, "/")));
// Convert markdown to HTML using marked.
app.get("/convert", checkAuthenticated, (req, res) => {
const fileUrl = req.query.url;
if (!fileUrl) {
res.send(
"You have to provide a file-url to the markdown file by specifying `?url=<your-url-encoded-url>` in order to convert the markdown to HTML."
);
return;
}
mdGetToHtml(fileUrl, req).then((html) => {
wrapInPage(html, getStartPage(), req).then((r) => {
res.send(r);
});
})
.catch((error) => {
res.send(`Error: ${error}`);
});
});
// Post the request as a JSON file following this format:
// {
// "url": "https://raw.githubusercontent.com/..."
// }
// The URL should point to a markdown file.
// Or you may also send the markdown content directly using this format:
// {
// "content": "## Hello World"
// }
// The server will then convert the markdown to HTML and send it back as a JSON response using this format:
// {
// "html": "<html>...</html>"
// }
// The HTML will be sanitized using DOMPurify and will not contain any html, head or body tags.
app.post("/convert", checkAuthenticated, (req, res) => {
if (typeof req.body == "undefined" || req.body == null) {
res.json({
error:
"No data found in body. You have to specify 'url' or 'content' in your body in order to convert the markdown to HTML.",
});
} else {
if (req.body.url !== undefined && req.body.url !== null) {
mdGetToHtml(req.body.url, req)
.then((html) => {
res.json({ html: html });
})
.catch((error) => {
res.json({ error: error });
});
} else {
const c = req.body.content;
if (c !== undefined && c !== null) {
sanitizeAndParseMarkdown(c, req).then((html) => {
res.json({ html: html });
});
}
}
}
});
app.get("/userattributes", checkAuthenticated, (req, res) => {
// send the user profile as response
refreshAccessToken(req).then(() => {
res.json(req.user);
});
});
app.post("/userattributes", checkAuthenticated, (req, res) => {
if (typeof req.body == "undefined" || req.body == null) {
res.json({
error:
"No data found in body. You have to specify 'attribute' and 'value' in your body in order to save an client-attribute in your keycloak instance.",
});
} else {
setUserAttribute(req, 'config', JSON.stringify(req.body)).then((result) => {
if (result) {
res.json({ success: true });
} else {
res.json({ success: false });
}
});
}
});
const basePath = process.env.NEXT_PUBLIC_IS_APP_FOLDER ? '/app/' : '.';
scanFiles("md/", path.join(basePath, "md")).then(() => {
scanFonts(path.join(basePath, "assets")).then(() => {
app.listen(process.env.NEXT_PUBLIC_PORT, "0.0.0.0");
});
});
// If file is not found, redirect to the start page.
app.use((_, res) => res.redirect(getStartPage()));
});