Skip to content

Commit fb06e31

Browse files
committed
Initial support for folder notes
1 parent e62ad54 commit fb06e31

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/helpers/filetreeUtils.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function getPermalinkMeta(note, key) {
7070
}
7171
if (note.data.tags && note.data.tags.indexOf("gardenEntry") != -1) {
7272
permalink = "/";
73-
}
73+
}
7474
if (note.data.title) {
7575
name = note.data.title;
7676
}
@@ -112,12 +112,36 @@ function assignNested(obj, keyPath, value) {
112112
obj[keyPath[lastKeyIndex]] = value;
113113
}
114114

115+
/**
116+
* A folder note is a note with the same filename as the parent folder.
117+
* Hide the folder note and link to it from the folder to treat it as a special case.
118+
*
119+
* @returns {void}
120+
*/
121+
function detectFoldersWithFolderNotes(tree) {
122+
for (const [fileOrFolderName, fileOrFolder] of Object.entries(tree)) {
123+
if (fileOrFolder.isFolder) {
124+
// Check if any of the children are notes with the same name
125+
for (const [childName, child] of Object.entries(fileOrFolder)) {
126+
if (child.isNote && childName === (fileOrFolderName + ".md")) {
127+
// Found, hide the standalone note
128+
child.hide = true;
129+
fileOrFolder.folderNote = child
130+
} else if (child.isFolder) {
131+
detectFoldersWithFolderNotes(child);
132+
}
133+
}
134+
}
135+
}
136+
}
137+
115138
function getFileTree(data) {
116139
const tree = {};
117140
(data.collections.note || []).forEach((note) => {
118141
const [meta, folders] = getPermalinkMeta(note);
119142
assignNested(tree, folders, { isNote: true, ...meta });
120143
});
144+
detectFoldersWithFolderNotes(tree);
121145
const fileTree = sortTree(tree);
122146
return fileTree;
123147
}

src/site/_includes/components/filetree.njk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
<div x-show="isOpen" style="display:none" class="{{'filelist' if step>0}}">
44
{% if fileOrFolder.isNote and not fileOrFolder.hide %}
55
<div @click.stop class="notelink {{ 'active-note' if fileOrFolder.permalink === permalink}}">
6-
<a data-note-icon="{{fileOrFolder.noteIcon}}" style="text-decoration: none;" class="filename" href="{{fileOrFolder.permalink}}">{{fileOrFolder.name}} </a>
6+
<a data-note-icon="{{fileOrFolder.noteIcon}}" style="text-decoration: none;" class="filename" href="{{fileOrFolder.permalink}}">{{fileOrFolder.name}}</a>
77
</div>
88
{% elif fileOrFolder.isFolder %}
99
<div class="folder inner-folder" x-data="{isOpen: $persist(false).as({{currentPath | dump}})}" @click.stop="isOpen=!isOpen">
1010
<div class="foldername-wrapper align-icon">
1111
<i x-show="isOpen" style="display: none;" data-lucide="chevron-down"></i>
1212
<i x-show="!isOpen" data-lucide="chevron-right"></i>
13-
<span class="foldername">{{fileOrFolderName}}</span>
13+
{% if fileOrFolder.folderNote %}
14+
<a @click.stop class="filename" href="{{fileOrFolder.folderNote.permalink}}">{{fileOrFolderName}}</a>
15+
{% else %}
16+
<span class="foldername">{{fileOrFolderName}}</span>
17+
{% endif %}
1418
</div>
1519
{% for fileOrFolderName, child in fileOrFolder %}
1620
{{menuItem(fileOrFolderName, child, step+1, (currentPath+"/"+fileOrFolderName))}}

0 commit comments

Comments
 (0)