@@ -19,40 +19,51 @@ interface ParsedNoteData {
1919
2020interface NoteSyncResult {
2121 added : number ;
22+ modified : number ;
2223 linesModified : boolean ;
2324 lines : string [ ] ;
2425}
2526
26- export async function syncVaultNotes ( app : App ) : Promise < number > {
27+ export interface SyncSummary {
28+ added : number ;
29+ modified : number ;
30+ }
31+
32+ export async function syncVaultNotes ( app : App ) : Promise < SyncSummary > {
2733 const markdownFiles = app . vault . getMarkdownFiles ( ) ;
2834 await addMissingDecks ( ) ;
2935
3036 let totalAdded = 0 ;
37+ let totalModified = 0 ;
3138 for ( const file of markdownFiles ) {
32- totalAdded += await syncSingleFile ( app , file ) ;
39+ const result = await syncSingleFile ( app , file ) ;
40+ totalAdded += result . added ;
41+ totalModified += result . modified ;
3342 }
34- return totalAdded ;
43+ return { added : totalAdded , modified : totalModified } ;
3544}
3645
37- async function syncSingleFile ( app : App , file : TFile ) : Promise < number > {
46+ async function syncSingleFile ( app : App , file : TFile ) : Promise < SyncSummary > {
3847 const originalLines = ( await app . vault . read ( file ) ) . split ( "\n" ) ;
3948 const notesData = parseDocument ( originalLines ) ;
40- if ( notesData . length === 0 ) return 0 ;
49+ if ( notesData . length === 0 ) return { added : 0 , modified : 0 } ;
4150
4251 let totalAdded = 0 ;
52+ let totalModified = 0 ;
4353 let linesModified = false ;
4454 let lines = originalLines ;
4555 for ( const noteData of notesData ) {
4656 const result = await syncSingleNote ( noteData , lines ) ;
4757 totalAdded += result . added ;
58+ totalModified += result . modified ;
4859 linesModified = linesModified || result . linesModified ;
4960 lines = result . lines ;
5061 }
5162
5263 if ( linesModified ) {
5364 await app . vault . modify ( file , lines . join ( "\n" ) ) ;
5465 }
55- return totalAdded ;
66+ return { added : totalAdded , modified : totalModified } ;
5667}
5768
5869async function syncSingleNote ( noteData : ParsedNoteData , lines : string [ ] ) : Promise < NoteSyncResult > {
@@ -70,15 +81,16 @@ async function syncSingleNote(noteData: ParsedNoteData, lines: string[]): Promis
7081 const ankiFields = ankiNote . fields ;
7182 if ( obsidianFields . Front !== ankiFields . Front . value || obsidianFields . Back !== ankiFields . Back . value ) {
7283 await updateNoteById ( ankiNote . noteId , obsidianFields ) ;
84+ return { added : 0 , modified : 1 , linesModified : false , lines } ;
7385 }
74- return { added : 0 , linesModified : false , lines } ;
86+ return { added : 0 , modified : 0 , linesModified : false , lines } ;
7587}
7688
7789async function createAndWriteNoteId ( noteData : ParsedNoteData , lines : string [ ] ) : Promise < NoteSyncResult > {
7890 const newId = await sendNote ( noteData . note ) ;
7991 const updatedLines = [ ...lines ] ;
8092 updatedLines [ noteData . index ] = `> [!flashcard] %%${ newId } %% ${ noteData . note . fields . Front } ` ;
81- return { added : 1 , linesModified : true , lines : updatedLines } ;
93+ return { added : 1 , modified : 0 , linesModified : true , lines : updatedLines } ;
8294}
8395
8496async function addMissingDecks ( ) {
0 commit comments