@@ -302,6 +302,56 @@ export default class ReadwisePlugin extends Plugin {
302302 } ;
303303 }
304304
305+ /** helper to extract all book IDs from frontmatter in the readwiseDir ("base folder"),
306+ perfectly matching them to the document they came from */
307+ async extractBookIDs ( ) {
308+ console . log ( 'Readwise Official plugin: extracting book IDs from frontmatter...' ) ;
309+ const bookIDs : { [ bookID : string ] : string } = { } ;
310+ if ( ! this . settings . frontmatterBookIdKey ) {
311+ console . log ( 'Readwise Official plugin: no frontmatter key defined, skipping extraction' ) ;
312+ return bookIDs ;
313+ }
314+
315+ const files = this . app . vault . getMarkdownFiles ( ) ;
316+ for ( const file of files ) {
317+ if ( file . path . startsWith ( this . settings . readwiseDir ) ) {
318+ const cache = this . app . metadataCache . getFileCache ( file ) ;
319+ // skip if there's no cache
320+ if ( ! cache ) continue ;
321+
322+ const frontmatter = cache . frontmatter ;
323+ // skip if there's no frontmatter
324+ if ( ! frontmatter ) continue ;
325+
326+ const bookID = frontmatter [ this . settings . frontmatterBookIdKey ] ;
327+ if ( bookID ) bookIDs [ file . path ] = bookID ;
328+ }
329+ }
330+
331+ return bookIDs ;
332+ }
333+
334+ async getRWfiles ( ) {
335+ let jsonBookIDs = this . settings . booksIDsMap ;
336+ const frontmatterBookIDs = await this . extractBookIDs ( ) ;
337+
338+ // merge the two objects, with frontmatterBookIDs taking precedence
339+ return { ...jsonBookIDs , ...frontmatterBookIDs } ;
340+ }
341+
342+ getFileBookId ( file : TFile ) : string {
343+ const frontmatterBookId = this . app . metadataCache . getFileCache ( file ) . frontmatter ?. [ this . settings . frontmatterBookIdKey ] ;
344+ // type narrowing from any -> string
345+ if ( frontmatterBookId && typeof frontmatterBookId !== 'string' ) {
346+ throw new Error ( `Readwise Official plugin: bookId not a string` ) ;
347+ }
348+
349+ const jsonBookId = this . settings . booksIDsMap [ file . path ] ;
350+
351+ // prefer book id from frontmatter if it exists
352+ return frontmatterBookId || jsonBookId ;
353+ }
354+
305355 async downloadArchive ( exportID : number , buttonContext : ButtonComponent ) : Promise < void > {
306356 let artifactURL = `${ baseURL } /api/download_artifact/${ exportID } ` ;
307357 if ( exportID <= this . settings . lastSavedStatusID ) {
@@ -461,8 +511,8 @@ export default class ReadwisePlugin extends Plugin {
461511 await this . saveSettings ( ) ;
462512 }
463513
464- reimportFile ( vault : Vault , fileName : string ) {
465- const bookId = this . settings . booksIDsMap [ fileName ] ;
514+ reimportFile ( vault : Vault , file : TFile ) {
515+ const bookId = this . getFileBookId ( file ) ;
466516 try {
467517 fetch (
468518 `${ baseURL } /api/refresh_book_export` ,
@@ -476,7 +526,7 @@ export default class ReadwisePlugin extends Plugin {
476526 let booksToRefresh = this . settings . booksToRefresh ;
477527 this . settings . booksToRefresh = booksToRefresh . filter ( n => ! [ bookId ] . includes ( n ) ) ;
478528 this . saveSettings ( ) ;
479- vault . delete ( vault . getAbstractFileByPath ( fileName ) ) ;
529+ vault . delete ( vault . getAbstractFileByPath ( file . path ) ) ;
480530 this . startSync ( ) ;
481531 } else {
482532 this . notice ( "Failed to reimport. Please try again" , true ) ;
@@ -557,8 +607,8 @@ export default class ReadwisePlugin extends Plugin {
557607 id : 'readwise-official-reimport-file' ,
558608 name : 'Delete and reimport this document' ,
559609 checkCallback : ( checking : boolean ) => {
560- const activeFilePath = this . app . workspace . getActiveFile ( ) . path ;
561- const isRWfile = activeFilePath in this . settings . booksIDsMap ;
610+ const activeFile = this . app . workspace . getActiveFile ( ) ;
611+ const isRWfile = ! ! this . getFileBookId ( this . app . workspace . getActiveFile ( ) ) ;
562612 if ( checking ) {
563613 return isRWfile ;
564614 }
@@ -585,12 +635,12 @@ export default class ReadwisePlugin extends Plugin {
585635 modal . close ( ) ;
586636 } ) ;
587637 confirmBtn . onClickEvent ( ( ) => {
588- this . reimportFile ( this . app . vault , activeFilePath ) ;
638+ this . reimportFile ( this . app . vault , activeFile ) ;
589639 modal . close ( ) ;
590640 } ) ;
591641 modal . open ( ) ;
592642 } else {
593- this . reimportFile ( this . app . vault , activeFilePath ) ;
643+ this . reimportFile ( this . app . vault , activeFile ) ;
594644 }
595645 }
596646 } ) ;
0 commit comments