@@ -683,6 +683,42 @@ export default class ReadwisePlugin extends Plugin {
683
683
}
684
684
}
685
685
} ) ;
686
+ this . addCommand ( {
687
+ id : 'readwise-official-frontmatter-bookid-file' ,
688
+ name : 'Populate frontmatter with book ID in this document' ,
689
+ checkCallback : ( checking : boolean ) => {
690
+ const activeFile = this . app . workspace . getActiveFile ( ) ;
691
+ // blank tab returns null for getActiveFile
692
+ if ( ! activeFile ) return false ;
693
+
694
+ const bookId = this . getFileBookId ( activeFile ) ;
695
+ const isRWfile = ! ! bookId ;
696
+ const cache = this . app . metadataCache . getFileCache ( activeFile ) ;
697
+ const existingBookId = cache . frontmatter ?. [ this . settings . frontmatterBookIdKey ] ;
698
+
699
+ if ( checking ) {
700
+ // don't show the command if the frontmatter key is not set
701
+ if ( ! this . settings . frontmatterBookIdKey ) {
702
+ return false
703
+ }
704
+
705
+ // don't show the command if the file cache is not available
706
+ if ( ! cache ) return false ;
707
+
708
+ // don't show the command if the book ID is already set in the frontmatter
709
+ if ( existingBookId ) return false ;
710
+
711
+ // finally,
712
+ // show the command if the file is a Readwise file
713
+ return isRWfile ;
714
+ }
715
+
716
+ this . notice ( "Populating document frontmatter with book ID..." , true ) ;
717
+
718
+ // set the book ID in the frontmatter
719
+ this . writeBookIdToFrontmatter ( activeFile , bookId ) ;
720
+ }
721
+ } ) ;
686
722
this . registerMarkdownPostProcessor ( ( el , ctx ) => {
687
723
if ( ! ctx . sourcePath . startsWith ( this . settings . readwiseDir ) ) {
688
724
return ;
0 commit comments