* Adds a timeblock to a daily note (creating the note if needed)
( app: App, date: string, timeblock: TimeBlock )
| 921 | * Adds a timeblock to a daily note (creating the note if needed) |
| 922 | */ |
| 923 | async function addTimeblockToDailyNote( |
| 924 | app: App, |
| 925 | date: string, |
| 926 | timeblock: TimeBlock |
| 927 | ): Promise<void> { |
| 928 | const { createDailyNote, getDailyNote, getAllDailyNotes } = await import( |
| 929 | "obsidian-daily-notes-interface" |
| 930 | ); |
| 931 | |
| 932 | const moment = getWindowMoment(date); |
| 933 | const allDailyNotes = getAllDailyNotes(); |
| 934 | let dailyNote = getDailyNote(moment, allDailyNotes); |
| 935 | |
| 936 | if (!dailyNote) { |
| 937 | try { |
| 938 | dailyNote = await createDailyNote(moment); |
| 939 | } catch (error) { |
| 940 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 941 | throw new Error( |
| 942 | `Failed to create daily note: ${errorMessage}. Please check your Daily Notes plugin configuration and ensure the daily notes folder exists.` |
| 943 | ); |
| 944 | } |
| 945 | |
| 946 | // Validate that daily note was created successfully |
| 947 | if (!dailyNote) { |
| 948 | throw new Error( |
| 949 | "Failed to create daily note. Please check your Daily Notes plugin configuration and ensure the daily notes folder exists." |
| 950 | ); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | const content = await app.vault.read(dailyNote); |
| 955 | const frontmatter = |
| 956 | (extractFrontmatter(content) as DailyNoteFrontmatterWithTimeblocks | null) || {}; |
| 957 | |
| 958 | if (!frontmatter.timeblocks) { |
| 959 | frontmatter.timeblocks = []; |
| 960 | } |
| 961 | |
| 962 | frontmatter.timeblocks.push(timeblock); |
| 963 | |
| 964 | // Save back to file |
| 965 | await updateDailyNoteFrontmatter(app, dailyNote, frontmatter, content); |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Updates daily note frontmatter while preserving body content |
no test coverage detected