Skip to content

Instantly share code, notes, and snippets.

@johannrichard
Last active July 13, 2024 09:38
Show Gist options
  • 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

  • Copy WikiLink.js: this script will copy a WikiLink to the current file to the clipboard. It's been inspired by this question on the Obsidian Forum.

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
// Make sure to modify the regexp accordingly.
// This one accomodates for Zettels which also include a "Folgezettel" part of up to two characters
const zettelPrefix = "YYYYMMDDhhmm⁝ ";
const zettelRegexp = /^[0-9]{12}[a-zA-Z]{0,2}?⁝ .*$/
module.exports = {};
module.exports.onload = function (plugin) {
plugin.addCommand({
id: "zettlerize-note",
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}`);
if(!zettelRegexp.test(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);
});
} else {
console.warn(`File ${file.basename} is already a Zettel`);
}
},
});
};
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