Skip to content

Commit

Permalink
Merge pull request #20 from Edo78/develop
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
Edo78 authored Jan 26, 2022
2 parents 4874781 + df8ba7c commit 879084e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 24 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,33 @@ 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

### Template 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
```
# Title: [[<%= it.bookPath %>|<%= it.title %>]]
by: [[<%= it.authors %>]]
## Chapter: <%= it.chapter %>
**==<%= it.highlight %>==**
<%= it.text %>
```
In the `Template 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 full path in `Template file`

The template receive the following arguments:
- `bookPath`: koreader/(book) How to Take Smart Notes_... {book suffix}-Sönke Ahrens
- `title`: How to Take Smart Notes: One Simple Technique to Boost Writing, Learning and Thinking - for Students, Academics and Nonfiction Book Writers
- `authors`: Sönke Ahrens
- `chapter`: 1.1 Good Solutions are Simple – and Unexpected
- `highlight`: Clance and Imes 1978; Brems et al. 1994
- `text`: Clance (1978) first identified the Impostor Phenomenon in therapeutic sessions with highly successful women who attributed achievements to external factors
- `datetime`: 2022-01-22 09:57:29

## Usage
Once the plugin is configured properly you can plug the device with KOReader and click on the icon with two documents. The plugin should propmplty create a single file for each note.
Once the plugin is configured properly you can plug the device with KOReader and click on the icon with two documents and the tooltip `KOReader Plugin`. The plugin should propmplty create a single file for each note.

Create an issue if something didn't work as expected.

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.1.2",
"version": "0.2.0",
"minAppVersion": "0.12.0",
"description": "This is a plugin for Obsidian. This plugin syncs highlights and notes taken in KOReader.",
"author": "Federico \"Edo\" Granata",
Expand Down
21 changes: 19 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-koreader-plugin",
"version": "0.1.2",
"version": "0.2.0",
"description": "This is a plugin for Obsidian. This plugin syncs highlights and notes taken in KOReader.",
"main": "main.js",
"scripts": {
Expand Down Expand Up @@ -34,6 +34,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"eta": "^1.12.3",
"gray-matter": "^4.0.3",
"lua-json": "^1.0.0",
"node-find-files": "^1.0.0"
Expand Down
88 changes: 70 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
Setting,
normalizePath,
TAbstractFile,
TFile,
} from 'obsidian';
import * as matter from 'gray-matter';
import * as crypto from 'crypto';
import * as eta from 'eta';
import { Book, Bookmark, Books, FrontMatter } from './types';
import { KOReaderMetadata } from './koreader-metadata';

Expand All @@ -18,11 +20,14 @@ interface KOReaderSettings {
bookTitleOptions: TitleOptions;
keepInSync: boolean;
aFolderForEachBook: boolean;
customTemplate: boolean;
templatePath?: string;
}

const DEFAULT_SETTINGS: KOReaderSettings = {
keepInSync: false,
aFolderForEachBook: false,
customTemplate: false,
koreaderBasePath: '/media/user/KOBOeReader',
obsidianNoteFolder: '/',
noteTitleOptions: {
Expand Down Expand Up @@ -72,6 +77,10 @@ export default class KOReader extends Plugin {
}

async onload() {
eta.configure({
cache: true, // Make Eta cache templates
autoEscape: false,
});
await this.loadSettings();

const ribbonIconEl = this.addRibbonIcon(
Expand All @@ -83,7 +92,7 @@ export default class KOReader extends Plugin {
this.addSettingTab(new KoreaderSettingTab(this.app, this));
}

onunload() { }
onunload() {}

async loadSettings() {
this.settings = { ...DEFAULT_SETTINGS, ...(await this.loadData()) };
Expand All @@ -93,7 +102,7 @@ export default class KOReader extends Plugin {
await this.saveData(this.settings);
}

private createNote(note: {
private async createNote(note: {
path: string;
uniqueId: string;
bookmark: Bookmark;
Expand All @@ -109,20 +118,36 @@ export default class KOReader extends Plugin {
const noteTitle = noteItself
? this.manageTitle(noteItself, this.settings.noteTitleOptions)
: `${this.manageTitle(
bookmark.notes,
this.settings.noteTitleOptions
)} - ${book.authors}`;
bookmark.notes,
this.settings.noteTitleOptions
)} - ${book.authors}`;
const notePath = normalizePath(`${path}/${noteTitle}`);

const content = `# Title: [[${normalizePath(
`${path}/${managedBookTitle}`
)}|${book.title}]]
by: [[${book.authors}]]
## Chapter: ${bookmark.chapter}
**==${bookmark.notes}==**
const defaultTemplate = `# Title: [[<%= it.bookPath %>|<%= it.title %>]]
by: [[<%= it.authors %>]]
## Chapter: <%= it.chapter %>
${noteItself}
`;
**==<%= it.highlight %>==**
<%= it.text %>`;

const templateFile = this.settings.customTemplate
? this.app.vault.getAbstractFileByPath(this.settings.templatePath)
: null;
const template = templateFile
? await this.app.vault.read(templateFile as TFile)
: defaultTemplate;
const content = (await eta.render(template, {
bookPath: normalizePath(`${path}/${managedBookTitle}`),
title: book.title,
authors: book.authors,
chapter: bookmark.chapter,
highlight: bookmark.notes,
text: noteItself,
datetime: bookmark.datetime,
})) as string;

const frontmatterData: { [key: string]: FrontMatter } = {
[KOREADERKEY]: {
Expand Down Expand Up @@ -176,11 +201,11 @@ ${noteItself}
)}-${data[book].authors}`;
// if aFolderForEachBook is set, create a folder for each book
if (this.settings.aFolderForEachBook) {
const bookPath = normalizePath(
const bookFolder = normalizePath(
`${this.settings.obsidianNoteFolder}/${managedBookTitle}`
);
if (!this.app.vault.getAbstractFileByPath(bookPath)) {
this.app.vault.createFolder(bookPath);
if (!this.app.vault.getAbstractFileByPath(bookFolder)) {
this.app.vault.createFolder(bookFolder);
}
}
for (const bookmark in data[book].bookmarks) {
Expand All @@ -206,7 +231,7 @@ ${noteItself}
const path = this.settings.aFolderForEachBook
? `${this.settings.obsidianNoteFolder}/${managedBookTitle}`
: this.settings.obsidianNoteFolder;
const { content, frontmatterData, notePath } = this.createNote({
const { content, frontmatterData, notePath } = await this.createNote({
path,
uniqueId,
bookmark: data[book].bookmarks[bookmark],
Expand Down Expand Up @@ -242,7 +267,7 @@ class KoreaderSettingTab extends PluginSettingTab {

new Setting(containerEl)
.setName('KOReader mounted path')
.setDesc('/media/<user>/KOBOeReader')
.setDesc('Eg. /media/<user>/KOBOeReader')
.addText((text) =>
text
.setPlaceholder('Enter the path wher KOReader is mounted')
Expand Down Expand Up @@ -314,6 +339,33 @@ class KoreaderSettingTab extends PluginSettingTab {
})
);

containerEl.createEl('h2', { text: 'Template settings' });

new Setting(containerEl)
.setName('Custom template')
.setDesc('Use a custom template for the notes')
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.customTemplate)
.onChange(async (value) => {
this.plugin.settings.customTemplate = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName('Template file')
.setDesc('The template file to use. Remember to add the ".md" extension')
.addText((text) =>
text
.setPlaceholder('templates/note.md')
.setValue(this.plugin.settings.templatePath)
.onChange(async (value) => {
this.plugin.settings.templatePath = value;
await this.plugin.saveSettings();
})
);

containerEl.createEl('h2', { text: 'Note title settings' });

new Setting(containerEl).setName('Prefix').addText((text) =>
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"0.0.6": "0.12.0",
"0.1.0": "0.12.0",
"0.1.1": "0.12.0",
"0.1.2": "0.12.0"
"0.1.2": "0.12.0",
"0.2.0": "0.12.0"
}

0 comments on commit 879084e

Please sign in to comment.