Dataviewjs link

Things I have tried

This works:

const dayOfWeek = DateTime.now().weekday-1   // normalize to 0-6
const startOfLessonWeek = 0 // lesson week start; lesson is 6 days later
const offset =  dayOfWeek - startOfLessonWeek
const lessonDay = luxon.DateTime.now().minus({'days': offset})
dv.paragraph(dv.fileLink("SecondBrain/Lessons/lesson-" + lessonDay.toFormat("yyyy-MM-dd") , false, "Lesson"))

But it is not an inline link which is what I want. I tried this:

  • =link("SecondBrain/Lessons/lesson-" + luxon.DateTime.now().minus({'days': DateTime.now().weekday-1 - 0}).toFormat("yyyy-MM-dd"), 'Lesson')

But it doesn’t compile. (it does have a single back tick before the equal sign and after the end)

What I’m trying to do

I’m trying to build a link to a lesson note for my dashboard. To work properly in the dashboard, it needs to be a link.

you can’t mix dql syntax with dvjs syntax!

Using almost all of your code, I got it to work, but I changed three parts. First of all, I changed from an inline DQL query, into a inline dataviewjs query by doing `$=, secondly you need to still use the dv.fileLink() like you originally did. And thirdly, you need to change to dv.span( ... ) instead of dv.paragraph( ... ) since you want the end result to be inline.

This leaves us with the following:

start `$= dv.span(dv.fileLink("SecondBrain/Lessons/lesson-" + luxon.DateTime.now().minus({'days': DateTime.now().weekday-1 - 0}).toFormat("yyyy-MM-dd") , false, "Lesson")) ` end

Note that it does need to be one a single line in order to compile, which make these kind of queries somewhat hard to read. So if this construct is something you’re likely to reuse a lot, I would suggest looking into dv.view(), which would require a specific javascript file with your code (but it could be (closely) formatted like your first example), and then you could do stuff like: start $= dv.view("/js/somewhere/lesson", 0) end to achieve the same effect, with the offset as a parameter.

Hope this helps,
Holroy

1 Like

This worked perfectly. Thanks so much. I will spend some time understanding what the differences are between what you did and my initial attempt. I suspect it relates to @mnvwvnm’s comment (which I didn’t understand). I’m fairly new to dataview and obsidian in general. Still trying to grok it all.

  • = > this is the prefix to dql inline queries, not for js inline queries ($=)
  • link() > this is a dql function, not a dvjs function (dv.fileLink())
  • the remaining code is in js

That’s why I said you can’t mix two type of syntax…

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.