@@ -25,7 +25,7 @@ export default class AnkiLink extends Plugin {
2525 "Sample" ,
2626 ( evt : MouseEvent ) => {
2727 // Called when the user clicks the icon.
28- const numStr = this . parse ( ) . then ( ( n ) => n . toString ( ) ) ;
28+ const numStr = this . syncNotes ( ) . then ( ( n ) => n . toString ( ) ) ;
2929 numStr . then (
3030 ( n ) => new Notice ( n ) ,
3131 ( e ) => console . error ( e ) ,
@@ -59,24 +59,23 @@ export default class AnkiLink extends Plugin {
5959 await this . saveData ( this . settings ) ;
6060 }
6161
62- async parse ( ) : Promise < number > {
62+ async syncNotes ( ) : Promise < number > {
6363 const { vault } = this . app ;
6464
6565 const markdownFiles = vault . getMarkdownFiles ( ) ;
66+ await this . addMissingDecks ( ) ;
6667
67- const deckNamesRes = await sendDeckNamesRequest ( ) ;
68- if ( deckNamesRes . error ) throw new Error ( `AnkiConnect: ${ deckNamesRes . error } ` )
69- const decks = deckNamesRes . result ;
70- if ( ! decks . contains ( TARGET_DECK ) ) {
71- const createDeckRes = await sendCreateDeckRequest ( TARGET_DECK ) ;
72- if ( createDeckRes . error ) throw new Error ( `AnkiConnect: ${ createDeckRes . error } ` )
73- }
7468 let totalAdded = 0 ;
69+ const fileLines = await this . readFiles ( vault , markdownFiles ) ;
70+ const fileNoteData = await this . readNoteData ( fileLines ) ;
7571 for ( const file of markdownFiles ) {
76- const lines = await this . readFile ( vault , file ) ;
7772 let linesModified = false ;
78- const noteDataList = this . parseDocument ( lines ) ;
79- for ( const noteData of noteDataList ) {
73+ const notes = fileNoteData . get ( file ) ;
74+ const lines = fileLines . get ( file ) ;
75+ if ( ! notes || ! lines ) {
76+ continue ;
77+ }
78+ for ( const noteData of notes ) {
8079 if ( noteData . id == undefined ) {
8180 noteData . id = await this . sendNote ( noteData . note ) ;
8281 lines [ noteData . index ] = `> [!flashcard] %%{noteId}%% ${ noteData . note . fields . Front } ` ;
@@ -87,14 +86,38 @@ export default class AnkiLink extends Plugin {
8786 }
8887 }
8988 if ( linesModified ) {
89+ fileLines . set ( file , lines ) ;
9090 await vault . modify ( file , lines . join ( "\n" ) ) ;
9191 }
9292 }
9393 return totalAdded ;
9494 }
9595
96- private async readFile ( vault : Vault , file : TFile ) : Promise < string [ ] > {
97- return ( await vault . read ( file ) ) . split ( "\n" ) ;
96+ private async readNoteData ( fileLines : Map < TFile , string [ ] > ) {
97+ const fileNoteData = new Map < TFile , ParsedNoteData [ ] > ( ) ;
98+ for ( const [ file , lines ] of fileLines ) {
99+ fileNoteData . set ( file , this . parseDocument ( lines ) )
100+ }
101+ return fileNoteData ;
102+ }
103+
104+ private async readFiles ( vault : Vault , files : TFile [ ] ) {
105+ const fileLines = new Map < TFile , string [ ] > ( ) ;
106+ for ( const file of files ) {
107+ const lines = ( await vault . read ( file ) ) . split ( "\n" ) ;
108+ fileLines . set ( file , lines ) ;
109+ }
110+ return fileLines ;
111+ }
112+
113+ private async addMissingDecks ( ) {
114+ const deckNamesRes = await sendDeckNamesRequest ( ) ;
115+ if ( deckNamesRes . error ) throw new Error ( `AnkiConnect: ${ deckNamesRes . error } ` )
116+ const decks = deckNamesRes . result ;
117+ if ( ! decks . contains ( TARGET_DECK ) ) {
118+ const createDeckRes = await sendCreateDeckRequest ( TARGET_DECK ) ;
119+ if ( createDeckRes . error ) throw new Error ( `AnkiConnect: ${ createDeckRes . error } ` )
120+ }
98121 }
99122
100123 /**
0 commit comments