Skip to content

Commit 9b00c9f

Browse files
committed
fix content parsing
better code blocks
1 parent 6e0f38a commit 9b00c9f

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

css/markdown.css

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
2-
3-
4-
5-
6-
7-
8-
9-
101
#markdown-content {
112
margin: auto;
123
min-width: 830.67px;
@@ -22,11 +13,6 @@
2213
background-color: #c3c3c3;
2314
}
2415

25-
#markdown-content .line,
26-
#markdown-content .line * {
27-
white-space: pre-line;
28-
}
29-
3016
#markdown-content table {
3117
border-collapse: collapse;
3218
margin: 1px;
@@ -53,4 +39,7 @@
5339
.shiki {
5440
word-wrap: break-word;
5541
word-break: break-all;
42+
line-height: 1.3em;
43+
font-size: 0.9em;
44+
font-family: Fira-Code, monospace;
5645
}

obsidian.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export async function scanFonts(dir, root = dir) {
186186
filePath.startsWith(slides)
187187
)
188188
continue;
189-
scanFonts(filePath, root);
189+
scanFonts(filePath, root);
190190
} else {
191191
// All other files.
192192
const fileName = path.basename(file);
@@ -374,24 +374,30 @@ export async function preParse(md, req) {
374374
async function removeForbiddenContent(md, req) {
375375
const regex = /^[ \t]*@@@(.*?)\n([\s\S]*?)@@@/gms;
376376
const matches = Array.from(md.matchAll(regex));
377-
const replacements = await Promise.all(matches.map(([match, perms, content]) => {
378-
const permissions = perms.split(",").map((p) => p.trim().toLowerCase());
379-
return hasSomeRoles(req, permissions, true).then((r) => r ? content : "");
380-
}));
377+
const replacements = await Promise.all(
378+
matches.map(([match, perms, content]) => {
379+
const permissions = perms.split(",").map((p) => p.trim().toLowerCase());
380+
return hasSomeRoles(req, permissions, true).then((r) =>
381+
r ? content : ""
382+
);
383+
})
384+
);
381385
for (let i = 0; i < matches.length; i++) {
382386
md = md.replace(matches[i][0], replacements[i]);
383387
}
384388
return md;
385389
}
386390

387391
function preMarkCode(md) {
388-
const regex = /(^[ \t]*```[\s\S]*?```)/gms;
392+
let regex = /^[ \t]*```(.*?)\n([\s\S]*?)```/gms;
389393
let match;
390394
let r = md;
391395
codeList = [];
392396
while ((match = regex.exec(r)) !== null) {
393-
codeList.push(match[0]);
397+
const string = `\`\`\`${match[1].toLowerCase()}\n${match[2]}\`\`\``;
398+
codeList.push(string);
394399
r = r.replace(match[0], internalSubstitutions.code.string);
400+
regex = /^[ \t]*```(.*?)\n([\s\S]*?)```/gms;
395401
}
396402
return r;
397403
}
@@ -474,27 +480,26 @@ function preReplaceObsidianFileLinks(html, req) {
474480
}
475481

476482
export async function preReplacePlantUml(md, req) {
477-
const regex = /^\s*```+\s*([\w\s]+)$/gm;
483+
let regex = /^\s*```+\s*(plantuml)$/gim;
478484
let match;
479485
while ((match = regex.exec(md)) !== null) {
480-
if (match[1].toLowerCase().trim() === "plantuml") {
481-
// PlantUML
482-
const start = match.index + match[0].length;
483-
const end = md.indexOf("```", start);
484-
const plantuml = md.substring(start, end);
485-
let serverUrl = process.env.NEXT_PUBLIC_PLANTUML_URL;
486-
if (serverUrl === undefined || serverUrl === null || serverUrl === "") {
487-
serverUrl = `https://plantuml.unterrainer.info/plantuml`;
488-
}
486+
// PlantUML
487+
const start = match.index + match[0].length;
488+
const end = md.indexOf("```", start);
489+
const plantuml = md.substring(start, end);
490+
let serverUrl = process.env.NEXT_PUBLIC_PLANTUML_URL;
491+
if (serverUrl === undefined || serverUrl === null || serverUrl === "") {
492+
serverUrl = `https://plantuml.unterrainer.info/plantuml`;
493+
}
489494

490-
// Encode in UTF-8, compress using Deflate, and reencode in ASCII
491-
const compressed = pako.deflate(plantuml, { to: "string" });
492-
const encoded = toPlantUmlEncoding(compressed);
495+
// Encode in UTF-8, compress using Deflate, and reencode in ASCII
496+
const compressed = pako.deflate(plantuml, { to: "string" });
497+
const encoded = toPlantUmlEncoding(compressed);
493498

494-
const url = `${serverUrl}/svg/${encoded}`;
495-
const img = `![PlantUML](${url})`;
496-
md = md.substring(0, match.index) + img + md.substring(end + 3);
497-
}
499+
const url = `${serverUrl}/svg/${encoded}`;
500+
const img = `![PlantUML](${url})`;
501+
md = md.substring(0, match.index) + img + md.substring(end + 3);
502+
regex = /^\s*```+\s*(plantuml)$/gim;
498503
}
499504
return md;
500505
}
@@ -659,9 +664,9 @@ function replaceObsidianImageLinks(html, req) {
659664
}
660665
}
661666
const serverUrl = `${req.protocol}://${req.get("host")}`;
662-
return `<img alt="${fileName}" src="${serverUrl}/${dirPrefix + f}" style="${
663-
r ? `width: ${r.width}; height: ${r.height};` : ""
664-
}" />`;
667+
return `<img alt="${fileName}" src="${serverUrl}/${
668+
dirPrefix + f
669+
}" style="${r ? `width: ${r.width}; height: ${r.height};` : ""}" />`;
665670
} else {
666671
return match;
667672
}
@@ -762,11 +767,13 @@ function findFirstDifferentIndex(arr1, arr2) {
762767

763768
async function getDirectoryListing(req) {
764769
const allFiles = Object.values(mdFilesDirStructure);
765-
const files = await Promise.all(allFiles.map(async (f) => {
766-
const hasRole = await hasSomeRoles(req, f.permissions, true);
767-
return hasRole ? f : null;
768-
}));
769-
const filteredFiles = files.filter(f => f !== null);
770+
const files = await Promise.all(
771+
allFiles.map(async (f) => {
772+
const hasRole = await hasSomeRoles(req, f.permissions, true);
773+
return hasRole ? f : null;
774+
})
775+
);
776+
const filteredFiles = files.filter((f) => f !== null);
770777
let r = await getDirectoryListingInternal(req, filteredFiles, []);
771778
if (filteredFiles[filteredFiles.length - 1].folders.length > 0) {
772779
r += `</div></div>`;
@@ -804,11 +811,7 @@ async function getDirectoryListingInternal(req, files, folders) {
804811
const subfolderFiles = files.filter(
805812
(f, index) => f.folders.startsWith(folders) && index > i
806813
);
807-
html += await getDirectoryListingInternal(
808-
req,
809-
subfolderFiles,
810-
folders
811-
);
814+
html += await getDirectoryListingInternal(req, subfolderFiles, folders);
812815

813816
// Update the last processed file index
814817
lastProcessedFileIndex = i + subfolderFiles.length;
@@ -993,7 +996,11 @@ async function getTopBar(startPage, req) {
993996
)}</button>
994997
<div class="topbar-title nav-font">${
995998
req.path !== "/convert/"
996-
? decodeURIComponent(req.path.startsWith(`/${dirPrefix}`) ? req.path.slice(dirPrefix.length + 1) : req.path)
999+
? decodeURIComponent(
1000+
req.path.startsWith(`/${dirPrefix}`)
1001+
? req.path.slice(dirPrefix.length + 1)
1002+
: req.path
1003+
)
9971004
: decodeURIComponent(req.query.url)
9981005
}</div>
9991006
<div class="topbar-menu">
@@ -1106,7 +1113,7 @@ export async function wrapInReveal(reveal) {
11061113
}
11071114
pre.shiki code {
11081115
max-width: 100%;
1109-
max-height: 60vh !important;
1116+
max-height: 60vh !important;
11101117
}
11111118
11121119
.reveal {

0 commit comments

Comments
 (0)