import { TFile, Vault, moment } from "obsidian"; import { Task, Category } from "./types"; export function formatDuration(milliseconds: number): string { const totalSeconds = Math.floor(milliseconds / 1000); const hours = Math.floor(totalSeconds / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); const seconds = totalSeconds % 60; if (hours > 0) { return `${hours}h ${minutes}m ${seconds}s`; } else if (minutes > 0) { return `${minutes}m ${seconds}s`; } else { return `${seconds}s`; } } export function generateUniqueId(): string { return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; } export async function logTaskToDailyNote( vault: Vault, task: Task, category: Category | undefined, format: string, dailyNotePath: string ): Promise { const today = moment().format("YYYY-MM-DD"); const dailyNoteFileName = `${today}.md`; 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) .replace("{{duration}}", duration) .replace("{{emoji}}", category?.emoji || "") .replace("{{category}}", category?.name || ""); const content = await vault.read(file); const newContent = content ? `${content}\n${logEntry}` : logEntry; await vault.modify(file, newContent); } }