Skip to content

Commit

Permalink
Add automatic sync and overwrite options
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyw committed Jul 13, 2023
1 parent 7548b79 commit 717db15
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# vscode
.vscode
.vscode
.devcontainer

# Intellij
*.iml
Expand Down
4 changes: 4 additions & 0 deletions const/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface BooksidianSettings {
fileName: string;
frontmatterDictionary: CurrentYAML;
bodyString: string;
frequency: string;
overwrite: boolean;
}

export interface CurrentYAML {
Expand All @@ -18,4 +20,6 @@ export const DEFAULT_SETTINGS: BooksidianSettings = {
goodreadsShelves: "currently-reading",
frontmatterDictionary: {},
bodyString: "# {{title}}\n\nauthor::[[{{author}}]]",
frequency: "0", // manual
overwrite: false,
};
15 changes: 15 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BooksidianSettings, DEFAULT_SETTINGS } from "const/settings";

export default class Booksidian extends Plugin {
settings: BooksidianSettings;
scheduleInterval: null | number = null;

async onload() {
await this.loadSettings();
Expand Down Expand Up @@ -51,4 +52,18 @@ export default class Booksidian extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}

async configureSchedule() {
const minutes = parseInt(this.settings.frequency);
const milliseconds = minutes * 60 * 1000; // minutes * seconds * milliseconds
console.log('Readwise Official plugin: setting interval to ', milliseconds, 'milliseconds');
window.clearInterval(this.scheduleInterval);
this.scheduleInterval = null;
if (!milliseconds) {
// we got manual option
return;
}
this.scheduleInterval = window.setInterval(() => this.updateLibrary(), milliseconds);
this.registerInterval(this.scheduleInterval);
}
}
16 changes: 11 additions & 5 deletions src/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ export class Book {

public async createFile(book: Book, path: string): Promise<void> {
const fileName = this.getBody(this.plugin.settings.fileName);
const fullName = `${path}/${fileName}.md`;

try {
await this.plugin.app.vault.create(
`${path}/${fileName}.md`,
book.getContent()
);
const fs = this.plugin.app.vault.adapter;

if (fs.exists(fullName) && !this.plugin.settings.overwrite) {
return;
}

// Either create new file or overwrite one that exists.
await fs.write(fullName, book.getContent());
} catch (error) {
console.log(book.getTitle() + " already exists!");
console.log(`Error writing ${fullName}`, error);
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@ export class Settings extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName('Configure resync frequency')
.setDesc('If not set to manual, Booksidian will resync with Goodreads RSS at configured interval')
.addDropdown(dropdown => {
dropdown.addOption("0", "Manual");
dropdown.addOption("60", "Every 1 hour");
dropdown.addOption((12 * 60).toString(), "Every 12 hours");
dropdown.addOption((24 * 60).toString(), "Every 24 hours");

dropdown.setValue(this.plugin.settings.frequency);

dropdown.onChange((newValue) => {
this.plugin.settings.frequency = newValue;
this.plugin.saveSettings();

this.plugin.configureSchedule();
})
});

new Setting(containerEl)
.setName('Overwrite')
.setDesc('When syncing with Goodreads, overwrite existing notes. Modifications to notes will be lost, but changes from Goodreads will now be picked up.')
.addToggle(toggle => {
toggle.setValue(this.plugin.settings.overwrite);

toggle.onChange((newValue) => {
this.plugin.settings.overwrite = newValue;
this.plugin.saveSettings();
})
})

containerEl.createEl("h3", { text: "Body" });
containerEl.createEl("p", {
text: "You can specify the content of the book-note by using {{placeholders}}. You can see the full list of placeholders in the dropdown of the frontmatter. You can choose the frontmatter placeholders you'd like and apply specific formatting to each of them.",
Expand Down

0 comments on commit 717db15

Please sign in to comment.