updated daily notes section to be in line with core plugin

This commit is contained in:
2025-12-17 20:48:49 +01:00
parent a265c31460
commit bbc03acd03
4 changed files with 45 additions and 42 deletions

View File

@@ -148,11 +148,10 @@ export default class TaskWeaverPlugin extends Plugin {
if (this.settings.enableDailyNoteLogging) { if (this.settings.enableDailyNoteLogging) {
const category = this.settings.categories.find(c => c.id === task.categoryId); const category = this.settings.categories.find(c => c.id === task.categoryId);
await logTaskToDailyNote( await logTaskToDailyNote(
this.app.vault, this.app,
task, task,
category, category,
this.settings.dailyNoteFormat, this.settings.dailyNoteFormat
this.settings.dailyNotePath
); );
} }
} else { } else {

View File

@@ -25,16 +25,10 @@ export class TaskWeaverSettingTab extends PluginSettingTab {
})); }));
if (this.plugin.settings.enableDailyNoteLogging) { if (this.plugin.settings.enableDailyNoteLogging) {
new Setting(containerEl) containerEl.createEl("p", {
.setName("Daily note path") text: "Tasks will be logged to your daily note using the Daily Notes core plugin settings.",
.setDesc("Path to your daily notes folder (e.g., 'Daily Notes' or leave empty for root)") cls: "setting-item-description"
.addText(text => text });
.setPlaceholder("Daily Notes")
.setValue(this.plugin.settings.dailyNotePath)
.onChange(async (value) => {
this.plugin.settings.dailyNotePath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl) new Setting(containerEl)
.setName("Daily note format") .setName("Daily note format")

View File

@@ -20,13 +20,11 @@ export interface TaskWeaverSettings {
tasks: Task[]; tasks: Task[];
enableDailyNoteLogging: boolean; enableDailyNoteLogging: boolean;
dailyNoteFormat: string; dailyNoteFormat: string;
dailyNotePath: string;
} }
export const DEFAULT_SETTINGS: TaskWeaverSettings = { export const DEFAULT_SETTINGS: TaskWeaverSettings = {
categories: [], categories: [],
tasks: [], tasks: [],
enableDailyNoteLogging: false, enableDailyNoteLogging: false,
dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}", dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}"
dailyNotePath: ""
}; };

View File

@@ -1,4 +1,4 @@
import { TFile, Vault, moment } from "obsidian"; import { App, TFile } from "obsidian";
import { Task, Category } from "./types"; import { Task, Category } from "./types";
export function formatDuration(milliseconds: number): string { export function formatDuration(milliseconds: number): string {
@@ -20,34 +20,43 @@ export function generateUniqueId(): string {
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
} }
async function getDailyNote(app: App): Promise<TFile> {
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( export async function logTaskToDailyNote(
vault: Vault, app: App,
task: Task, task: Task,
category: Category | undefined, category: Category | undefined,
format: string, format: string
dailyNotePath: string
): Promise<void> { ): Promise<void> {
const today = moment().format("YYYY-MM-DD"); try {
const dailyNoteFileName = `${today}.md`; 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 duration = formatDuration(task.totalElapsed);
const logEntry = format const logEntry = format
.replace("{{title}}", task.title) .replace("{{title}}", task.title)
@@ -55,8 +64,11 @@ export async function logTaskToDailyNote(
.replace("{{emoji}}", category?.emoji || "") .replace("{{emoji}}", category?.emoji || "")
.replace("{{category}}", category?.name || ""); .replace("{{category}}", category?.name || "");
const content = await vault.read(file); const content = await app.vault.read(dailyNote);
const newContent = content ? `${content}\n${logEntry}` : logEntry; 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;
} }
} }