Files
bobbie-pkm/Templates/Scripts/note-rename.js
2026-03-02 17:06:32 +00:00

48 lines
1.5 KiB
JavaScript

module.exports = async (params) => {
const { app } = params;
const activeFile = app.workspace.getActiveFile();
if (!activeFile) return;
const cache = app.metadataCache.getFileCache(activeFile);
const fm = cache?.frontmatter;
if (!fm || !fm.title) {
new Notice("No changes made, title property not set");
return;
}
// 1. Get the Date components
const createdDate = fm.created?.split('T')[0]; // e.g. 2026-01-06
const eventDate = fm.date || createdDate || moment().format("YYYY-MM-DD");
// 2. Logic: Should we use actual time or 0000?
let timePart = "";
// If the event is happening today, use the actual created time
if (eventDate === createdDate) {
if (fm.created && fm.created.includes('T')) {
timePart = fm.created.split('T')[1].replace(/:/g, '').substring(0, 4);
} else {
new Notice("Error: 'created' property is missing or incorrect format");
return;
}
}
// 3. Construct filename
let newName = "";
if (timePart.length === 0) {
newName = `${eventDate} ${fm.title}`;
}
else {
newName = `${eventDate} ${timePart} ${fm.title}`;
}
const newPath = `${activeFile.parent.path}/${newName}.md`;
// 4. Rename
if (activeFile.name !== `${newName}.md`) {
await app.fileManager.renameFile(activeFile, newPath);
new Notice(`Renamed: ${newName}`);
} else {
new Notice("Filename already matches.");
}
};