Dataviewjs and images

Things I have tried

Please help me

My daily note has this code (below). I borrowed it here on the forum, I don’t know how to write it myself yet. Every time you open a note, an image appears, but then for some reason disappears. When switching the editor mode (in any direction), it appears again. Why is this code unstable? What am I doing wrong?

const currentHour = moment().format('HH');
let greeting;
if (currentHour >= 18 || currentHour < 5) {
 	greeting = '🌙 Good evening <br> ![[image1.jpg]]'
} else if (currentHour >= 5 && currentHour < 12) {
	greeting = '🌞 Good morning! <br> ![[image2.jpg]]'
} else {
	greeting = '🌤️ Good afternoon! <br> ![[image3.jpg]]'

dv.header(3, greeting)

### What I'm trying to do


Please describe what you're trying to accomplish so the community can better help you. Sometimes the solution you have in mind is not necessarily the best option, in which case knowing some background info can let others come up with creative solutions for your use case.

Please continue below this line. -->

It looks fine to me, although it’s definitely a bit odd to put a <br> and the image inside your header.

That might be the problem (?), so here’s how I would do it, by separating the header and the image:

const currentHour = +moment().format('H')
let greeting, image
if (currentHour >= 18 || currentHour < 5) {
	greeting = '🌙 Good evening'
	image = 1
} else if (currentHour >= 5 && currentHour < 12) {
	greeting = '🌞 Good morning!'
	image = 2
} else {
	greeting = '🌤️ Good afternoon!'
	image = 3

dv.header(3, greeting)

Thanks! I’ll try :slight_smile:

AlanG, I don’t understand what it means
image = 1
“1” is what? My file is for example “picture_1.jpg”

I think @AlanG look for the expressions ![[image1.jpg]], ![[image2.jpg]], ![[image3.jpg]]… The only variable is the number: “image” + number + “.jpg”.

That’s why he suggest dv.paragraph(`![[image${image}.jpg]]`), where ${image} will be replaced by the number defined in image

But if you define in image something like:

image = "my image.jpg"

then I guess you can use


But Alan will confirm if true or not.


^^^ exactly correct :+1:


Great!!! Thanks! :smile:

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