What I’m trying to do
Sorry if this is a basic question but I am struggling with formatting my dataviewjs block. I have added a moonday property to my Daily Note frontmatter in the format of moonday : mXX (where XX is the moonday). This helps me track the Moon’s age in the sidereal calendar. I have a folder of pixelated .png files that follow the name format of the moonday (mXX.png). I would like to set up my daily notes so that once I populate the moonday property, the appropriate png will be displayed.
Things I have tried
plugins used: dataview
css-snippets: ITS-image-adjustment
I quickly realized that using $= dv.current().moonday was not possible to insert into an inline link and therefore opted to write a dataviewjs block.
1st attempt:
let currentMoonday = dv.current().moonday
dv.paragraph( "![[" + currentMoonday + ".png|right htiny clear-hr]]" )
This did not work as only the alt text would be displayed depending on the source mode and the file would not update dynamically if the moonday property was changed.
2nd attempt
I was able to stumble upon a very amazing post on this forum and was able to get the task to work properly by modifying the code presented as so:
const myField = app.vault.getFiles().filter(file => file.extension === 'png' && file.path.includes('moon') && file.name.includes(dv.current().moonday))
const myFieldType = dv.func.typeof(myField)
if ( myFieldType == "link" ) {
dv.span(imgDisplay(dv.fileLink(dv.current().ID + ".png")))
} else if ( myFieldType == "array" ) {
myField.forEach(i => dv.span(imgDisplay(i)))
}
function imgDisplay(imageLink) {
return '<img src="' + app.vault.getResourcePath(dv.fileLink(imageLink.path)) + '" alt="right htiny clear-hr"/>'
}
My question, however refers to the reply noting that this code is incomplete as the elif statement logic should allow escaping with an error given multiple or unknown values. I do have an image named munknown.png and the Daily Note template defaults to moonday: munknown upon creation. However, I would love to escape my block with an error if moonday is not found in the folder or if multiple values exist.
Apologies if this is a basic question. I am slowly teaching myself JavaScript from scratch but am having ++issues with combining standard JS learning with Dataviewjs codeblocks.