Skip to content

Commit c6ed384

Browse files
feature: add 'populate with book ID' command
1 parent d4b821d commit c6ed384

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/main.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,42 @@ export default class ReadwisePlugin extends Plugin {
683683
}
684684
}
685685
});
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+
});
686722
this.registerMarkdownPostProcessor((el, ctx) => {
687723
if (!ctx.sourcePath.startsWith(this.settings.readwiseDir)) {
688724
return;

0 commit comments

Comments
 (0)