docmarionum1_obsidian-pinne.../main.js
2025-03-10 20:00:09 -04:00

108 lines
3.6 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository
*/
'use strict';
var obsidian = require('obsidian');
class PinDailyNotePlugin extends obsidian.Plugin {
async onload() {
const handleDailyNote = async () => {
const todayPath = this.getTodayNotePath();
if (!todayPath) return;
const leaves = this.app.workspace.getLeavesOfType('markdown');
let leaf = leaves.find(leaf =>
leaf.pinned && this.isDailyNotePath(leaf.view?.file?.path)
);
if (!leaf) {
leaf = this.app.workspace.getLeaf('tab');
leaf.setPinned(true);
}
this.app.workspace.setActiveLeaf(leaf, { focus: true });
let todayFile = this.app.vault.getAbstractFileByPath(todayPath);
if (!(todayFile instanceof obsidian.TFile)) {
const dailyNotesCommand = this.app.commands.commands['daily-notes'];
if (dailyNotesCommand) {
await dailyNotesCommand.callback();
const newLeaf = this.app.workspace.getMostRecentLeaf();
if (newLeaf && newLeaf !== leaf) {
newLeaf.detach();
}
todayFile = this.app.vault.getAbstractFileByPath(todayPath);
}
}
await leaf.openFile(todayFile);
this.app.workspace.setActiveLeaf(leaf, { focus: true });
};
this.addRibbonIcon('calendar-plus', 'Open today\'s daily note (Pinned)', () => {
handleDailyNote();
});
this.addCommand({
id: 'open-todays-daily-note-pinned',
name: 'Open today\'s daily note',
callback: () => handleDailyNote(),
});
}
getTodayNotePath() {
const dailyNotesPlugin = this.app.internalPlugins.plugins['daily-notes'];
if (!dailyNotesPlugin?.enabled) return null;
try {
const settings = dailyNotesPlugin.instance?.options;
if (!settings) return null;
const folder = settings.folder?.trim().replace(/\/$/, '') || '';
const format = settings.format?.trim() || 'YYYY-MM-DD';
const date = window.moment();
let filename = date.format(format);
if (format.includes('/')) {
const formattedPath = folder
? `${folder}/${filename}`
: filename;
return formattedPath + '.md';
} else {
const path = folder
? `${folder}/${filename}`
: filename;
return path + '.md';
}
} catch (error) {
console.error('Error generating daily note path:', error);
return null;
}
}
isDailyNotePath(path) {
if (!path) return false;
const dailyNotesPlugin = this.app.internalPlugins.plugins['daily-notes'];
if (!dailyNotesPlugin?.enabled) return false;
try {
const settings = dailyNotesPlugin.instance?.options;
if (!settings) return false;
const folder = settings.folder?.trim().replace(/\/$/, '') || '';
if (folder && !path.startsWith(folder)) return false;
const filename = path.slice(folder ? folder.length + 1 : 0, -3);
return window.moment(filename, settings.format?.trim() || 'YYYY-MM-DD', true).isValid();
} catch (error) {
return false;
}
}
}
module.exports = PinDailyNotePlugin;