@@ -60,7 +60,10 @@ export class CardsService {
60
60
let deckName = "" ;
61
61
if ( parseFrontMatterEntry ( frontmatter , "cards-deck" ) ) {
62
62
deckName = parseFrontMatterEntry ( frontmatter , "cards-deck" ) ;
63
- } else if ( this . settings . folderBasedDeck && activeFile . parent ?. path !== "/" ) {
63
+ } else if (
64
+ this . settings . folderBasedDeck &&
65
+ activeFile . parent ?. path !== "/"
66
+ ) {
64
67
// If the current file is in the path "programming/java/strings.md" then the deck name is "programming::java"
65
68
// TODO parent might be undefined
66
69
deckName = activeFile . parent ! . path . split ( "/" ) . join ( "::" ) ;
@@ -191,68 +194,45 @@ export class CardsService {
191
194
frontmatter : FrontMatterCache ,
192
195
deckName : string
193
196
) : Promise < number | undefined > {
194
- if ( cardsToCreate . length ) {
195
- let insertedCards = 0 ;
196
- try {
197
- const ids = await this . anki . addCards ( cardsToCreate ) ;
198
- // Add IDs from response to Flashcard[]
199
- ids . map ( ( id : number , index : number ) => {
200
- cardsToCreate [ index ] . id = id ;
201
- } ) ;
202
-
203
- let total = 0 ;
204
- cardsToCreate . forEach ( ( card ) => {
205
- if ( card . id === null ) {
206
- new Notice (
207
- `Error, could not add: '${ card . initialContent } '` ,
208
- NOTICE_TIMEOUT
209
- ) ;
210
- } else {
211
- card . reversed ? ( insertedCards += 2 ) : insertedCards ++ ;
212
- }
213
- card . reversed ? ( total += 2 ) : total ++ ;
214
- } ) ;
197
+ if ( cardsToCreate . length === 0 ) return ;
215
198
216
- this . updateFrontmatter ( frontmatter , deckName ) ;
217
- this . writeAnkiBlocks ( cardsToCreate ) ;
199
+ let ids : number [ ] | undefined = undefined ;
200
+ try {
201
+ ids = await this . anki . addCards ( cardsToCreate ) ;
202
+ } catch ( err ) {
203
+ console . error ( err ) ;
204
+ throw Error ( "Error: Could not write cards on Anki" ) ;
205
+ }
218
206
219
- this . notifications . push (
220
- `Inserted successfully ${ insertedCards } /${ total } cards.`
207
+ ids . forEach ( ( id : number , index : number ) => ( cardsToCreate [ index ] . id = id ) ) ;
208
+
209
+ let cardsInserted = 0 ;
210
+ let cardsTotal = 0 ;
211
+ cardsToCreate . forEach ( ( card ) => {
212
+ cardsTotal += card . reversed ? 2 : 1 ;
213
+ if ( card . id !== null ) {
214
+ cardsInserted += card . reversed ? 2 : 1 ;
215
+ } else {
216
+ new Notice (
217
+ `Error, could not add: '${ card . initialContent } '` ,
218
+ NOTICE_TIMEOUT
221
219
) ;
222
- return insertedCards ;
223
- } catch ( err ) {
224
- console . error ( err ) ;
225
- Error ( "Error: Could not write cards on Anki" ) ;
226
220
}
227
- }
228
- }
221
+ } ) ;
229
222
230
- private updateFrontmatter ( frontmatter : FrontMatterCache , deckName : string ) {
231
- let newFrontmatter = "" ;
232
- const cardsDeckLine = `cards-deck: ${ deckName } \n` ;
233
- if ( frontmatter ) {
234
- const oldFrontmatter : string = this . file . substring (
235
- frontmatter . position . start . offset ,
236
- frontmatter . position . end . offset
237
- ) ;
238
- if ( ! oldFrontmatter . match ( this . regex . cardsDeckLine ) ) {
239
- newFrontmatter =
240
- oldFrontmatter . substring ( 0 , oldFrontmatter . length - 3 ) +
241
- cardsDeckLine +
242
- "---" ;
243
- this . totalOffset += cardsDeckLine . length ;
244
- this . file =
245
- newFrontmatter +
246
- this . file . substring (
247
- frontmatter . position . end . offset ,
248
- this . file . length + 1
249
- ) ;
250
- }
251
- } else {
252
- newFrontmatter = `---\n${ cardsDeckLine } ---\n\n` ;
253
- this . totalOffset += newFrontmatter . length ;
254
- this . file = newFrontmatter + this . file ;
255
- }
223
+ // update frontmatter
224
+ // TODO this might not be needed?
225
+ const activeFile = this . app . workspace . getActiveFile ( ) ! ;
226
+ this . app . fileManager . processFrontMatter ( activeFile , ( frontmatter ) => {
227
+ frontmatter [ "cards-deck" ] = deckName ;
228
+ } ) ;
229
+
230
+ this . writeAnkiBlocks ( cardsToCreate ) ;
231
+
232
+ this . notifications . push (
233
+ `Inserted successfully ${ cardsInserted } /${ cardsTotal } cards.`
234
+ ) ;
235
+ return cardsInserted ;
256
236
}
257
237
258
238
private writeAnkiBlocks ( cardsToCreate : Card [ ] ) {
0 commit comments