Skip to content

Instantly share code, notes, and snippets.

@johannrichard
Created September 10, 2023 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannrichard/21ebac7debbb1014517bacdf7e231f34 to your computer and use it in GitHub Desktop.
Save johannrichard/21ebac7debbb1014517bacdf7e231f34 to your computer and use it in GitHub Desktop.
Obsidian User Plugins

Obsidian User Plugins

These scripts are to be used with the (fabulous) Obsidian User Plugins plugin. As of today, the following scripts can be found here:

  • Zettlerize.js: this script will make a "Zettel" out of the current note by a) prepending a "Zettel ID" to the filename, b) moving the file to the root of the Vault, c) adding the ID as an alias and d) copying the ID to the clipboard for immediate use

You can easily modify these scripts to your liking when adding them to your Obsidian Vault. Please note that these scripts will modify your vault and bad things might happen. Use at your own risk and make sure you understand the README,md of the Obsidian plugin.

/****
* Zettlerize current note in Obsidian:
* - Create Zettel ID "YYYYMMDDhhmm⁝ "
* - Add Zettel ID to frontmatter
* - Prepend Zettel ID to filename (i.e. rename file)
* - Move file to Vault root
* - Copy ID to clipboard for easy use
*
* This is to be used with
* https://github.com/mnowotnik/obsidian-user-plugins
*/
// Prefix format: change this to the format you desire
const zettelPrefix = "YYYYMMDDhhmm⁝ ";
module.exports = {};
module.exports.onload = function (plugin) {
plugin.addCommand({
name: "Zettlerize Note",
callback: () => {
console.log("Starting..."), console.log(plugin);
const file = plugin.app.workspace.getActiveFile();
if (!file) return void new Notice("No active file.");
console.log("Found active file: ", file.basename);
// New ID: YYYYMMDDhhmm⁝
const id = window.moment().format(zettelPrefix);
console.log("New id: ", id);
// New fileName: "YYYYMMDDhhmm⁝ Basename"
const fileName = id + file.basename + ".md";
console.log("New filename: ", fileName);
// Add Zettel ID to Frontmatter (in the original file)
plugin.app.fileManager
.processFrontMatter(file, (fm) => {
if ("aliases" in fm) {
fm["aliases"].push(id);
} else {
// No aliases present
fm["aliases"] = [id];
}
if ("tags" in fm) {
fm["tags"].push("zettel");
} else {
fm["tags"] = ["zettel"];
}
console.log("Zettel ID added to Frontmatter");
})
.then((result) => {
// Rename the file
plugin.app.fileManager.renameFile(file, fileName);
console.log("Finished!");
navigator.clipboard.writeText(id);
});
},
});
};
module.exports.onunload = function (plugin) {
console.log("unload");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment