This has probably already been done, but I quite like making notes with sub-notes, so I made a little way to go up to the previous sub-note!
Go ‘up’ a page in the navigation hierarchy
silverbullet.registerCommand({
name: "Navigate: Up"
}, async () => {
const current_page = await editor.getCurrentPage();
// If we're not currently actually anywhere
if ( current_page.indexOf("/") == -1
&& current_page != "index")
{
// go home
editor.navigate("index", false, false);
return
}
// Else
// Doing it this way to avoid some disgusting Regex
let new_page_path = current_page.split("/");
new_page_path.pop();
editor.navigate(new_page_path.join("/"), false, false);
});
6 Likes
mjf
(Matouš Jan Fialka)
January 29, 2025, 8:09am
2
Slightly modified version taking into account that some people set indexPage
to something else than the default index
:
silverbullet.registerCommand({
name: 'Navigate: Parent Page'
}, async () => {
const current_page = await editor.getCurrentPage()
const config_index_page = await system.getSpaceConfig('indexPage', 'index')
const index_page = config_index_page
.toString()
.replace(/^\[\[/, '')
.replace(/\]\]$/, '')
if(current_page.indexOf('/') == -1 && current_page != index_page) {
editor.navigate(index_page, false, false)
return
}
let new_page_path = current_page.split('/')
new_page_path.pop()
editor.navigate(new_page_path.join('/'), false, false)
return
})
Next step would be to adapt it (optionaly, not everybody would like it) to skip empty parent pages.
2 Likes
Bam, some sexy sexy recursion and we have an option to skip or not skip empty parents.
And, according to some guy on the internet having the await inside of the if check doesn’t lose us any time when skip_empty_parents
is false.
silverbullet.registerCommand({
name: "Navigate: Parent Page"
}, async (current_page) => {
// Toggle this if you want this function to skip empty parent notes
const skip_empty_parents = false;
if (typeof(current_page) == "object") {
current_page = await editor.getCurrentPage();
}
const config_index_page = await system.getSpaceConfig("indexPage", 'index');
const index_page = config_index_page
.toString()
.replace(/^\[\[/, '')
.replace(/\]\]$/, '');
if(current_page.indexOf('/') == -1 && current_page != index_page) {
editor.navigate(index_page, false, false);
return
}
let new_page_path = current_page.split('/');
new_page_path.pop();
new_page_path = new_page_path.join('/');
if (skip_empty_parents && ! await space.fileExists(new_page_path + ".md")) {
system.invokeCommand("Navigate: Parent Page", new_page_path);
return
}
editor.navigate(new_page_path, false, false);
return
})
(Also, I honestly didn’t even realise you could not have a main “index” page, so that’s funky to learn)
Aaaand edited to remove some console logs I forgot to remove
5 Likes
mjf
(Matouš Jan Fialka)
February 13, 2025, 9:57pm
5
Hm, what can be done to let back/forth navigation work with this too?