mirror of
https://github.com/docmarionum1/obsidian-pinned-daily-notes.git
synced 2026-07-22 05:41:59 +00:00
ts implementation
This commit is contained in:
parent
860b4e98e9
commit
70e6c2ce3f
5 changed files with 263 additions and 182 deletions
215
main.js
215
main.js
|
|
@ -1,108 +1,123 @@
|
|||
/*
|
||||
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
||||
if you want to view the source, please visit the github repository
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
|
||||
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;
|
||||
// main.ts
|
||||
var main_exports = {};
|
||||
__export(main_exports, {
|
||||
default: () => PinDailyNotePlugin
|
||||
});
|
||||
module.exports = __toCommonJS(main_exports);
|
||||
var import_obsidian = require("obsidian");
|
||||
var PinDailyNotePlugin = class extends import_obsidian.Plugin {
|
||||
constructor(app, manifest) {
|
||||
super(app, manifest);
|
||||
this.obsidianApp = app;
|
||||
}
|
||||
async onload() {
|
||||
const handleDailyNote = async () => {
|
||||
const todayPath = this.getTodayNotePath();
|
||||
if (!todayPath)
|
||||
return;
|
||||
const leaves = this.obsidianApp.workspace.getLeavesOfType("markdown");
|
||||
let leaf = leaves.find((leaf2) => {
|
||||
var _a;
|
||||
const obsLeaf = leaf2;
|
||||
const view = obsLeaf.view;
|
||||
return obsLeaf.pinned && this.isDailyNotePath((_a = view == null ? void 0 : view.file) == null ? void 0 : _a.path);
|
||||
});
|
||||
if (!leaf) {
|
||||
leaf = this.obsidianApp.workspace.getLeaf("tab");
|
||||
leaf.setPinned(true);
|
||||
}
|
||||
this.obsidianApp.workspace.setActiveLeaf(leaf, { focus: true });
|
||||
let todayFile = this.obsidianApp.vault.getAbstractFileByPath(todayPath);
|
||||
if (!(todayFile instanceof import_obsidian.TFile)) {
|
||||
const dailyNotesCommand = this.obsidianApp.commands.commands["daily-notes"];
|
||||
if (dailyNotesCommand) {
|
||||
await dailyNotesCommand.callback();
|
||||
const newLeaf = this.obsidianApp.workspace.getMostRecentLeaf();
|
||||
if (newLeaf && newLeaf !== leaf) {
|
||||
newLeaf.detach();
|
||||
}
|
||||
todayFile = this.obsidianApp.vault.getAbstractFileByPath(todayPath);
|
||||
}
|
||||
}
|
||||
if (todayFile instanceof import_obsidian.TFile) {
|
||||
await leaf.openFile(todayFile);
|
||||
this.obsidianApp.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() {
|
||||
var _a, _b, _c;
|
||||
const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins["daily-notes"];
|
||||
if (!(dailyNotesPlugin == null ? void 0 : dailyNotesPlugin.enabled))
|
||||
return null;
|
||||
try {
|
||||
const settings = (_a = dailyNotesPlugin.instance) == null ? void 0 : _a.options;
|
||||
if (!settings)
|
||||
return null;
|
||||
const folder = ((_b = settings.folder) == null ? void 0 : _b.trim().replace(/\/$/, "")) || "";
|
||||
const format = ((_c = settings.format) == null ? void 0 : _c.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;
|
||||
}
|
||||
}
|
||||
isDailyNotePath(path) {
|
||||
var _a, _b, _c;
|
||||
if (!path)
|
||||
return false;
|
||||
const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins["daily-notes"];
|
||||
if (!(dailyNotesPlugin == null ? void 0 : dailyNotesPlugin.enabled))
|
||||
return false;
|
||||
try {
|
||||
const settings = (_a = dailyNotesPlugin.instance) == null ? void 0 : _a.options;
|
||||
if (!settings)
|
||||
return false;
|
||||
const folder = ((_b = settings.folder) == null ? void 0 : _b.trim().replace(/\/$/, "")) || "";
|
||||
if (folder && !path.startsWith(folder))
|
||||
return false;
|
||||
const filename = path.slice(folder ? folder.length + 1 : 0, -3);
|
||||
return window.moment(filename, ((_c = settings.format) == null ? void 0 : _c.trim()) || "YYYY-MM-DD", true).isValid();
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PinDailyNotePlugin;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
199
main.ts
199
main.ts
|
|
@ -1,87 +1,148 @@
|
|||
import { App, Plugin, TFile, moment } from 'obsidian';
|
||||
import { App, Plugin, TFile, TAbstractFile, WorkspaceLeaf, View } from 'obsidian';
|
||||
import type moment from 'moment';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
moment: typeof moment;
|
||||
}
|
||||
}
|
||||
|
||||
interface DailyNotesSettings {
|
||||
folder?: string;
|
||||
format?: string;
|
||||
}
|
||||
|
||||
interface DailyNotesPlugin {
|
||||
enabled: boolean;
|
||||
instance?: {
|
||||
options: DailyNotesSettings;
|
||||
};
|
||||
}
|
||||
|
||||
interface ObsidianApp extends App {
|
||||
internalPlugins: {
|
||||
plugins: {
|
||||
'daily-notes': DailyNotesPlugin;
|
||||
};
|
||||
};
|
||||
commands: {
|
||||
commands: Record<string, { callback: () => Promise<void> }>;
|
||||
};
|
||||
}
|
||||
|
||||
interface ObsidianWorkspaceLeaf extends WorkspaceLeaf {
|
||||
pinned?: boolean;
|
||||
}
|
||||
|
||||
interface ObsidianView extends View {
|
||||
file?: TFile;
|
||||
}
|
||||
|
||||
export default class PinDailyNotePlugin extends Plugin {
|
||||
pinnedLeafId: string | null = null;
|
||||
private obsidianApp: ObsidianApp;
|
||||
|
||||
async onload() {
|
||||
// Pin today's note on startup
|
||||
this.app.workspace.onLayoutReady(() => this.pinTodayNote());
|
||||
constructor(app: App, manifest: any) {
|
||||
super(app, manifest);
|
||||
this.obsidianApp = app as ObsidianApp;
|
||||
}
|
||||
|
||||
// Register event for when daily notes plugin creates a new note
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('file-open', (file) => {
|
||||
if (file && this.isDailyNote(file)) {
|
||||
this.pinDailyNote(file);
|
||||
async onload(): Promise<void> {
|
||||
const handleDailyNote = async (): Promise<void> => {
|
||||
const todayPath = this.getTodayNotePath();
|
||||
if (!todayPath) return;
|
||||
|
||||
const leaves = this.obsidianApp.workspace.getLeavesOfType('markdown');
|
||||
let leaf = leaves.find(leaf => {
|
||||
const obsLeaf = leaf as ObsidianWorkspaceLeaf;
|
||||
const view = obsLeaf.view as ObsidianView;
|
||||
return obsLeaf.pinned && this.isDailyNotePath(view?.file?.path);
|
||||
});
|
||||
|
||||
if (!leaf) {
|
||||
leaf = this.obsidianApp.workspace.getLeaf('tab');
|
||||
(leaf as ObsidianWorkspaceLeaf).setPinned(true);
|
||||
}
|
||||
|
||||
this.obsidianApp.workspace.setActiveLeaf(leaf, { focus: true });
|
||||
|
||||
let todayFile = this.obsidianApp.vault.getAbstractFileByPath(todayPath);
|
||||
if (!(todayFile instanceof TFile)) {
|
||||
const dailyNotesCommand = this.obsidianApp.commands.commands['daily-notes'];
|
||||
if (dailyNotesCommand) {
|
||||
await dailyNotesCommand.callback();
|
||||
const newLeaf = this.obsidianApp.workspace.getMostRecentLeaf();
|
||||
if (newLeaf && newLeaf !== leaf) {
|
||||
newLeaf.detach();
|
||||
}
|
||||
todayFile = this.obsidianApp.vault.getAbstractFileByPath(todayPath);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Add ribbon icon to manually pin today's note
|
||||
this.addRibbonIcon('pin', 'Pin Today\'s Note', () => {
|
||||
this.pinTodayNote();
|
||||
if (todayFile instanceof TFile) {
|
||||
await leaf.openFile(todayFile);
|
||||
this.obsidianApp.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(),
|
||||
});
|
||||
}
|
||||
|
||||
getDailyNoteSettings() {
|
||||
// @ts-ignore
|
||||
const dailyNotesPlugin = this.app.internalPlugins.plugins['daily-notes'];
|
||||
// Explicitly check boolean status
|
||||
const isEnabled = dailyNotesPlugin?.enabled === true;
|
||||
if (!isEnabled) {
|
||||
return null;
|
||||
}
|
||||
return dailyNotesPlugin.instance.options;
|
||||
}
|
||||
|
||||
isDailyNote(file: TFile): boolean {
|
||||
const dailyNotePath = this.getTodayNotePath();
|
||||
return dailyNotePath ? file.path === dailyNotePath : false;
|
||||
}
|
||||
|
||||
getTodayNotePath(): string | null {
|
||||
const settings = this.getDailyNoteSettings();
|
||||
if (!settings) {
|
||||
console.log('Daily Notes plugin is not enabled');
|
||||
const dailyNotesPlugin = this.obsidianApp.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;
|
||||
}
|
||||
|
||||
const folder = settings.folder?.trim() || '';
|
||||
const format = settings.format?.trim() || 'YYYY-MM-DD';
|
||||
const filename = moment().format(format);
|
||||
|
||||
return folder
|
||||
? `${folder}/${filename}.md`
|
||||
: `${filename}.md`;
|
||||
}
|
||||
|
||||
async pinTodayNote() {
|
||||
const dailyNotePath = this.getTodayNotePath();
|
||||
if (!dailyNotePath) {
|
||||
return;
|
||||
}
|
||||
|
||||
const file = this.app.vault.getAbstractFileByPath(dailyNotePath);
|
||||
if (file instanceof TFile) {
|
||||
await this.pinDailyNote(file);
|
||||
}
|
||||
}
|
||||
|
||||
async pinDailyNote(file: TFile) {
|
||||
let targetLeaf = this.pinnedLeafId
|
||||
? this.app.workspace.getLeafById(this.pinnedLeafId)
|
||||
: this.app.workspace.getLeaf(true);
|
||||
|
||||
if (!targetLeaf) {
|
||||
targetLeaf = this.app.workspace.getLeaf(true);
|
||||
}
|
||||
|
||||
// Set the leaf to be pinned
|
||||
await targetLeaf.setPinned(true);
|
||||
isDailyNotePath(path: string | undefined): boolean {
|
||||
if (!path) return false;
|
||||
|
||||
// Store the leaf id
|
||||
this.pinnedLeafId = targetLeaf.id;
|
||||
const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins['daily-notes'];
|
||||
if (!dailyNotesPlugin?.enabled) return false;
|
||||
|
||||
// Open the file in the pinned leaf
|
||||
await targetLeaf.openFile(file);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
"id": "obsidian-pinned-daily-notes",
|
||||
"name": "Pinned Daily Notes",
|
||||
"version": "1.0.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"minAppVersion": "1.8.9",
|
||||
"description": "Dynamically update a pinned tab with today's daily note",
|
||||
"author": "Jeremy Neiman",
|
||||
"isDesktopOnly": false
|
||||
|
|
|
|||
24
package.json
24
package.json
|
|
@ -1,24 +1,28 @@
|
|||
{
|
||||
"name": "obsidian-pin-daily-note",
|
||||
"name": "obsidian-pinned-daily-notes",
|
||||
"version": "1.0.0",
|
||||
"description": "Automatically pins today's daily note to the first tab",
|
||||
"description": "Automatically pins today's daily note to a tab and updates it when opening new daily notes",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": ["obsidian", "plugin", "daily-notes"],
|
||||
"keywords": [
|
||||
"obsidian",
|
||||
"plugin",
|
||||
"daily-notes"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.2.0",
|
||||
"@typescript-eslint/parser": "^5.2.0",
|
||||
"builtin-modules": "^3.2.0",
|
||||
"esbuild": "0.13.12",
|
||||
"obsidian": "^1.1.1",
|
||||
"tslib": "2.3.1",
|
||||
"typescript": "4.4.4"
|
||||
"@typescript-eslint/eslint-plugin": "5.29.0",
|
||||
"@typescript-eslint/parser": "5.29.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
"esbuild": "0.17.3",
|
||||
"obsidian": "latest",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,11 @@
|
|||
"module": "ESNext",
|
||||
"target": "ES6",
|
||||
"allowJs": true,
|
||||
"noImplicitAny": false,
|
||||
"noImplicitAny": true,
|
||||
"moduleResolution": "node",
|
||||
"importHelpers": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ES5",
|
||||
|
|
|
|||
Loading…
Reference in a new issue