How to hide an empty keyword with inline Javacsript?

Hi! I’m setting up a bookshelf of sorts that has various metadata in the frontmatter which I am summarizing in a callout in the body of the note itself. What I’d like to do is hide a certain keyword in the summary callout when it’s empty in the frontmatter. I am an absolute beginner with Javascript AND Obsidian, and I’m hoping this is just a dead simple thing someone can give me a hand with. What I’ve got so far is copy/pasted from @Gibson 's wonderfully inspiring Bookshelf setup.

Things I have tried

The book note template looks like this:

---
cssclass: bookentry
Author:
Status:
Format:
Location:
Genre:
SeriesName:
SeriesNumber:
---
>[!Metadata]
>**Author:** `= this.Author`
>**Status:** `= this.Status`
>**Format:** `= this.Format`
>**Location:** `= this.Location`
>**Genre:** `= this.Genre`
>**Series:** `= this.SeriesName` : Book `= this.SeriesNumber`

I’ve tried various phrasings of if statements to try to only output that last line if SeriesName has a value, but I haven’t been able to get the grammar right. I tried looking through Substack, but I’m just not super clear on how to translate general JS grammar into something inline that Obsidian understands.

What I’m trying to do

What I really want is for the final “Series” line in the callout/summary to only appear if there is data for the “SeriesName”. I’m just not sure how to make it happen!

Here’s what it looks like for a book in a series:

And here’s what it currently looks like for a book not in a series:

And here’s what I’d like it to look like for a book not in a series:

I could also totally live with this if the “Series:” pair in the callout summary is just blank (or has the autogenerated dash). (So the colon and “Book” word and and SeriesNumber didn’t appear if there is no SeriesName.)

Thanks so much!

1 Like

I’m still trying to figure this out. The following code works properly - that is, it displays the Series line when there is a series entered in the frontmatter, and doesn’t display anything when there isn’t - but I can’t figure out how to get it into the callout summary.

>[!Metadata]
>**Author:** `= this.Author`
>**Status:** `= this.Status`
>**Format:** `= this.Format`
>**Genre:** `= this.Genre`
``` dataviewjs
let page = dv.current();
if (page.SeriesName)
{ dv.paragraph( "**Series:** " + page.SeriesName + " : Book " + page.SeriesNumber ) }

Can you put dataviewjs output in a callout/admonition? I tried putting the greater-than in front of the code block, and I tried putting it in like ">**Series:** " etc as well. Both caused evaluation errors.

I feel like I must be close - can anyone give me a hand on this?

Ok! so “choice” is a primitive if statement! Final code looks like this, in case any one else ever needs it:

>[!Metadata]
>**Author:** `= this.Author`
>**Status:** `= this.Status`
>**Format:** `= this.Format`
>**Genre:** `= this.Genre`
>`= choice(length(this.SeriesName) > 0, ("**Series:** " + this.SeriesName + " : Book " + this.SeriesNumber ), "")`
2 Likes

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