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) {
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 {

View File

@@ -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")

View File

@@ -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}}"
};

View File

@@ -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<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(
vault: Vault,
app: App,
task: Task,
category: Category | undefined,
format: string,
dailyNotePath: string
format: string
): Promise<void> {
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;
}
}