import moment from "moment" ; import { TFile , FileSystemAdapter } from "obsidian" ; export class DailyNotesFolderMissingError extends Error { } export async function getRemainingTasks ( note ) { if ( ! note ) { return 0 ; } const { vault } = window . app ; const fileContents = await vault . cachedRead ( note ) ; const matchLength = ( fileContents . match ( /(-|\*) (\[(\s|x|X|\\|\-|\>|D|\?|\/|\+|R|\!|i|B|P|C)\]\s)?/g ) || [ ] ) . length ; return matchLength ; } export async function getTasksFromFiles ( file , dailyEvents ) { if ( ! file ) { return [ ] ; } const { vault } = window . app ; const Tasks = await getRemainingTasks ( file ) ; if ( Tasks ) { let fileContents = await vault . cachedRead ( file ) ; let fileLines = getAllLinesFromFile ( fileContents ) ; for ( let i = 0 ; i < fileLines . length ; i ++ ) { const line = fileLines [ i ] ; if ( line . length === 0 ) continue ; const rawText = extractTextFromTodoLine ( line ) ; const dueDate = getDueDateFromBulletLine ( line ) ; const scheduleDate = getScheduledDateFromBulletLine ( line ) ; const startDate = getStartDateFromBulletLine ( line ) ; if ( dueDate && scheduleDate ) { if ( lineIsValidTodoEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'TODO' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidDoneEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'DONE' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidAnotherEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'ANOTHER' , file : file , originalText : line , lineNum : i , } ) ; } else { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'JOURNAL' , file : file , originalText : line , lineNum : i , } ) ; } } else if ( dueDate && startDate ) { if ( lineIsValidTodoEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'TODO' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidDoneEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'DONE' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidAnotherEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'ANOTHER' , file : file , originalText : line , lineNum : i , } ) ; } else { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'JOURNAL' , file : file , originalText : line , lineNum : i , } ) ; } } else if ( dueDate && ! startDate && ! scheduleDate ) { if ( lineIsValidTodoEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'TODO' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidDoneEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'DONE' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidAnotherEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'ANOTHER' , file : file , originalText : line , lineNum : i , } ) ; } else { dailyEvents . push ( { id : i , title : rawText , start : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'JOURNAL' , file : file , originalText : line , lineNum : i , } ) ; } } else if ( scheduleDate && ! dueDate && ! startDate ) { if ( lineIsValidTodoEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'TODO' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidDoneEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'DONE' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidAnotherEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'ANOTHER' , file : file , originalText : line , lineNum : i , } ) ; } else { dailyEvents . push ( { id : i , title : rawText , start : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'JOURNAL' , file : file , originalText : line , lineNum : i , } ) ; } } else if ( scheduleDate && startDate ) { if ( lineIsValidTodoEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'TODO' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidDoneEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'DONE' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidAnotherEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getScheduledDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'ANOTHER' , file : file , originalText : line , lineNum : i , } ) ; } else { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getDueDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'JOURNAL' , file : file , originalText : line , lineNum : i , } ) ; } } else if ( startDate && ! dueDate && ! scheduleDate ) { if ( lineIsValidTodoEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'TODO' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidDoneEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'DONE' , file : file , originalText : line , lineNum : i , } ) ; } else if ( lineIsValidAnotherEvent ( line ) ) { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'ANOTHER' , file : file , originalText : line , lineNum : i , } ) ; } else { dailyEvents . push ( { id : i , title : rawText , start : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , end : moment ( getStartDateFromBulletLine ( line ) , "YYYY-MM-DD" ) . toDate ( ) , resourceId : i , status : 'JOURNAL' , file : file , originalText : line , lineNum : i , } ) ; } } } fileLines = null ; fileContents = null ; } } export function getPath ( filePath ) { const { vault } = window . app ; if ( vault . adapter instanceof FileSystemAdapter ) { return ( vault . adapter as FileSystemAdapter ) . getFullPath ( filePath ) ; } throw new Error ( 'cannot determine base path' ) ; } export async function outputTasksResults ( ) { const events = [ ] ; const { vault } = window . app ; const allFiles = vault . getMarkdownFiles ( ) ; for ( let i = 0 ; i < allFiles . length ; i ++ ) { if ( allFiles [ i ] instanceof TFile ) { await getTasksFromFiles ( allFiles [ i ] , events ) ; } } return events ; } const getAllLinesFromFile = ( cache ) => cache . split ( /\r?\n/ ) ; const lineIsValidTodoEvent = ( line ) => { return /^\s*(\-|\*)\s\[ \]\s?(.*)$/ . test ( line ) ; } ; const lineIsValidDoneEvent = ( line ) => { return /^\s*(\-|\*)\s\[x\]\s?(.*)$/ . test ( line ) ; } ; const lineIsValidAnotherEvent = ( line ) => { return /^\s*(\-|\*)\s\[(X|\\|\-|\>|D|\?|\/|\+|R|\!|i|B|P|C)\]\s?(.*)$/ . test ( line ) ; } ; const lineContainsTime = ( line ) => { return /^\s*(\-|\*)\s(\[(\s|x|X|\\|\-|\>|D|\?|\/|\+|R|\!|i|B|P|C)\]\s)?(\<time\>)?\d{1,2}\:\d{2}(.*)$/ . test ( line ) ; } ; const extractTextFromTodoLine = ( line ) => /^(\s*)?(\-|\*)\s\[(\s|x|X|\\|\-|\>|D|\?|\/|\+|R|\!|i|B|P|C)\]\s?(\<time\>)?((\d{1,2})\:(\d{2}))?(\<\/time\>)?(.+?)(?=(⏫|🛫|📅|📆|(@{)|⏳|⌛|(\[due\:\:)|(\[created\:\:)))/ . exec ( line ) ?. [ 9 ] ; const getStartDateFromBulletLine = ( line ) => /\s(🛫|(\[created\:\:)|➕) ?(\d{4}-\d{2}-\d{2})(\])?/ . exec ( line ) ?. [ 3 ] ; const getDueDateFromBulletLine = ( line ) => /\s(📅|📆|(@{)|(\[due\:\:)) ?(\d{4}-\d{2}-\d{2})(\])?/ . exec ( line ) ?. [ 4 ] ; const getScheduledDateFromBulletLine = ( line ) => /\s(⏳|⌛) ?(\d{4}-\d{2}-\d{2})/ . exec ( line ) ?. [ 2 ] ; const tagFromLine = ( line ) => /\s(#\S{0,})\s/ ;