Skip to content

Commit 5728f72

Browse files
committed
fix encoding for special characters in folder names
1 parent a75d98e commit 5728f72

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

md/folder-tests/a very long folder with spaces oh my god i cannot believe how long it is/Untitled.md

Whitespace-only changes.

md/folder-tests/folder with spaces/Untitled.md

Whitespace-only changes.

md/folder-tests/special chars $'&`´!?/Untitled.md

Whitespace-only changes.

obsidian.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ async function markPathForSelectedPage(req, files) {
13461346
};
13471347
r.path.push(f);
13481348
}
1349-
// console.log("markPathForSelectedPage", r);
1349+
console.log("markPathForSelectedPage", r);
13501350
return r;
13511351
}
13521352

@@ -1382,7 +1382,7 @@ async function getDirectoryListingInternal(path, req, files, folders) {
13821382
const folder = file.folderArray[j];
13831383
html += insertDirFolder(folder, j);
13841384
if (lookupAndMarkPath(folder, currentLevel, path.path)) {
1385-
openNavTreeScript += `toggleDirList('ff-${folder}-${j}');\n`;
1385+
openNavTreeScript += `toggleDirList('${getCssHashKey(folder, j)}');\n`;
13861386
currentLevel++;
13871387
}
13881388
}
@@ -1413,19 +1413,33 @@ async function getDirectoryListingInternal(path, req, files, folders) {
14131413
return html;
14141414
}
14151415

1416+
function hash32(str) {
1417+
let h = 2166136261;
1418+
for (let i = 0; i < str.length; i++) {
1419+
h ^= str.charCodeAt(i);
1420+
h = Math.imul(h, 16777619);
1421+
}
1422+
return (h >>> 0).toString(36);
1423+
}
1424+
1425+
function getCssHashKey(folder, j) {
1426+
return `ff-${hash32(folder)}-${j}`;
1427+
}
1428+
14161429
function insertDirFolder(folder, j) {
14171430
let r = "";
1431+
const key = getCssHashKey(folder, j);
14181432
r += `<div class="folder" style="margin: 0px; padding: 0px;">`;
1419-
r += `<div class="folder-name row" onclick="toggleDirList('ff-${folder}-${j}')" style="cursor: pointer;">${indentStringFor(
1433+
r += `<div class="folder-name row" onclick="toggleDirList('${key}')" style="cursor: pointer;">${indentStringFor(
14201434
j
1421-
)}${folder}<div class="folder-files ff-${folder}-${j}-chevron" style="
1435+
)}${folder}<div class="folder-files ${key}-chevron" style="
14221436
position: inline-block;
14231437
top: 1px;
14241438
padding: 0px;
14251439
margin: 0px;
14261440
transition: transform 0.3s ease;
14271441
">${lucideIcon("ChevronRight", null, "12px")}</div></div>`;
1428-
r += `<div class="folder-files ff-${folder}-${j}" style="
1442+
r += `<div class="folder-files ${key}" style="
14291443
opacity: 1;
14301444
padding-top: 0px;
14311445
max-height: 0;
@@ -1435,16 +1449,24 @@ function insertDirFolder(folder, j) {
14351449
return r;
14361450
}
14371451

1452+
function encodePathPreserveSlashes(path) {
1453+
return path
1454+
.split("/")
1455+
.map(seg => encodeURIComponent(seg))
1456+
.join("/");
1457+
}
1458+
14381459
function insertDirLink(file, req, indent, i, files) {
14391460
let r = "";
14401461
let correctedPath = decodeURIComponent(req.path);
14411462
if (correctedPath.startsWith("/md/")) {
14421463
correctedPath = correctedPath.slice(4);
14431464
}
14441465
let highlight = "";
1466+
14451467
if (file.path === correctedPath) highlight = "highlight";
14461468
r += `<a href="/${
1447-
dirPrefix + file.path
1469+
encodePathPreserveSlashes(dirPrefix + file.path)
14481470
}" class="${highlight}">${indentStringFor(
14491471
file.lastFolder === "" ? 0 : indent
14501472
)}${file.fileNameWithoutExtension}</a>`;

0 commit comments

Comments
 (0)