Skip to content

Commit

Permalink
Merge pull request #44 from hadynz/template-validation
Browse files Browse the repository at this point in the history
Validate note template
  • Loading branch information
hadynz authored May 27, 2021
2 parents c3158c2 + d2c6956 commit 177884f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-kindle-plugin",
"name": "Kindle Highlights",
"version": "0.2.9",
"version": "0.2.10",
"minAppVersion": "0.10.2",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"author": "Hady Osman",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-kindle-plugin",
"version": "0.2.9",
"version": "0.2.10",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"main": "src/index.ts",
"repository": {
Expand Down
9 changes: 9 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export class Renderer {
nunjucks.configure({ autoescape: false });
}

validate(template: string): boolean {
try {
nunjucks.renderString(template, {});
return true;
} catch (error) {
return false;
}
}

render(entry: BookHighlight): string {
const { book, highlights } = entry;

Expand Down
15 changes: 12 additions & 3 deletions src/settingsTab/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { App, PluginSettingTab, Setting } from 'obsidian';
import pickBy from 'lodash.pickby';
import { App, PluginSettingTab, Setting } from 'obsidian';
import { get } from 'svelte/store';

import AmazonLogoutModal from '../components/amazonLogoutModal';
import templateInstructions from './templateInstructions.html';
import type KindlePlugin from '..';
import AmazonLogoutModal from '../components/amazonLogoutModal';
import { Renderer } from '../renderer';
import { settingsStore } from '../store';
import { scrapeLogoutUrl } from '../scraper';

const { moment } = window;

export class SettingsTab extends PluginSettingTab {
public app: App;
private renderer: Renderer;

constructor(app: App, plugin: KindlePlugin) {
super(app, plugin);
this.app = app;
this.renderer = new Renderer();
}

public async display(): Promise<void> {
Expand Down Expand Up @@ -104,7 +107,13 @@ export class SettingsTab extends PluginSettingTab {
text
.setValue(get(settingsStore).noteTemplate)
.onChange(async (value) => {
await settingsStore.actions.setNoteTemplate(value);
const isValid = this.renderer.validate(value);

if (isValid) {
await settingsStore.actions.setNoteTemplate(value);
}

text.inputEl.style.border = isValid ? '' : '1px solid red';
});
return text;
});
Expand Down

0 comments on commit 177884f

Please sign in to comment.