|
/**** |
|
* 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"); |
|
}; |