Last active
December 14, 2022 19:05
-
-
Save bf4648/17be7b0d672eadaec925d713bdc5dcb5 to your computer and use it in GitHub Desktop.
Display links at the top of an obsidian page for the previous and next files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```dataviewjs | |
let currentFilePath = dv.current().file.path; | |
let currentFileDir = dv.current().file.folder; | |
let currentSubPagesSorted = dv.pages('"' + currentFileDir + '"').sort(b => b.file.name, 'asc').values; | |
function getIndex(options) { | |
let direction = options.direction; | |
let targetIndex = 0; | |
for (let i = 0; i < currentSubPagesSorted.length; i++) { | |
let filePath = currentSubPagesSorted[i].file.path; | |
if (filePath == currentFilePath) { | |
if (direction == 'forward') { | |
targetIndex = i + 1; | |
break; | |
} | |
if (direction == 'backwards') { | |
targetIndex = i - 1; | |
break; | |
} | |
} | |
} | |
return targetIndex | |
} | |
function getPrevFileLink(options) { | |
let prevIndex = options.prevIndex; | |
if (prevIndex == -1) { | |
prevIndex = currentSubPagesSorted.length - 1; | |
} | |
let prevPage = currentSubPagesSorted[prevIndex]; | |
let prevPageFileLink = dv.fileLink(prevPage.file.link.path, false, "<< " + prevPage.file.name); | |
return prevPageFileLink | |
} | |
function getNextFileLink(options) { | |
let nextIndex = options.nextIndex | |
if (nextIndex == currentSubPagesSorted.length) { | |
nextIndex = 0; | |
} | |
let nextPage = currentSubPagesSorted[nextIndex]; | |
let nextPageFileLink = dv.fileLink(nextPage.file.link.path, false, nextPage.file.name + " >>"); | |
return nextPageFileLink; | |
} | |
// prevIndex | |
let prevIndex = getIndex({'direction': 'backwards'}); | |
let prevFileLink = getPrevFileLink({'prevIndex': prevIndex}); | |
// nextIndex | |
let nextIndex = getIndex({'direction': 'forward'}); | |
let nextFileLink = getNextFileLink({'nextIndex': nextIndex}); | |
dv.span(prevFileLink + ' | ' + nextFileLink); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment