Last active
July 27, 2024 07:40
-
-
Save thomasloven/418e1cba1f0093528af0b9ffcf27d134 to your computer and use it in GitHub Desktop.
Obsidian.md templater script to update the link chain of Daily Notes
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
const PREV_PROPERTY = "prev-log" | |
const NEXT_PROPERTY = "next-log" | |
const LOG_PATH = "Log/"; | |
/* | |
This will set the prev-log and next-log properties of the current file to the previous and next daily log files. | |
It also updates the properties of the previous and next files to point correctly. | |
Requires the file title to be the date, and nothing else. | |
The script requires access to tp, so call it with | |
<% await tp.user.fixdailylinks(tp) %> | |
With dynamic previous and next pointer using Dataview: | |
`$= "<<" + (dv.current()["prev-log"]||"None") +" | " + (dv.current()["next-log"]||"None") + ">>"` <%* await tp.user.fixdailylinks(tp) %> | |
*/ | |
async function fix_daily_links(tp) { | |
const today = new Date(tp.file.title) | |
const this_link = `[[${tp.config.target_file.path}]]` | |
const log_files = app.vault.getMarkdownFiles().filter(file => { | |
if (!file.path.startsWith(LOG_PATH)) return false; | |
return true | |
}); | |
log_files.sort((a,b) => { | |
if (a.name < b.name) return -1; | |
if (a.name > b.name) return 1; | |
return 0; | |
}) | |
const older_files = log_files.filter(file => { | |
const fileDate = new Date(file.basename) | |
if (fileDate >= today) return false; | |
return true | |
}) | |
const prev_file = older_files?.[older_files.length-1]; | |
const newer_files = log_files.filter(file => { | |
const fileDate = new Date(file.basename) | |
if (fileDate <= today) return false; | |
return true | |
}) | |
const next_file = newer_files?.[0]; | |
setTimeout(async () => { | |
if(prev_file) | |
await app.fileManager.processFrontMatter(prev_file, fm => { | |
fm[NEXT_PROPERTY]= this_link; | |
return fm; | |
}); | |
if(next_file) | |
await app.fileManager.processFrontMatter(next_file, fm => { | |
fm[PREV_PROPERTY]= this_link; | |
return fm; | |
}); | |
await app.fileManager.processFrontMatter(tp.config.target_file, fm => { | |
fm[NEXT_PROPERTY] = next_file ? `[[${next_file.path}]]` : ""; | |
fm[PREV_PROPERTY] = prev_file ? `[[${prev_file.path}]]` : ""; | |
return fm; | |
}) | |
}, 300); | |
return ""; | |
} | |
module.exports = fix_daily_links; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment