From bbc03acd038b1684c9100a3011efb49576048b22 Mon Sep 17 00:00:00 2001 From: crib Date: Wed, 17 Dec 2025 20:48:49 +0100 Subject: [PATCH] updated daily notes section to be in line with core plugin --- main.ts | 5 ++--- settings.ts | 14 ++++-------- types.ts | 4 +--- utils.ts | 64 +++++++++++++++++++++++++++++++---------------------- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/main.ts b/main.ts index 0c4a664..d0a82b9 100644 --- a/main.ts +++ b/main.ts @@ -148,11 +148,10 @@ export default class TaskWeaverPlugin extends Plugin { if (this.settings.enableDailyNoteLogging) { const category = this.settings.categories.find(c => c.id === task.categoryId); await logTaskToDailyNote( - this.app.vault, + this.app, task, category, - this.settings.dailyNoteFormat, - this.settings.dailyNotePath + this.settings.dailyNoteFormat ); } } else { diff --git a/settings.ts b/settings.ts index a9540ff..d03841d 100644 --- a/settings.ts +++ b/settings.ts @@ -25,16 +25,10 @@ export class TaskWeaverSettingTab extends PluginSettingTab { })); if (this.plugin.settings.enableDailyNoteLogging) { - new Setting(containerEl) - .setName("Daily note path") - .setDesc("Path to your daily notes folder (e.g., 'Daily Notes' or leave empty for root)") - .addText(text => text - .setPlaceholder("Daily Notes") - .setValue(this.plugin.settings.dailyNotePath) - .onChange(async (value) => { - this.plugin.settings.dailyNotePath = value; - await this.plugin.saveSettings(); - })); + containerEl.createEl("p", { + text: "Tasks will be logged to your daily note using the Daily Notes core plugin settings.", + cls: "setting-item-description" + }); new Setting(containerEl) .setName("Daily note format") diff --git a/types.ts b/types.ts index 90afdda..ce40abc 100644 --- a/types.ts +++ b/types.ts @@ -20,13 +20,11 @@ export interface TaskWeaverSettings { tasks: Task[]; enableDailyNoteLogging: boolean; dailyNoteFormat: string; - dailyNotePath: string; } export const DEFAULT_SETTINGS: TaskWeaverSettings = { categories: [], tasks: [], enableDailyNoteLogging: false, - dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}", - dailyNotePath: "" + dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}" }; diff --git a/utils.ts b/utils.ts index de629d9..d7f6c1d 100644 --- a/utils.ts +++ b/utils.ts @@ -1,4 +1,4 @@ -import { TFile, Vault, moment } from "obsidian"; +import { App, TFile } from "obsidian"; import { Task, Category } from "./types"; export function formatDuration(milliseconds: number): string { @@ -20,34 +20,43 @@ export function generateUniqueId(): string { return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; } +async function getDailyNote(app: App): Promise { + const { vault } = app; + + // @ts-ignore - Access internal daily notes API + const dailyNotesPlugin = app.internalPlugins.plugins["daily-notes"]; + + if (!dailyNotesPlugin || !dailyNotesPlugin.enabled) { + throw new Error("Daily notes plugin is not enabled. Please enable it in Settings → Core plugins."); + } + + // @ts-ignore - Access daily notes interface + const { createDailyNote, getDailyNote, getAllDailyNotes } = app.internalPlugins.plugins["daily-notes"].instance; + + // @ts-ignore + const dailyNotes = getAllDailyNotes(); + // @ts-ignore + const today = window.moment(); + // @ts-ignore + let dailyNote = getDailyNote(today, dailyNotes); + + if (!dailyNote) { + // @ts-ignore + dailyNote = await createDailyNote(today); + } + + return dailyNote; +} + export async function logTaskToDailyNote( - vault: Vault, + app: App, task: Task, category: Category | undefined, - format: string, - dailyNotePath: string + format: string ): Promise { - const today = moment().format("YYYY-MM-DD"); - const dailyNoteFileName = `${today}.md`; + try { + const dailyNote = await getDailyNote(app); - let dailyNoteFolderPath = dailyNotePath.trim(); - if (dailyNoteFolderPath && !dailyNoteFolderPath.endsWith("/")) { - dailyNoteFolderPath += "/"; - } - - const fullPath = `${dailyNoteFolderPath}${dailyNoteFileName}`; - - let file = vault.getAbstractFileByPath(fullPath); - - if (!file || !(file instanceof TFile)) { - const folder = dailyNoteFolderPath.slice(0, -1); - if (folder && !vault.getAbstractFileByPath(folder)) { - await vault.createFolder(folder); - } - file = await vault.create(fullPath, ""); - } - - if (file instanceof TFile) { const duration = formatDuration(task.totalElapsed); const logEntry = format .replace("{{title}}", task.title) @@ -55,8 +64,11 @@ export async function logTaskToDailyNote( .replace("{{emoji}}", category?.emoji || "") .replace("{{category}}", category?.name || ""); - const content = await vault.read(file); + const content = await app.vault.read(dailyNote); const newContent = content ? `${content}\n${logEntry}` : logEntry; - await vault.modify(file, newContent); + await app.vault.modify(dailyNote, newContent); + } catch (error) { + console.error("Failed to log task to daily note:", error); + throw error; } }