Load a Seasonal Banner in Obsidian
2 min read

Load a Seasonal Banner in Obsidian

I wrote a user script and a snippet for Obsidian's Templater to load a seasonal banner.
Load a Seasonal Banner in Obsidian

I wrote a user script and a snippet for Obsidian's  Templater to load a seasonal banner.

Script

The season.js script is just a Javascript function which gets a month number as a string (1-12) and returns a season:

/*
 season.js
 This is an Obsidian user script which returns the season of a month.
 
 It accepts a month's number as a string (1-12), to keep it simple.
 You can obtain it from `tp.date.now("MM")`
 
 License: MIT
 Author: Laur Ivan <[email protected]>

*/

function season(month) {
    season = '';
    switch(month) {
        case '12':
        case '1':
        case '2':
            season = 'winter';
        break;
        case '3':
        case '4':
        case '5':
            season = 'spring';
        break;
        case '6':
        case '7':
        case '8':
            season = 'summer';
        break;
        case '9':
        case '10': 
        case '11':
            season = 'fall';
        break;
		default:
			season = "Undefined for month [" + month + "]";
    }
	return season;
}

module.exports = season

You need to place this script in your Templater scripts folder, so it's recognised as a user script.

Templater snippet

The season-banner.md templater snippet is just a switch() statement, with the relevant templater syntax:

<%* let season = tp.user.season(tp.date.now("MM"))
switch(season) { 
	case "fall": %>
banner: "![[fall-image.jpg]]"
banner_y: 0.84538
<%* break;
    case "winter":%>
banner: "![[winter-image.png]]"
banner_y: 0.84538
<%* break;
    case "spring":%>
banner: "![[spring-image.png]]"
banner_y: 0.84538
<%* break;
    case "summer":%>
banner: "![[summer-image.png]]"
banner_y: 0.84538
<%* break;
} %>

I put the snippet in my Templates/snippets directory in the vault and I can call it via include so:

<% tp.file.include("[[season-banner]]") %>

Now, you'll have a banner.

Possible improvements

This is my first Obsidian development and I use this snippet for my notes right now. I can however be made more versatile:

  • allow a parameterised array or dict for the input script
  • make the season.js more versatile, with hemispheres, astronomical seasons and whatnot

I will improve this if I have some time and make it available in a repository. It's on my TODO list :)

Until then, HTH!