Hi I built a quick script with dataviewjs to collate my daily note lists for my weekly note. It’s using regular expressions and the obsidian API to accomplish this. I’m pretty new to using javascript so any feedback will be appreciated.
//Set the beginning and end of the week
const beg = moment("2021-05-16")
const end = moment("2021-05-22")
//The RegEx that does the heavy lifting
const er = new RegExp(/##.Lists\n([\s|\S]*)##.[I|D]/g);
const re = new RegExp(/(?:####.*\n)(?:[-|\d.].*\n)*/g);
/*
My Lists look like this
## Lists
#### a list
- markdown
#### another list
- [ ] it catches these too
*/
//Obsidian's API has allows you to grab a file's parent
const par = app.vault.getAbstractFileByPath("Daily/2021-05-13.md").parent;
//Get the files for the week as a Map
const week = par.children.reduce((acc,chil) => {
let name = chil.basename;
let date = moment(name);
if (date >= beg && date <= end){
acc.set(name,chil)
}
return acc
},new Map())
const sortWeek = new Map([...week].sort((a,b) => String(a[0]).localeCompare(b[0])))
sortWeek.forEach((v,k,m) => {
let path = app.vault.getAbstractFileByPath(v.path)
app.vault.read(path)
.then( fileText => {
let listText = fileText.match(er)
return new Promise((resolve,reject) =>{resolve(...listText)})
})
.then(listText => {
const lists = listText.match(re);
if (lists !== null){
dv.header(4,k)
dv.list(lists)
}
})
});