Skip to content

Commit dce91bc

Browse files
committed
add open selected nav-element in nav-tree
fix highlighting
1 parent 20d1862 commit dce91bc

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

obsidian.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const internalSubstitutions = {
2626
},
2727
};
2828
let codeList = [];
29+
let openNavTreeScript = '';
2930

3031
export const callouts = {
3132
note: {
@@ -777,31 +778,62 @@ async function getDirectoryListing(req) {
777778
})
778779
);
779780
const filteredFiles = files.filter((f) => f !== null);
780-
await getSelectedPage(req, files);
781-
let r = await getDirectoryListingInternal(req, filteredFiles, []);
781+
const p = await markPathForSelectedPage(req, files);
782+
783+
openNavTreeScript = "<script lang=\"javascript\">\n"
784+
openNavTreeScript += `toggleDirList('sidebar-dirlist');\n`;
785+
let r = await getDirectoryListingInternal(p, req, filteredFiles, []);
786+
openNavTreeScript += "</script>";
787+
// console.log("openNavTreeScript", openNavTreeScript);
788+
782789
if (filteredFiles[filteredFiles.length - 1].folders.length > 0) {
783790
r += `</div></div>`;
784791
}
785792
return r;
786793
}
787794

788-
async function getSelectedPage(req, files) {
789-
// console.log("getSelectedPage", req);
790-
console.log("getSelectedPage", files);
795+
async function markPathForSelectedPage(req, files) {
796+
// console.log("getSelectedPage", req);
797+
// console.log("getSelectedPage", files);
791798
let path = req._parsedUrl.path;
792799
if(path.startsWith("/md/")) {
793800
path = path.slice(4);
794801
}
795802
while (path.startsWith("/")) {
796803
path = path.slice(1);
797804
}
798-
console.log('parsedOriginalUrl', path);
805+
const r = {
806+
path: [],
807+
file: null
808+
}
809+
const splitPath = path.split("/");
810+
r.file = splitPath.pop();
811+
for (let i = 0; i < splitPath.length; i++) {
812+
const f = {
813+
dir: splitPath[i],
814+
level: i
815+
}
816+
r.path.push(f);
817+
}
818+
// console.log("markPathForSelectedPage", r);
819+
return r;
799820
}
800821

801-
async function getDirectoryListingInternal(req, files, folders) {
822+
function lookupAndMarkPath(folder, level, path) {
823+
for (const p of path) {
824+
if (p.level === level && p.dir === folder) {
825+
return true;
826+
}
827+
}
828+
return false;
829+
}
830+
831+
async function getDirectoryListingInternal(path, req, files, folders) {
802832
let html = "";
803833
let lastProcessedFileIndex = -1;
804834
let lastFile = null;
835+
let currentLevel = 0;
836+
805837
for (let i = 0; i < files.length; i++) {
806838
const file = files[i];
807839
lastFile = file;
@@ -818,6 +850,10 @@ async function getDirectoryListingInternal(req, files, folders) {
818850
for (let j = diffIndex; j < file.folderArray.length; j++) {
819851
const folder = file.folderArray[j];
820852
html += insertDirFolder(folder, j);
853+
if (lookupAndMarkPath(folder, currentLevel, path.path)) {
854+
openNavTreeScript += `toggleDirList('ff-${folder}-${j}');\n`;
855+
currentLevel++;
856+
}
821857
}
822858
folders = file.folderArray;
823859

@@ -828,7 +864,7 @@ async function getDirectoryListingInternal(req, files, folders) {
828864
const subfolderFiles = files.filter(
829865
(f, index) => f.folders.startsWith(folders) && index > i
830866
);
831-
html += await getDirectoryListingInternal(req, subfolderFiles, folders);
867+
html += await getDirectoryListingInternal(path, req, subfolderFiles, folders);
832868

833869
// Update the last processed file index
834870
lastProcessedFileIndex = i + subfolderFiles.length;
@@ -865,9 +901,14 @@ function insertDirFolder(folder, j) {
865901

866902
function insertDirLink(file, req, indent, i, files) {
867903
let r = "";
868-
r += `<a href="/${dirPrefix + file.path}" class="${
869-
"/" + file.path === decodeURIComponent(req.path) ? "highlight" : ""
870-
}">${indentStringFor(file.lastFolder === "" ? 0 : indent)}${
904+
let correctedPath = decodeURIComponent(req.path);
905+
if(correctedPath.startsWith("/md/")) {
906+
correctedPath = correctedPath.slice(4);
907+
}
908+
let highlight = "";
909+
if(file.path === correctedPath)
910+
highlight = "highlight";
911+
r += `<a href="/${dirPrefix + file.path}" class="${highlight}">${indentStringFor(file.lastFolder === "" ? 0 : indent)}${
871912
file.fileNameWithoutExtension
872913
}</a>`;
873914
// Only add <br> if it isn't the last file in the folder
@@ -1113,6 +1154,7 @@ export async function wrapInPage(html, startPage, req) {
11131154
)}');
11141155
init();
11151156
</script>
1157+
${openNavTreeScript}
11161158
</body>
11171159
</html>
11181160
`;

0 commit comments

Comments
 (0)