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

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