Skip to content

Commit

Permalink
Merge pull request #40 from Edo78/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Edo78 authored Jan 30, 2022
2 parents 092fe6f + 0a77ea7 commit 1ef0654
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 33 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ There ara four main settings:
- `Keep in sync` that define if the plugin **should** keep the notes in sync with KOReader importing them again (see [sync](#sync))
- `Create a folder for each book` if you are a fan of folders enabling this setting the **new notes** will be created in a subfolder named as the book itself

### Danger Zone

This area contains settings that can be useful in a very few edga cases and can be dangerous in a day to day usage.

- `Enable reset of imported notes` enable a one shot execution of the [command](#commands) `Reset Sync List`

### View configuration
The plugin use [Eta.js](https://eta.js.org/) as template engine to create the body of the note (the same used from the plugin [Templater](https://github.com/SilentVoid13/Templater)).
The default template is pretty minimal
Expand Down Expand Up @@ -59,6 +65,7 @@ Once the plugin is configured properly you can plug the device with KOReader and
### Commands
There are five commands:
- `Sync` it's the same as clicking on the plugin's icon, it's trigger the sync of the notes
- `Reset Sync List` empty the list of imported notes (see [Danger Zone](#danger-zone)). Always try to retrieve the deleted notes from trash before using this command because all the rightfully discarded notes will be imported again. This command will also disable itself so you have to enable in the settings again if you wish to use it again.
- `Mark this note as Edited` set the frontmatter propery `yet_to_be_edited` to `false` (see [Note editing](#note-editing))
- `Mark this note as NOT Edited` set the frontmatter propery `yet_to_be_edited` to `true` (see [Note editing](#note-editing))
- `Enable Sync for this note` set the frontmatter propery `keep_in_sync` to `true` (see [sync](#sync))
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-koreader-plugin",
"name": "KOReader Highlights",
"version": "0.4.0",
"version": "0.4.1",
"minAppVersion": "0.13.19",
"description": "This is a plugin for Obsidian. This plugin syncs highlights and notes taken in KOReader.",
"author": "Federico \"Edo\" Granata",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-koreader-plugin",
"version": "0.4.0",
"version": "0.4.1",
"description": "This is a plugin for Obsidian. This plugin syncs highlights and notes taken in KOReader.",
"main": "main.js",
"scripts": {
Expand Down
111 changes: 83 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ interface KOReaderSettings {
customTemplate: boolean;
templatePath?: string;
createDataviewQuery: boolean;
importedNotes: { [key: string]: boolean };
enbleResetImportedNotes: boolean;
}

const DEFAULT_SETTINGS: KOReaderSettings = {
importedNotes: {},
enbleResetImportedNotes: false,
keepInSync: false,
aFolderForEachBook: false,
customTemplate: false,
Expand Down Expand Up @@ -151,6 +155,22 @@ export default class KOReader extends Plugin {
},
});

this.addCommand({
id: 'obsidian-koreader-plugin-reset-sync-list',
name: 'Reset Sync List',
checkCallback: (checking: boolean) => {
if (this.settings.enbleResetImportedNotes) {
if (!checking) {
this.resetSyncList();
this.settings.enbleResetImportedNotes = false;
this.saveSettings();
}
return true;
}
return false;
},
});

this.addSettingTab(new KoreaderSettingTab(this.app, this));
}

Expand All @@ -164,6 +184,11 @@ export default class KOReader extends Plugin {
await this.saveData(this.settings);
}

private async resetSyncList() {
this.settings.importedNotes = {};
await this.saveSettings();
}

private getObjectProperty(object: { [x: string]: any }, path: string) {
if (path === undefined || path === null) {
return object;
Expand Down Expand Up @@ -204,6 +229,7 @@ export default class KOReader extends Plugin {
const val = this.getObjectProperty(data, property);
const note = matter.stringify(content, data);
view.setViewData(note, false);
view.requestSave();
}

private async createNote(note: {
Expand Down Expand Up @@ -297,7 +323,7 @@ Page: <%= it.page %>
managed_title: managedBookTitle,
},
};
console.log('frontMatter', frontMatter);

const defaultTemplate = `# Title: <%= it.title %>
\`\`\`dataviewjs
Expand Down Expand Up @@ -360,9 +386,6 @@ return n['koreader-sync'] && n['koreader-sync'].type == 'koreader-sync-note' &&
this.settings.createDataviewQuery &&
!this.app.vault.getAbstractFileByPath(`${path}/${managedBookTitle}.md`)
) {
console.log(
`Creating dataview query in ${path}/${managedBookTitle}.md`
);
this.createDataviewQueryPerBook({
path,
managedBookTitle,
Expand All @@ -371,41 +394,57 @@ return n['koreader-sync'] && n['koreader-sync'].type == 'koreader-sync-note' &&
}

for (const bookmark in data[book].bookmarks) {
const updateNote: boolean = false;
const uniqueId = crypto
.createHash('md5')
.update(
`${data[book].title} - ${data[book].authors} - ${data[book].bookmarks[bookmark].pos0} - ${data[book].bookmarks[bookmark].pos1}`
)
.digest('hex');

if (Object.keys(existingNotes).includes(uniqueId)) {
// if the user wants to keep the note in sync, we delete only if the note is not yet_to_be_edited
if (
existingNotes[uniqueId].keep_in_sync &&
existingNotes[uniqueId].yet_to_be_edited
) {
this.app.vault.delete(existingNotes[uniqueId].note);
} else {
continue;
// if the note is not yet imported, we create it
if (!Object.keys(this.settings.importedNotes).includes(uniqueId)) {
if (!Object.keys(existingNotes).includes(uniqueId)) {
const { content, frontmatterData, notePath } =
await this.createNote({
path,
uniqueId,
bookmark: data[book].bookmarks[bookmark],
managedBookTitle,
book: data[book],
keepInSync: this.settings.keepInSync,
});

this.app.vault.create(
`${notePath}.md`,
matter.stringify(content, frontmatterData)
);
}
}

const { content, frontmatterData, notePath } = await this.createNote({
path,
uniqueId,
bookmark: data[book].bookmarks[bookmark],
managedBookTitle,
book: data[book],
keepInSync:
existingNotes[uniqueId]?.keep_in_sync || this.settings.keepInSync,
});
this.settings.importedNotes[uniqueId] = true;
// else if the note exists and keep_in_sync is true and yet_to_be_edited is false, we update it
} else if (
Object.keys(existingNotes).includes(uniqueId) &&
existingNotes[uniqueId].keep_in_sync &&
!existingNotes[uniqueId].yet_to_be_edited
) {
const note = existingNotes[uniqueId].note as TFile;
const { content, frontmatterData, notePath } = await this.createNote({
path,
uniqueId,
bookmark: data[book].bookmarks[bookmark],
managedBookTitle,
book: data[book],
keepInSync: existingNotes[uniqueId]?.keep_in_sync,
});

this.app.vault.create(
`${notePath}.md`,
matter.stringify(content, frontmatterData)
);
this.app.vault.modify(
note,
matter.stringify(content, frontmatterData)
);
}
}
}
await this.saveSettings();
}
}

Expand Down Expand Up @@ -655,5 +694,21 @@ class KoreaderSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);

containerEl.createEl('h2', { text: 'DANGER ZONE' });

new Setting(containerEl)
.setName('Enable reset of imported notes')
.setDesc(
"Enable the command to empty the list of imported notes in case you can't recover from the trash one or more notes"
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.enbleResetImportedNotes)
.onChange(async (value) => {
this.plugin.settings.enbleResetImportedNotes = value;
await this.plugin.saveSettings();
})
);
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"0.2.1": "0.12.0",
"0.2.2": "0.12.0",
"0.3.0": "0.13.19",
"0.4.0": "0.13.19"
"0.4.0": "0.13.19",
"0.4.1": "0.13.19"
}

0 comments on commit 1ef0654

Please sign in to comment.