Edo78 / obsidian-koreader-sync

Obsidian.md plugin to sync highlights/notes from koreader
MIT License
76 stars 7 forks source link

Obsidian KOReader Plugin

Sync KOReader notes in your Obsidian vault. The KOReader device must be connected to the device running obsidian to let the plugin scan through it's files.

In the beginning of each note there a series of YAML data knwon as Frontmatter. Those data are mainly used by the plugin itself (you can use them as shown in dataview examples) but messing with them will cause unexpected behaviour so use the provided commands to properly interact with them.

When you're comfy reading your notes in obsidian think about how useful is this plugin to you and express your gratitude with a tweet or with a coffee :coffee:

Twitter URL Buy Me A Coffee

Configuration

There ara four main settings:

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.

View configuration

The plugin use Eta.js as template engine to create the body of the note (the same used from the plugin Templater). The default template is pretty minimal

## Title: [[<%= it.bookPath %>|<%= it.title %>]]

### by: [[<%= it.authors %>]]

### Chapter: <%= it.chapter %>

Page: <%= it.page %>

**==<%= it.highlight %>==**

<%= it.text %>

In the View settings section you can found the the option to use a custom template. If you chose to do so you must create a .md file in the vault and write your template in it (I suggest to copy the default in it as a starting point) and write the path in Template file

The template receive the following arguments:

Book view configuration

The default template is minimal but complex

# Title: <%= it.data.title %>

<progress value="<%= it.metadata.percent_finished %>" max="100"> </progress>
```dataviewjs
const title = dv.current()['koreader-sync'].metadata.managed_title
dv.pages().where(n => {
return n['koreader-sync'] && n['koreader-sync'].type == 'koreader-sync-note' && n['koreader-sync'].metadata.managed_book_title == title
}).sort(p => p['koreader-sync'].data.page).forEach(p => dv.paragraph(dv.fileLink(p.file.name, true), {style: 'test-css'}))
```

The core of this template is a js dataview embedded query. Don't mess with it if you don't know what you are doing (I don't because I barely know Dataview).

The template receive exactly the same data you can see in the frontmatter. If it's not there you can't use it but you can create an issue asking for it.

Dataview embedded

Besides a native support for Dataview (look at the example) the plugin let the user chose to automatically create a note for each book with a dataview query inside. The note is created in the same folder of the notes of the book but can be moved and renamed and Obsidian will take care of updating the links. To use this feature Dataview needs to be installed and its Enable JavaScript Queries must be enabled. The query itself will embed the single notes and a CSS will hide every h2 and h3 tags (with the default template this will hide the title, the author and the chapter).

ATTENTION: this feature require at least Obsidian v0.13.19 but there is a glitch that sometimes show only the filename of the notes instead of their contents. Try to close the note and open it again (sorry, not my fault)

Usage

Once the plugin is configured properly you can plug the device with KOReader and click on the icon with two documents and the tooltip Sync your KOReader highlights. The plugin should propmplty create a single file for each note. The plugin should take care of automatically detect when you update the text of the note itself and update the frontmatter properties accordingly.

Commands

NOTE: if a command is suppose to set a frontmatter property equal to a certain value then it will be shown only if the open note has such property with a different value.

There are five commands:

Note editing

When you edit a note you should avoit to change the frontmatter at all. Since version 0.6.0 the plugin itself should be able detect any changes to the note and to update:

If you want to discard your manual edits you can use the command Mark this note as NOT Edited and overwrite it at the next sync. If you change something beside the text itself (eg. the chapter, the title of the book, etc) you must use the Mark this note as Edited to made the plugin aware of your changes (this should not be necessary in future releases)

It's easier/safer to use the proper commands instead of manually editing the frontmatter

Sync

WARNING Sync works by deleting a note and creating it again from KOReader. Anything added or updated (in Obsidian) will be lost like tears in rain. Consider yourself warned.

The syncing process rely on two property defined in the frontmatter metadata:

Both needs to be true for the note to be synced.

keep_in_sync can be controlled at global level through the setting Keep in sync or in each note while yet_to_be_edited is set to true when the note is imported from KOReader and can only be manually changed in the note itself.

The default value for keep_in_sync is false so the default behaviour is that once a note is in obsidian it will never be synced again.

If you modify your notes in KOReader and want them to be synced in obsidian you have to enable the Keep in sync setting OR use the proper commands to change the keep_in_sync frontmatter of a specific note from false to true and if the yet_to_be_edited of that note is true then the note will be deleted and recreated.

Dataview examples

Thanks to the frontmatter data in each note you can use Dataview to easily query your notes

Books

```dataview
list
where koreader-sync
group by koreader-sync.data.title
```

Chapters of a specific book (with notes in them)

```dataview
list
where koreader-sync.data.title = "How to Take Smart Notes: One Simple Technique to Boost Writing, Learning and Thinking - for Students, Academics and Nonfiction Book Writers"
group by koreader-sync.data.chapter
```

Notes of a specific chapter of a specific book

```dataview
list
where koreader-sync.data.title = "How to Take Smart Notes: One Simple Technique to Boost Writing, Learning and Thinking - for Students, Academics and Nonfiction Book Writers" and koreader-sync.data.chapter = "Introduction"
```

Text of notes of a specific book (without a link to the note and only where text is present)

```dataview
list without id koreader-sync.data.text
where koreader-sync.data.title = "How to Take Smart Notes: One Simple Technique to Boost Writing, Learning and Thinking - for Students, Academics and Nonfiction Book Writers"
where koreader-sync.data.text
```

List of notes yet to be edited

```dataview
list 
where koreader-sync.metadata.yet_to_be_edited
```

List of notes that should be kept in sync

```dataview
list 
where koreader-sync.metadata.keep_in_sync
```

List of notes that will be kept in sync

```dataview
list 
where koreader-sync.metadata.keep_in_sync and koreader-sync.metadata.yet_to_be_edited
```