-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsidian.js
1311 lines (1195 loc) · 40.7 KB
/
obsidian.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from "fs";
import readline from "readline";
import path from "path";
import pako from "pako";
import { v4 as uuidv4 } from "uuid";
import { JSDOM } from "jsdom";
import { hasSomeRoles } from "./utils.js";
import * as lucideIcons from "lucide-static";
const internalTags = {
callout: {
string: {
start: "<calloutstart/>",
end: "<calloutend/>",
},
regexp: {
start: "<calloutstart/>",
end: "<calloutend/>",
},
},
};
const internalSubstitutions = {
code: {
string: "@#@##@@--@code@--@@##@#@",
regexp: /@#@##@@--@code@--@@##@#@/gms,
},
};
let codeList = [];
export const callouts = {
note: {
icon: "Pencil",
color: "#086ddd",
},
abstract: {
icon: "ClipboardList",
color: "#00bfbc",
},
summary: { link: "abstract" },
tldr: { link: "abstract" },
info: {
icon: "Info",
color: "#086ddd",
},
todo: {
icon: "CheckCircle2",
color: "#086ddd",
},
tip: {
icon: "Flame",
color: "#00bfbc",
},
hint: { link: "tip" },
important: { link: "tip" },
success: {
icon: "Check",
color: "#08b94e",
},
done: { link: "success" },
check: { link: "success" },
question: {
icon: "HelpCircle",
color: "#ec7500",
},
help: { link: "question" },
faq: { link: "question" },
warning: {
icon: "AlertTriangle",
color: "#ec7500",
},
caution: { link: "warning" },
attention: { link: "warning" },
failure: {
icon: "X",
color: "#e93147",
},
fail: { link: "failure" },
missing: { link: "failure" },
danger: {
icon: "Zap",
color: "#e93147",
},
error: { link: "danger" },
bug: {
icon: "Bug",
color: "#e93147",
},
example: {
icon: "List",
color: "#7852ee",
},
quote: {
icon: "Quote",
color: "#9e9e9e",
},
cite: { link: "quote" },
};
// Datastructures to store the file paths.
// Key is the file-name without path (and no extension for the md-file-list).
// Value is an array of the relative paths to all the files with that name anywhere in the file-system.
// If there is only one file with that name, the array will contain only one element.
export let dirPrefix = "";
export const mdFilesMap = {};
export const filesMap = {};
export const mdFilesDir = {};
export const mdFilesDirOnHdd = {};
export let mdFilesDirStructure = {};
export const mainFonts = {};
export const mainFontsArray = [];
export const navFonts = {};
export const navFontsArray = [];
export let contentMap = {};
export function parseFirstLineForPermissions(line) {
const match = line.match(/^\s*@@@(.*)/);
let r = null;
if (match) {
r = [];
const s = match[1];
s.split(",").forEach((p) => {
r.push(p.trim().toLowerCase());
});
}
return r;
}
function getPermissionsFor(path) {
return new Promise((resolve, reject) => {
const fileStream = fs.createReadStream(path);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
let r;
const lineReader = (line) => {
r = parseFirstLineForPermissions(line);
if (r === null) {
r = [];
}
// Since we only want the first line, we remove the listener after the first iteration
rl.removeListener("line", lineReader);
};
rl.on("line", lineReader);
rl.on("close", () => {
resolve(r);
});
rl.on("error", (err) => {
reject(err);
});
});
}
function makeSafeForCSS(name) {
return name.replace(/[^a-z0-9]/g, function (s) {
var c = s.charCodeAt(0);
if (c == 32) return "-";
if (c >= 65 && c <= 90) return "_" + s.toLowerCase();
return "__" + ("000" + c.toString(16)).slice(-4);
});
}
export async function scanFonts(dir, root = dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
try {
if (dir && file) {
// Check if dir and file are not null
const filePath = path.join(dir, file);
const isDirectory = fs.statSync(filePath).isDirectory();
if (isDirectory) {
const nmods = process.env.NEXT_PUBLIC_IS_APP_FOLDER
? "/app/node_modules"
: "node_modules";
const slides = process.env.NEXT_PUBLIC_IS_APP_FOLDER
? "/app/slides"
: "slides";
if (
filePath.startsWith(".") ||
filePath.startsWith(nmods) ||
filePath.startsWith(slides)
)
continue;
scanFonts(filePath, root);
} else {
// All other files.
const fileName = path.basename(file);
const relativePath = path.relative(root, filePath);
const p = relativePath.replace(/\\/g, "/");
if (p.startsWith("main-fonts/")) {
const fontName = path.basename(file, ".ttf");
mainFonts[fontName] = "assets/" + p;
mainFontsArray.push(fontName);
}
if (p.startsWith("nav-fonts/")) {
const fontName = path.basename(file, ".ttf");
navFonts[fontName] = "assets/" + p;
navFontsArray.push(fontName);
}
}
}
} catch (err) {
console.error(`Error reading file while scanning fonts ${file}`, err);
}
}
}
/**
* If it's the root dir, dirPrefix should be an empty string.
*/
export async function scanFiles(prefix, dir, root = dir) {
dirPrefix = prefix;
scanFilesInternal(dir, root);
let mdFiles = await Promise.all(
Object.keys(mdFilesDir).map(async (file) => {
const pwe = mdFilesDir[file];
const folders = pwe.split("/").slice(0, -1).join("/");
const folderArray = folders.split("/");
if (folderArray.length === 1 && folderArray[0] === "") {
folderArray.pop();
}
return {
[file]: {
path: file,
pathWithoutExt: pwe,
folders: folders,
folderArray: folderArray,
depth: folders === "" ? 0 : folders.split("/").length,
fileName: file.split("/").pop(),
fileNameWithoutExtension: pwe.split("/").pop().split(".")[0],
lastFolder: pwe.split("/").slice(-2, -1)[0] || "",
cssName: makeSafeForCSS(folders),
permissions: await getPermissionsFor(dirPrefix + file),
},
};
})
);
mdFiles = mdFiles.reduce((acc, file) => {
const key = Object.keys(file)[0];
acc[key] = file[key];
return acc;
}, {});
// Convert to array, sort, and convert back to object
mdFiles = Object.entries(mdFiles)
.sort(([keyA, valueA], [keyB, valueB]) => {
// Iterate over the folderArray
for (
let i = 0;
i < Math.min(valueA.folderArray.length, valueB.folderArray.length);
i++
) {
const comp = valueA.folderArray[i].localeCompare(
valueB.folderArray[i],
undefined,
{ sensitivity: "base" }
);
if (comp !== 0) {
// If a difference is found, return the comparison result
return comp;
}
}
// If all elements are equal, compare the lengths of the arrays
if (valueA.folderArray.length !== valueB.folderArray.length) {
return valueB.folderArray.length - valueA.folderArray.length;
}
// If the folderArrays are equal, compare the name
return valueA.fileName.localeCompare(valueB.fileName, undefined, {
sensitivity: "base",
});
})
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
mdFilesDirStructure = mdFiles;
}
function scanFilesInternal(dir, root = dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
try {
if (dir && file) {
// Check if dir and file are not null
const filePath = path.join(dir, file);
const isDirectory = fs.statSync(filePath).isDirectory();
if (isDirectory) {
const nmods = process.env.NEXT_PUBLIC_IS_APP_FOLDER
? "/app/node_modules"
: "node_modules";
const slides = process.env.NEXT_PUBLIC_IS_APP_FOLDER
? "/app/slides"
: "slides";
if (
filePath.startsWith(".") ||
filePath.startsWith(nmods) ||
filePath.startsWith(slides)
)
continue;
scanFilesInternal(filePath, root);
} else if (path.extname(file) === ".md") {
// MD files.
const fileNameWithoutExt = path.basename(file, ".md");
const relativePath = path.relative(root, filePath);
const p = relativePath.replace(/\\/g, "/");
// MD files.
const dirPathWithoutFileName = path.dirname(relativePath);
let p2 = dirPathWithoutFileName.replace(/\\/g, "/");
p2 = p2 + "/" + fileNameWithoutExt;
if (p2.startsWith("./")) {
p2 = p2.slice(2);
}
mdFilesDir[p] = p2;
if (mdFilesMap[fileNameWithoutExt]) {
mdFilesMap[fileNameWithoutExt].push(p);
} else {
mdFilesMap[fileNameWithoutExt] = [p];
}
} else {
// All other files.
const fileName = path.basename(file);
const relativePath = path.relative(root, filePath);
const p = relativePath.replace(/\\/g, "/");
if (filesMap[fileName]) {
filesMap[fileName].push(p);
} else {
filesMap[fileName] = [p];
}
if (p.startsWith("assets/main-fonts/")) {
const fontName = path.basename(file, ".ttf");
mainFonts[fontName] = p;
mainFontsArray.push(fontName);
}
if (p.startsWith("assets/nav-fonts/")) {
const fontName = path.basename(file, ".ttf");
navFonts[fontName] = p;
navFontsArray.push(fontName);
}
}
}
} catch (err) {
console.error(`Error reading file ${file}`, err);
}
}
}
export function lucideIcon(name, color, size = 18) {
let svg = lucideIcons[name];
svg = svg.replace('width="24"', `width="${size}"`);
svg = svg.replace('height="24"', `height="${size}"`);
return `<span style="color: ${color};">${svg}</span>`;
}
export async function preParse(md, req) {
let r = md;
r = await preReplacePlantUml(r, req);
r = preMarkCode(r);
r = preReplaceObsidianFileLinks(r, req);
r = await removeForbiddenContent(r, req);
r = preMarkCallouts(r);
r = unmarkCode(r);
return r;
}
async function removeForbiddenContent(md, req) {
const regex = /^[ \t]*@@@(.*?)\n([\s\S]*?)@@@/gms;
const matches = Array.from(md.matchAll(regex));
const replacements = await Promise.all(
matches.map(([match, perms, content]) => {
const permissions = perms.split(",").map((p) => p.trim().toLowerCase());
return hasSomeRoles(req, permissions, true).then((r) =>
r ? content : ""
);
})
);
for (let i = 0; i < matches.length; i++) {
md = md.replace(matches[i][0], replacements[i]);
}
return md;
}
function preMarkCode(md) {
let regex = /^[ \t]*```(.*?)\n([\s\S]*?)```/gms;
let match;
let r = md;
codeList = [];
while ((match = regex.exec(r)) !== null) {
const string = `\`\`\`${match[1].toLowerCase()}\n${match[2]}\`\`\``;
codeList.push(string);
r = r.replace(match[0], internalSubstitutions.code.string);
regex = /^[ \t]*```(.*?)\n([\s\S]*?)```/gms;
}
return r;
}
function unmarkCode(md) {
let c = 0;
return md.replace(internalSubstitutions.code.regexp, () => {
return codeList[c++];
});
}
function preMarkCallouts(md) {
const regex = />[\s]*\[!(.*?)\].*?\n/gs;
let match;
let markedMd = md;
let offset = 0;
while ((match = regex.exec(md)) !== null) {
const { trimmedLines, originalLength } = getFollowingQuotedLines(
md,
match.index + match[0].length
);
const markedLines = `${internalTags.callout.string.start}\n\n${match[0]
.trim()
.substring(1)
.trim()}\n${trimmedLines}\n\n${internalTags.callout.string.end}\n\n`;
const originalSectionLength = match[0].length + originalLength; // originalLength already includes the newline
markedMd =
markedMd.slice(0, match.index + offset) +
markedLines +
markedMd.slice(match.index + originalSectionLength + offset);
offset += markedLines.length - originalSectionLength;
}
return markedMd;
}
function getFollowingQuotedLines(md, index) {
const lines = md.slice(index).split("\n");
let i = 0;
let originalLength = 0;
while (i < lines.length && lines[i].trim().startsWith(">")) {
originalLength += lines[i].length + 1; // +1 for the newline
lines[i] = lines[i].substring(lines[i].indexOf(">") + 1); // remove leading ">"
i++;
}
return { trimmedLines: lines.slice(0, i).join("\n"), originalLength };
}
function preReplaceObsidianFileLinks(html, req) {
const regex = /(?<!\!)\[\[([^\]\n]+)\]\]/g;
return html.replace(regex, (match) => {
let fileName = match.slice(2, -2);
let alt = null;
if (fileName && fileName.includes("|")) {
const parts = fileName.split("|");
fileName = parts[0].trim();
alt = parts[1].trim();
if (alt === "") {
alt = null;
}
}
const lastPartOfFileName = fileName.split("/").pop();
const filePath = mdFilesMap[lastPartOfFileName];
if (filePath) {
let f = filePath[0];
if (filePath.length > 1) {
f = filePath.find((path) => path === fileName + ".md");
if (!f) {
return match;
}
f = f.split(0, -3);
}
f = encodeURIComponent(f);
const serverUrl = `${req.protocol}://${req.get("host")}`;
return `[${alt ? alt : fileName}](${serverUrl}/${dirPrefix + f})`;
} else {
return match;
}
});
}
export async function preReplacePlantUml(md, req) {
let regex = /^\s*```+\s*(plantuml)$/gim;
let match;
while ((match = regex.exec(md)) !== null) {
// PlantUML
const start = match.index + match[0].length;
const end = md.indexOf("```", start);
const plantuml = md.substring(start, end);
let serverUrl = process.env.NEXT_PUBLIC_PLANTUML_URL;
if (serverUrl === undefined || serverUrl === null || serverUrl === "") {
serverUrl = `https://plantuml.unterrainer.info/plantuml`;
}
// Encode in UTF-8, compress using Deflate, and reencode in ASCII
const compressed = pako.deflate(plantuml, { to: "string" });
const encoded = toPlantUmlEncoding(compressed);
const url = `${serverUrl}/svg/${encoded}`;
const img = `![PlantUML](${url})`;
md = md.substring(0, match.index) + img + md.substring(end + 3);
regex = /^\s*```+\s*(plantuml)$/gim;
}
return md;
}
function toPlantUmlEncoding(buffer) {
const dictionary =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";
let result = "";
let current = 0;
let remaining = 0;
for (const byte of buffer) {
current = (current << 8) | byte;
remaining += 8;
while (remaining >= 6) {
remaining -= 6;
const index = (current >> remaining) & 0x3f;
result += dictionary[index];
}
}
if (remaining > 0) {
result += dictionary[(current << (6 - remaining)) & 0x3f];
}
return result;
}
export function manipulateHtml(html, req) {
let r = html;
r = replacePreMarkCallouts(r);
r = replaceObsidianImageLinks(r, req);
r = replaceObsidianImageAltResizeValues(r);
r = makeContentMap(r);
return r;
}
export function splitForReveal(html) {
const dom = new JSDOM(html);
const document = dom.window.document;
const sections = [];
let currentSection = [];
for (const node of Array.from(document.body.childNodes)) {
if (node.tagName === "H2") {
if (currentSection.length > 0) {
sections.push(splitSubSections(currentSection.join("")));
currentSection = [];
}
}
currentSection.push(node.outerHTML || node.textContent);
}
if (currentSection.length > 0) {
sections.push(splitSubSections(currentSection.join("")));
}
return sections.join("");
}
function splitSubSections(html) {
const dom = new JSDOM(html);
const document = dom.window.document;
const subSections = [];
let currentSubSection = [];
for (const node of Array.from(document.body.childNodes)) {
if (node.tagName === "H3") {
if (currentSubSection.length > 0) {
subSections.push(currentSubSection.join(""));
currentSubSection = [];
}
}
currentSubSection.push(node.outerHTML || node.textContent);
}
if (currentSubSection.length > 0) {
subSections.push(currentSubSection.join(""));
}
return `<section>${subSections
.map((subSection) => `<section>${subSection}</section>`)
.join("")}</section>`;
}
function makeContentMap(html) {
const dom = new JSDOM(html);
const document = dom.window.document;
const headers = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
contentMap = {};
contentMap["ROOT"] = { text: "TOP", indent: 1 };
headers.forEach((header) => {
const guid = uuidv4();
const indent = parseInt(header.tagName.slice(1));
contentMap[guid] = { text: header.textContent, indent: indent };
header.setAttribute("id", guid);
header.setAttribute("class", "docanchor");
});
return `<span id="ROOT" class="docanchor"></span>` + dom.serialize();
}
function replaceObsidianImageAltResizeValues(html) {
// One regex for alt first, then src, one for the other way around.
const regex1 = /<img[^>]+alt="(.*?)"[^>]+src="(.*?)"[^>]*>/gi;
const regex2 = /<img[^>]+src="(.*?)"[^>]+alt="(.*?)"[^>]*>/gi;
let r = html.replace(regex1, (match, alt, src) => {
return insertObsidianImageAltResizeValues(match, alt, src);
});
r = r.replace(regex2, (match, src, alt) => {
return insertObsidianImageAltResizeValues(match, alt, src);
});
return r;
}
function insertObsidianImageAltResizeValues(match, alt, src) {
if (alt && alt.includes("|")) {
const r = parseWidthHeight(alt);
return `<img alt="${r ? r.name : alt}" src="${src}" style="${
r ? `width: ${r.width}; height: ${r.height}` : ""
}" />`;
}
return match;
}
function parseWidthHeight(input) {
const parts = input.split("|");
const name = parts[0];
let width = "100%";
let height = "auto";
if (parts.length > 1) {
// something|100x200 or something|100
const s = parts[1].split("x");
if (s.length === 2) {
// something|100x200
width = isNaN(s[0]) ? s[0] : s[0] + "px";
height = isNaN(s[1]) ? s[1] : s[1] + "px";
} else {
// something|100
width = isNaN(s[0]) ? s[0] : s[0] + "px";
}
return { name, width, height };
}
return null;
}
function replaceObsidianImageLinks(html, req) {
const regex = /!\[\[(.*?)\]\]/g;
return html.replace(regex, (match) => {
let fileName = match.slice(3, -2);
const r = parseWidthHeight(fileName);
if (r) {
fileName = r.name;
}
const lastPartOfFileName = fileName.split("/").pop();
const filePath = filesMap[lastPartOfFileName];
if (filePath) {
let f = filePath[0];
if (filePath.length > 1) {
f = filePath.find((path) => path === fileName);
if (!f) {
return match;
}
}
const serverUrl = `${req.protocol}://${req.get("host")}`;
return `<img alt="${fileName}" src="${serverUrl}/${
dirPrefix + f
}" style="${r ? `width: ${r.width}; height: ${r.height};` : ""}" />`;
} else {
return match;
}
});
}
function replacePreMarkCallouts(html) {
const regex = new RegExp(
`${internalTags.callout.regexp.start}([\\s\\S]*?)${internalTags.callout.regexp.end}`,
"gi"
);
return html.replace(regex, (match, s) => {
const lines = s.split(/\s*(\n|<br\s*\/?>)\s*/);
const ls = lines.filter((l) => l.trim().length > 0);
// First line hold the info about the callout type, collapsable, and caption.
let n = ls[0];
if (n.startsWith("<p>")) {
n = n.slice(3);
}
if (n.endsWith("</p>")) {
n = n.slice(0, -4);
}
if (ls[ls.length - 1].endsWith("</p>")) {
ls[ls.length - 1] = ls[ls.length - 1].slice(0, -4);
}
const calloutRegex = /\[!(.*?)\](.*)/;
const [, type, caption] = n.match(calloutRegex) || [];
const content = ls.slice(1).join("\n");
let callout = callouts[type.toLowerCase()];
if (!callout) return match;
let t = type;
while (callout.link) {
t = callout.link;
callout = callouts[t];
}
t = t.charAt(0).toUpperCase() + t.slice(1);
if (caption.length > 0) {
t = caption;
}
let collapsable = false;
if (t.startsWith("-")) {
t = t.slice(1).trim();
collapsable = true;
}
return `<div class="obsidian-callout${
collapsable ? " obsidian-collapsable" : ""
}" style="
background-color: ${callout.color + "1a"};
">
<div class="row obsidian-collapsable-trigger"
style="cursor: ${collapsable ? "pointer" : "default"}"
>
<div class="col obsidian-callout-icon">${lucideIcon(
callout.icon,
callout.color
)}</div>
<div class="col obsidian-callout-title" style="color: ${
callout.color
};">${t}</div>
${
collapsable
? `<div class="col obsidian-callout-chevron" style="color: ${
callout.color
};">${lucideIcon("ChevronRight", callout.color)}</div>`
: ""
}
</div>
<div class="${
collapsable ? "collapsable " : ""
}obsidian-callout-content" style="${
content ? "" : "display: none; overflow: hidden;"
}">${content || ""}</div>
</div>`;
});
}
function indentStringFor(indent) {
let r = "";
for (let j = 0; j < indent; j++) {
r += " ";
}
return r;
}
function findFirstDifferentIndex(arr1, arr2) {
for (let i = 0; i < Math.min(arr1.length, arr2.length); i++) {
if (arr1[i] !== arr2[i]) {
return i;
}
}
// If no difference was found but the arrays are of different lengths
if (arr1.length !== arr2.length) {
return Math.min(arr1.length, arr2.length);
}
// If no difference was found and the arrays are of the same length
return -1;
}
async function getDirectoryListing(req) {
const allFiles = Object.values(mdFilesDirStructure);
const files = await Promise.all(
allFiles.map(async (f) => {
const hasRole = await hasSomeRoles(req, f.permissions, true);
return hasRole ? f : null;
})
);
const filteredFiles = files.filter((f) => f !== null);
let r = await getDirectoryListingInternal(req, filteredFiles, []);
if (filteredFiles[filteredFiles.length - 1].folders.length > 0) {
r += `</div></div>`;
}
return r;
}
async function getDirectoryListingInternal(req, files, folders) {
let html = "";
let lastProcessedFileIndex = -1;
let lastFile = null;
for (let i = 0; i < files.length; i++) {
const file = files[i];
lastFile = file;
const diffIndex = findFirstDifferentIndex(file.folderArray, folders);
if (diffIndex > -1) {
// New folder section
if (folders.length > diffIndex) {
// Close not needed folder sections
for (let j = folders.length - 1; j >= diffIndex; j--) {
html += `</div></div>`;
}
}
// Open as many folders as needed to reach the new folder
for (let j = diffIndex; j < file.folderArray.length; j++) {
const folder = file.folderArray[j];
html += insertDirFolder(folder, j);
}
folders = file.folderArray;
// Add the current file to the new folder section
html += insertDirLink(file, req, file.folderArray.length, i, files);
// Recursive call for subfolder
const subfolderFiles = files.filter(
(f, index) => f.folders.startsWith(folders) && index > i
);
html += await getDirectoryListingInternal(req, subfolderFiles, folders);
// Update the last processed file index
lastProcessedFileIndex = i + subfolderFiles.length;
i = lastProcessedFileIndex; // Skip the files that have been processed
} else if (i > lastProcessedFileIndex) {
// Add file to current folder section
html += insertDirLink(file, req, file.folderArray.length, i, files);
}
}
return html;
}
function insertDirFolder(folder, j) {
let r = "";
r += `<div class="folder" style="margin: 0px; padding: 0px;">`;
r += `<div class="folder-name row" onclick="toggleDirList('ff-${folder}-${j}')" style="cursor: pointer;">${indentStringFor(
j
)}${folder}<div class="folder-files ff-${folder}-${j}-chevron" style="
position: inline-block;
top: 1px;
padding: 0px;
margin: 0px;
transition: transform 0.3s ease;
">${lucideIcon("ChevronRight", null, "12px")}</div></div>`;
r += `<div class="folder-files ff-${folder}-${j}" style="
opacity: 1;
padding-top: 0px;
max-height: 0;
overflow: hidden;
transition: padding 0.3s ease, max-height 0.3s ease, opacity 0.3s ease, visibility 0.3s step-start;
">`;
return r;
}
function insertDirLink(file, req, indent, i, files) {
let r = "";
r += `<a href="/${dirPrefix + file.path}" class="${
"/" + file.path === decodeURIComponent(req.path) ? "highlight" : ""
}">${indentStringFor(file.lastFolder === "" ? 0 : indent)}${
file.fileNameWithoutExtension
}</a>`;
// Only add <br> if it isn't the last file in the folder
if (i < files.length - 1 && files[i + 1].folders === file.folders) {
r += "<br>";
}
return r;
}
function getContentListing() {
const list = Object.keys(contentMap).map((guid) => {
const o = contentMap[guid];
let indentation = " ".repeat((o.indent - 1) * 2);
return `${indentation}<a href="#${guid}">${o.text}</a>`;
});
return list.join("<br>");
}
function getFontImports() {
let result = "";
for (const font in navFonts) {
const f = navFonts[font];
result += `
@font-face {
font-family: "nav ${font}";
src: url(/${f.replaceAll(" ", "\\ ")}) format("truetype");
}
`;
}
for (const font in mainFonts) {
const f = mainFonts[font];
result += `
@font-face {
font-family: "main ${font}";
src: url(/${f.replaceAll(" ", "\\ ")}) format("truetype");
}
`;
}
return result;
}
function getNavFontsSelection() {
let result = "";
let c = 0;
for (const font of navFontsArray) {
result += `<option value="${c}">${font}</option>
`;
c++;
}
return result;
}
function getMainFontsSelection() {
let result = "";
let c = 0;
for (const font of mainFontsArray) {
result += `<option value="${c}">${font}</option>
`;
c++;
}
return result;
}
async function getTopdownMenu(req) {
return `
<div style="padding: 0px; margin: 0px;">
<button class="sl-button-accent" style="margin-left: 0px;" onclick="window.location.href='/logout'">${lucideIcon(
"LogOut"
)}<span style="margin-left: 6px; margin-right: 6px; position: relative; top: -2px;">logout</span></button>
<button class="sl-button" onclick="toggleLightDark()">${lucideIcon(
"SunMoon"
)}<span style="margin-left: 6px; margin-right: 6px; position: relative; top: -2px;">light/dark</span></button>
<div style="padding: 0px; margin: 0px; margin-top: 10px; width: 95%; text-align: left; display: flex;">
<span style="top: 1px; position: relative; cursor: pointer;" onclick="navFontChange(4)">${lucideIcon(
"PanelsTopLeft"
)}</span>
<select class="sl-select" style="width: auto; flex-grow: 1;" id="navFontSelect" onchange="navFontChange(this.value)">
${getNavFontsSelection()}
</select>
</div>
<div style="padding: 0px; margin: 0px; margin-top: 2px; width: 95%; padding-right: 100px; text-align: left; display: flex;">
<span style="top: 1px; position: relative; cursor: pointer;" onclick="mainFontChange(0)">${lucideIcon(
"SquareMenu"
)}</span>
<select class="sl-select" style="width: auto; flex-grow: 1;" id="mainFontSelect" onchange="mainFontChange(this.value)">
${getMainFontsSelection()}
</select>
</div>
</div>
<div style="padding: 0px; margin: 0px; margin-left: 0px; margin-top: 10px; margin-bottom: 20px;">
<div style="padding: 0px; margin: 0px; margin-bottom: 1px; margin-top: 10px; margin-left: 4px;">
<button class="sl-button button-group-left" onclick="fontSmaller()">${lucideIcon(
"AArrowDown"
)}</button>
<button class="sl-button-accent button-group-middle" onclick="fontStandard()">${lucideIcon(
"ALargeSmall"
)}</button>
<button class="sl-button button-group-right" onclick="fontBigger()">${lucideIcon(
"AArrowUp"
)}</button>
</div>
<div style="padding: 0px; margin: 0px; margin-top: 10px; margin-left: 4px;">
<button class="sl-button button-group-left" onclick="spacingSmaller()">${lucideIcon(
"Rows4"
)}</button>
<button class="sl-button-accent button-group-middle" onclick="spacingStandard()">${lucideIcon(
"Rows3"
)}</button>
<button class="sl-button button-group-right" onclick="spacingBigger()">${lucideIcon(
"Rows2"
)}</button>
</div>
<div style="padding: 0px; margin: 0px; margin-top: 10px; cursor: pointer;" onclick="toggleStartWithLastPage()">
<input type="checkbox" id="startWithLastPageCheckbox">remember last visited page</input>
</div>
<div class="flipswitch menu" style="display: inline-block; top: 16px; margin-left: 3px;">
<input checked="" onchange="toggleViewAnswer()" id="answersFs" class="flipswitch-cb" name="flipswitch" type="checkbox">
<label for="answersFs" class="flipswitch-label">
<div class="flipswitch-inner answers menu"></div>
<div class="flipswitch-switch answers menu"></div>
</label>
</div>
${
(await hasSomeRoles(req, ["teacher"]))
? `<div class="flipswitch menu" style="display: inline-block; top: 16px; margin-top: 12px; margin-left: 3px;">
<input checked="" onchange="toggleViewExam()" id="examFs" class="flipswitch-cb" name="flipswitch" type="checkbox">
<label for="examFs" class="flipswitch-label">
<div class="flipswitch-inner exam menu"></div>
<div class="flipswitch-switch exam menu"></div>
</label>
</div>`
: ""
}
</div>
`;
}
async function getTopBar(startPage, req) {
return `
<button class="sl-button topbar-burger" style="height: 32px; margin: 6px; margin-left: 0px;" onclick="toggleSidebar()">${lucideIcon(
"Menu"
)}</button>
<div class="topbar-title nav-font">${
req.path !== "/convert/"
? decodeURIComponent(
req.path.startsWith(`/${dirPrefix}`)