Skip to content

Instantly share code, notes, and snippets.

@DudeThatsErin
Last active August 7, 2024 17:34
Show Gist options
  • Save DudeThatsErin/2d1e3a262c93f642fe2b5ca2a51a9467 to your computer and use it in GitHub Desktop.
Save DudeThatsErin/2d1e3a262c93f642fe2b5ca2a51a9467 to your computer and use it in GitHub Desktop.
Choose a random banner upon file creation

Erin's Banner Creation on File Creation

All you have to do is the following...

  1. Install Templater (Obsidian Plugin)
  2. Enable it
  3. Open Templater Settings
  4. Scroll down to "User Script Functions"
  5. Create a scripts folder if you don't already have one
  6. Download my BannerOnCreation.js script and put it in that scripts folder.
  7. You will want to update line 24 to where you have your banners. I have a selection of banners available for free.
  8. If you are using the Banners Plugin, you need to remove the |banner on line 27. That is for the banners CSS snippet.
  9. Make sure it detects that file.
  10. Add <% tp.user.BannerOnCreation() %> to whatever templates you want to have automatically create a banner.

This works with the Obsidian Banners Plugin as well as the Banners CSS Snippet just make sure you follow those instructions and put the templater code (#10) where you want the banner to be added.

DONE! If you have done it right, it should work. Unfortunately, I can't upload a video but here is a screenshot: Screenshot 2024-08-06 at 8 55 44 PM

If you want the prompt that asks you which emoji you should add so that you can use <% tp.user.EmojiTitle(tp) %> to add an emoji when your file is created, follow similar steps to the above but instead of the BannerOnCreation.js use the EmojiTitle.js file. Just update line 9 if you are using the banners plugin (as opposed to the snippet).

If you are using AnuPpuccin and the Banners CSS Snippet and your banner title is not positioned right and the icon has a background color and border on it, use the BannerUpdates.css file to remove that. I added this CSS to the CSS Snippet provided under a /* ERINS UPDATE */ comment but you can create a separate snippet or do it however you wish. If you need to reposition it, just change the top and left values on lines 26 & 27 or 13 & 14.

const fs = require('fs').promises;
const path = require('path');
async function getRandomImageFromFolder(folderPath) {
try {
const files = await fs.readdir(folderPath);
const imageFiles = files.filter(file => {
const ext = path.extname(file).toLowerCase();
return ext === '.jpg' || ext === '.jpeg' || ext === '.png' || ext === '.gif';
});
if (imageFiles.length === 0) {
throw new Error('No image files found in the folder.');
}
const randomIndex = Math.floor(Math.random() * imageFiles.length);
return imageFiles[randomIndex];
} catch (error) {
throw new Error(error.message);
}
}
module.exports = async (tp) => {
const folderPath = tp.file.path(true).replace(/\/[^\/]+$/, '') + '/00-attachments/images/banners'; // replace with your banners folder
try {
const randomImage = await getRandomImageFromFolder(folderPath);
const imageMarkdown = `![[00-attachments/images/banners/${randomImage}|banner]]`; // if you are using the banners plugin remove the |banner at the end here. This is for the banners CSS snippet.
return imageMarkdown;
} catch (err) {
return `Error: ${err.message}`;
}
};
.banner.is-live-preview [data-callout="banner-icon"].callout .callout-title {
background: unset;
}
.banner.is-live-preview [data-callout="banner-icon"].callout {
border: unset;
}
/* Works on my 16" MacBook Pro & 55" LG TV */
@media screen and (min-width: 800px) {
.banner.banner-inline-title.markdown-preview-view .inline-title, .banner.banner-inline-title.is-live-preview .inline-title {
position: absolute;
top: 173px;
left: 110px;
}
}
/* Works on my iPad Pro 12.9" */
@media screen and (min-width: 500px) {
.banner.banner-inline-title.markdown-preview-view .inline-title, .banner.banner-inline-title.is-live-preview .inline-title {
background-color: var(--banner-title-background-color);
border-radius: var(--banner-title-border-radius);
--inline-title-color: var(--banner-title-color);
padding: var(--banner-title-padding);
position: absolute;
top: 173px;
left: 80px;
z-index: 1;
max-width: var(--banner-title-max-width);
width: auto !important;
text-wrap: nowrap;
overflow-x: hidden;
}
}
module.exports = async (tp) => {
// Prompt the user for an emoji
const emoji = await tp.system.prompt("Enter an emoji to use in the file title:");
// If no emoji is provided, use a default one
const finalEmoji = emoji ? emoji : "😄";
// Construct the new file name with the emoji
const newFileName = `> [!banner-icon] ${finalEmoji}`; // remove the > [!banner-icon] if you are using the banners plugin
// Rename the file
await tp.file.rename(newFileName);
// Return the new file name (optional)
return newFileName;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment