1+ import { json2md , md2json , parseJsonContent , } from "../lib/markdown" ;
12import { getDaysAgo , getToday , } from "../types/note" ;
23import type { DailyNote , } from "../types/note" ;
34import { loadDailyNote , saveDailyNote , } from "./storage" ;
@@ -141,29 +142,27 @@ function prependTasks(content: string, tasks: TaskLine[],): string {
141142 */
142143export async function rolloverTasks ( days : number = 30 , ) : Promise < boolean > {
143144 const today = getToday ( ) ;
144- // Map from task text → TaskLine (preserves indent, deduplicates by text)
145145 const taskMap = new Map < string , TaskLine > ( ) ;
146146 const modifiedNotes : DailyNote [ ] = [ ] ;
147147
148- // Scan past notes
149148 for ( let i = 1 ; i <= days ; i ++ ) {
150149 const date = getDaysAgo ( i , ) ;
151150 const note = await loadDailyNote ( date , ) ;
152151 if ( ! note || ! note . content . trim ( ) ) continue ;
152+ const markdown = json2md ( parseJsonContent ( note . content , ) , ) ;
153+ if ( ! markdown . trim ( ) ) continue ;
153154
154- // 1. Extract unchecked tasks → move to today (preserving nesting)
155- const { tasks, cleaned, } = extractUncheckedTasks ( note . content , ) ;
155+ const { tasks, cleaned, } = extractUncheckedTasks ( markdown , ) ;
156156 if ( tasks . length > 0 ) {
157157 tasks . forEach ( ( t , ) => {
158158 if ( ! taskMap . has ( t . text , ) ) {
159159 taskMap . set ( t . text , t , ) ;
160160 }
161161 } , ) ;
162- modifiedNotes . push ( { ...note , content : cleaned , } , ) ;
162+ modifiedNotes . push ( { ...note , content : JSON . stringify ( md2json ( cleaned , ) , ) , } , ) ;
163163 }
164164
165- // 2. Find checked recurring tasks that are due again
166- const checkedRecurring = extractCheckedRecurringTasks ( note . content , ) ;
165+ const checkedRecurring = extractCheckedRecurringTasks ( markdown , ) ;
167166 for ( const taskText of checkedRecurring ) {
168167 const recurrence = parseRecurrence ( taskText , ) ! ;
169168 const nextDue = addDaysToDate ( date , recurrence . intervalDays , ) ;
@@ -175,19 +174,15 @@ export async function rolloverTasks(days: number = 30,): Promise<boolean> {
175174
176175 if ( taskMap . size === 0 ) return false ;
177176
178- // Save cleaned past notes (unchecked tasks removed)
179177 await Promise . all ( modifiedNotes . map ( ( note , ) => saveDailyNote ( note , ) ) , ) ;
180-
181- // Deduplicate against tasks already in today's note
182178 const todayNote = await loadDailyNote ( today , ) ;
183- const todayContent = todayNote ? .content ?? "" ;
184- const existingTasks = extractAllTaskTexts ( todayContent , ) ;
179+ const todayMarkdown = todayNote ? json2md ( parseJsonContent ( todayNote . content , ) , ) : "" ;
180+ const existingTasks = extractAllTaskTexts ( todayMarkdown , ) ;
185181 const newTasks = [ ...taskMap . values ( ) , ] . filter ( ( t , ) => ! existingTasks . has ( t . text , ) ) ;
186-
187182 if ( newTasks . length === 0 ) return false ;
188183
189- const updated = prependTasks ( todayContent , newTasks , ) ;
190- await saveDailyNote ( { date : today , content : updated , } , ) ;
191-
184+ const updated = prependTasks ( todayMarkdown , newTasks , ) ;
185+ const updatedJson = JSON . stringify ( md2json ( updated , ) , ) ;
186+ await saveDailyNote ( { date : today , content : updatedJson , city : todayNote ?. city , } , ) ;
192187 return true ;
193188}
0 commit comments