Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiiiz committed Jul 21, 2022
2 parents 81c9e84 + d2ad147 commit 94628d1
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 27 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-raindrop-highlights",
"name": "Raindrop Highlights",
"version": "0.0.2",
"version": "0.0.3",
"minAppVersion": "0.14.0",
"description": "Sync your Raindrop.io highlights.",
"author": "kaiiiz",
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-raindrop-highlights",
"version": "0.0.2",
"version": "0.0.3",
"description": "Sync your Raindrop.io highlights.",
"main": "main.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class RaindropAPI {
}

async get(url: string, params: any) {
const token = this.plugin.settings.token;

const token = this.plugin.tokenManager.get();
if (!token) {
throw new Error("Invalid token");
}
Expand Down Expand Up @@ -111,6 +110,7 @@ export class RaindropAPI {
excerpt: raindrop['excerpt'],
link: raindrop['link'],
lastUpdate: new Date(raindrop['lastUpdate']),
tags: raindrop['tags'],
};
return article;
});
Expand Down
1 change: 1 addition & 0 deletions src/assets/defaultTemplate.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% if is_new_article %}
# Metadata
{% if link %}Source URL:: {{link}}{% endif %}
{% if tags|length %}Topics:: {{ tags | join(", ") }}{% endif %}

---
# {{title}}
Expand Down
27 changes: 24 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Plugin } from 'obsidian';
import { RaindropSettingTab } from './settings';
import RaindropSync from './sync';
import type { RaindropPluginSettings } from './types';
import type { RaindropCollection, RaindropPluginSettings } from './types';
import DEFAULT_TEMPLATE from './assets/defaultTemplate.njk';
import TokenManager from './tokenManager';


const DEFAULT_SETTINGS: RaindropPluginSettings = {
token: '',
highlightsFolder: '',
syncCollections: {},
template: DEFAULT_TEMPLATE,
Expand All @@ -15,11 +15,13 @@ const DEFAULT_SETTINGS: RaindropPluginSettings = {

export default class RaindropPlugin extends Plugin {
private raindropSync: RaindropSync;
settings: RaindropPluginSettings;
public settings: RaindropPluginSettings;
public tokenManager: TokenManager;

async onload() {
await this.loadSettings();

this.tokenManager = new TokenManager();
this.raindropSync = new RaindropSync(this.app, this);

this.addCommand({
Expand All @@ -44,4 +46,23 @@ export default class RaindropPlugin extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}

async updateCollectionSettings(collections: RaindropCollection[]) {
const syncCollections = this.settings.syncCollections;
collections.forEach(async (collection) => {
const {id, title} = collection;

if (!(id in syncCollections)) {
syncCollections[id] = {
id: id,
title: title,
sync: false,
lastSyncDate: undefined,
};
} else {
syncCollections[id].title = title;
}
});
await this.saveSettings();
}
}
7 changes: 5 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RenderTemplate = {
excerpt: string;
link: string;
highlights: RenderHighlight[];
tags: string[],
};

export default class Renderer {
Expand All @@ -40,7 +41,7 @@ export default class Renderer {
}

renderContent(article: RaindropArticle, newArticle = true) {
const { id , title, highlights, excerpt, link } = article;
const { id , title, highlights, excerpt, link, tags } = article;
const dateTimeFormat = this.plugin.settings.dateTimeFormat;

const renderHighlights: RenderHighlight[] = highlights.map(hl => {
Expand All @@ -62,8 +63,9 @@ export default class Renderer {
excerpt,
link,
highlights: renderHighlights,
tags,
};

const template = this.plugin.settings.template;
const content = nunjucks.renderString(template, context);
return content;
Expand All @@ -72,6 +74,7 @@ export default class Renderer {
addFrontMatter(markdownContent: string, article: RaindropArticle) {
const fm: ArticleFileFrontMatter = {
raindrop_id: article.id,
raindrop_last_update: (new Date()).toISOString(),
};
return matter.stringify(markdownContent, fm);
}
Expand Down
22 changes: 6 additions & 16 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ export class RaindropSettingTab extends PluginSettingTab {
.setDesc(tokenDescFragment)
.addText(async (text) => {
try {
text.setValue(this.plugin.settings.token);
const token = this.plugin.tokenManager.get();
if (token) {
text.setValue(token);
}
} catch (e) {
/* Throw away read error if file does not exist. */
}

text.onChange(async (value) => {
try {
this.plugin.settings.token = value;
this.plugin.tokenManager.set(value);
new Notice('Token saved');
} catch (e) {
new Notice('Invalid token');
Expand All @@ -90,20 +93,7 @@ export class RaindropSettingTab extends PluginSettingTab {
.onClick(async () => {
// update for new collections
const allCollections = await this.api.getCollections();
const syncCollections = this.plugin.settings.syncCollections;
allCollections.forEach(async (collection) => {
const {id, title} = collection;

if (!(id in syncCollections)) {
syncCollections[id] = {
id: id,
title: title,
sync: false,
lastSyncDate: undefined,
};
}
});
await this.plugin.saveSettings();
this.plugin.updateCollectionSettings(allCollections);

const collectionsModal = new CollectionsModal(this.app, this.plugin);
this.display(); // rerender
Expand Down
16 changes: 16 additions & 0 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default class RaindropSync {
}

async sync() {
const allCollections = await this.api.getCollections();
this.plugin.updateCollectionSettings(allCollections);

for (const id in this.plugin.settings.syncCollections) {
const collection = this.plugin.settings.syncCollections[id];
if (collection.sync) {
Expand Down Expand Up @@ -80,6 +83,19 @@ export default class RaindropSync {
}

async updateFile(file: TFile, article: RaindropArticle) {
const metadata = this.app.metadataCache.getFileCache(file);
if (metadata?.frontmatter && 'raindrop_last_update' in metadata.frontmatter) {
const localLastUpdate = new Date(metadata.frontmatter.raindrop_last_update);
if (localLastUpdate >= article.lastUpdate) {
console.debug('skip update file', file.path);
return;
}

article.highlights = article.highlights.filter(hl => {
return localLastUpdate < hl.lastUpdate;
});
}

console.debug("update file", file.path);
const newMdContent = this.renderer.renderContent(article, false);
const oldMdContent = await this.app.vault.cachedRead(file);
Expand Down
1 change: 1 addition & 0 deletions src/templates/templateInstructions.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<li><span class="u-pop">{{excerpt}}</span> - Article excerpt</li>
<li><span class="u-pop">{{link}}</span> - Link to source</li>
<li><span class="u-pop">{{highlights}}</span> - List of your Highlights</li>
<li><span class="u-pop">{{tags}}</span> - List of tag</li>
</ul>

Highlight
Expand Down
21 changes: 21 additions & 0 deletions src/tokenManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default class TokenManager {
localStorage: any;

constructor() {
this.localStorage = window.localStorage;
}

get(): string|null {
const token = this.localStorage.getItem('raindrop_token');

if (token === null || token.length == 0) {
return null;
}

return token;
}

set(token: string) {
this.localStorage.setItem('raindrop_token', token);
}
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface RaindropArticle { // Remote state
excerpt: string,
link: string,
lastUpdate: Date,
tags: string[],
}

// ----------
Expand All @@ -36,6 +37,7 @@ export interface ArticleFile {

export interface ArticleFileFrontMatter { // use snake_case in front matter
raindrop_id: number,
raindrop_last_update: string,
}

// ----------
Expand All @@ -50,7 +52,6 @@ export interface SyncCollection { // Local state
export interface SyncCollectionSettings {[id: number]: SyncCollection}

export interface RaindropPluginSettings {
token: string,
highlightsFolder: string;
syncCollections: SyncCollectionSettings;
template: string;
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"0.0.1": "0.12.0",
"0.0.2": "0.14.0"
"0.0.2": "0.14.0",
"0.0.3": "0.14.0"
}

0 comments on commit 94628d1

Please sign in to comment.