Skip to content

Instantly share code, notes, and snippets.

@johannrichard
Last active August 4, 2024 13:14
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
const zettelPrefix = "YYYYMMDDhhmm⁝";
const zettelRegexp = /^[0-9]{12}[a-zA-Z]{0,2}?⁝ .*$/
// Use the Zettel ID only for the title (set to false if you want the old name to be kept as an appendix to the Zettel ID)
const zettelIdOnly = true;
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" or "YYYYMMDDhhmm⁝"
let fileName = `${id}.md`;
if( !zettelIdOnly) {
fileName = `${id} ${file.basename}.md`;
}
console.log(`New filename: ${fileName}`);
// Add Zettel ID and basename to Frontmatter (in the original file)
plugin.app.fileManager
.processFrontMatter(file, (fm) => {
if ("aliases" in fm) {
fm["aliases"].push(id);
fm["aliases"].push(file.basename);
fm["aliases"].push(`${id} ${file.basename}`);
} else {
// No aliases present
fm["aliases"] = [id];
fm["aliases"].push(file.basename);
fm["aliases"].push(`${id} ${file.basename}`);
}
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