Skip to content

Commit

Permalink
v0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiiiz committed Sep 12, 2022
2 parents c6c043f + 1701939 commit 519ebce
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 94 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.12",
"version": "0.0.13",
"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.12",
"version": "0.0.13",
"description": "Sync your Raindrop.io highlights.",
"main": "main.js",
"scripts": {
Expand Down
60 changes: 32 additions & 28 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Notice, type App } from "obsidian";
import axios from "axios";
import type { RaindropArticle, RaindropCollection, RaindropHighlight, RaindropUser } from "./types";
import type { RaindropBookmark, RaindropCollection, RaindropHighlight, RaindropUser } from "./types";
import TokenManager from "./tokenManager";

const BASEURL = "https://api.raindrop.io/rest/v1"
Expand Down Expand Up @@ -42,17 +42,21 @@ export class RaindropAPI {

async getCollections(): Promise<RaindropCollection[]> {
let res = await this.get(`${BASEURL}/collections`, {});
const collectionMap: {[id: number]: string} = {}
const collectionMap: {[id: number]: string} = {};

let collections: RaindropCollection[] = res.items.map((collection: any) => {
let collections: RaindropCollection[] = [
{ id: -1, title: 'Unsorted' },
{ id: -99, title: 'Trash' },
];
res.items.forEach((collection: any) => {
const id = collection['_id'];
const title = collection['title'];
collectionMap[id] = title;
return {
collections.push({
title: title,
id: id,
};
})
});
});

res = await this.get(`${BASEURL}/collections/childrens`, {});
res.items.forEach((collection: any) => {
Expand All @@ -72,14 +76,14 @@ export class RaindropAPI {
return collections;
}

async getRaindropsAfter(collectionId: number, lastSync?: Date): Promise<RaindropArticle[]> {
async getRaindropsAfter(collectionId: number, lastSync?: Date): Promise<RaindropBookmark[]> {
const notice = new Notice("Fetch Raindrops highlights", 0);
let res = await this.get(`${BASEURL}/raindrops/${collectionId}`, {
"page": 0,
"sort": "-lastUpdate"
});
let raindropsCnt = res.count;
let articles = this.parseArticles(res.items);
let bookmarks = this.parseRaindrops(res.items);
let remainPages = Math.ceil(raindropsCnt / 25) - 1;
let totalPages = Math.ceil(raindropsCnt / 25) - 1;
let page = 1;
Expand All @@ -89,37 +93,37 @@ export class RaindropAPI {
"page": page,
"sort": "-lastUpdate"
});
articles = articles.concat(this.parseArticles(res.items));
bookmarks = bookmarks.concat(this.parseRaindrops(res.items));
}

if (articles.length > 0) {
if (bookmarks.length > 0) {
if (lastSync === undefined) { // sync all
while (remainPages--) {
notice.setMessage(`Sync Raindrop pages: ${totalPages - remainPages}/${totalPages}`)
await addNewPages(page++);
}
} else { // sync article after lastSync
while (articles[articles.length - 1].lastUpdate >= lastSync && remainPages--) {
while (bookmarks[bookmarks.length - 1].lastUpdate >= lastSync && remainPages--) {
notice.setMessage(`Sync Raindrop pages: ${totalPages - remainPages}/${totalPages}`)
await addNewPages(page++);
}
articles = articles.filter(article => {
return article.lastUpdate >= lastSync;
bookmarks = bookmarks.filter(bookmark => {
return bookmark.lastUpdate >= lastSync;
});
}
}

// get real highlights (raindrop returns only 3 highlights in /raindrops/${collectionId} endpoint)
for (let [idx, article] of articles.entries()) {
notice.setMessage(`Sync Raindrop articles: ${idx + 1}/${articles.length}`)
if (article.highlights.length == 3) {
let res = await this.get(`${BASEURL}/raindrop/${article.id}`, {});
article['highlights'] = this.parseHighlights(res.item.highlights);
for (let [idx, bookmark] of bookmarks.entries()) {
notice.setMessage(`Sync Raindrop bookmarks: ${idx + 1}/${bookmarks.length}`)
if (bookmark.highlights.length == 3) {
let res = await this.get(`${BASEURL}/raindrop/${bookmark.id}`, {});
bookmark['highlights'] = this.parseHighlights(res.item.highlights);
}
}

notice.hide();
return articles;
return bookmarks;
}

async getUser(): Promise<RaindropUser> {
Expand Down Expand Up @@ -151,20 +155,20 @@ export class RaindropAPI {
};
}

async getArticle(id: number): Promise<RaindropArticle> {
async getRaindrop(id: number): Promise<RaindropBookmark> {
const res = await this.get(`${BASEURL}/raindrop/${id}`, {});
const article = this.parseArticle(res.item);
return article;
const bookmark = this.parseRaindrop(res.item);
return bookmark;
}

private parseArticles(articles: any): RaindropArticle[] {
return articles.map((raindrop: any) => {
return this.parseArticle(raindrop);
private parseRaindrops(bookmarks: any): RaindropBookmark[] {
return bookmarks.map((raindrop: any) => {
return this.parseRaindrop(raindrop);
});
}

private parseArticle(raindrop: any): RaindropArticle {
const article: RaindropArticle = {
private parseRaindrop(raindrop: any): RaindropBookmark {
const bookmark: RaindropBookmark = {
id: raindrop['_id'],
collectionId: raindrop['collectionId'],
title: raindrop['title'],
Expand All @@ -178,7 +182,7 @@ export class RaindropAPI {
type: raindrop['type'],
important: raindrop['important'],
};
return article;
return bookmark;
}

private parseHighlights(highlights: any): RaindropHighlight[] {
Expand Down
18 changes: 3 additions & 15 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import DEFAULT_TEMPLATE from './assets/defaultTemplate.njk';
import type { RaindropPluginSettings } from "./types";

export const VERSION = '0.0.12';
export const VERSION = '0.0.13';

export const DEFAULT_SETTINGS: RaindropPluginSettings = {
version: VERSION,
username: undefined,
isConnected: false,
ribbonIcon: true,
appendMode: true,
onlyBookmarksWithHl: false,
highlightsFolder: '/',
syncCollections: {
'-1': {
id: -1,
title: 'Unsorted',
sync: false,
lastSyncDate: undefined,
},
'-99': {
id: -99,
title: 'Trash',
sync: false,
lastSyncDate: undefined,
}
},
syncCollections: {},
template: DEFAULT_TEMPLATE,
dateTimeFormat: 'YYYY/MM/DD HH:mm:ss',
autoSyncInterval: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export default class RaindropPlugin extends Plugin {
if (file) {
const fmc = app.metadataCache.getFileCache(file)?.frontmatter;
if (fmc?.raindrop_id) {
const article = await this.api.getArticle(fmc.raindrop_id);
window.open(`https://app.raindrop.io/my/${article.collectionId}/item/${article.id}/edit`);
const bookmark = await this.api.getRaindrop(fmc.raindrop_id);
window.open(`https://app.raindrop.io/my/${bookmark.collectionId}/item/${bookmark.id}/edit`);
} else {
new Notice("This is not a Raindrop article file")
}
Expand Down
34 changes: 17 additions & 17 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nunjucks from "nunjucks";
import Moment from "moment";
import type RaindropPlugin from "./main";
import type { ArticleFileFrontMatter, RaindropArticle } from "./types";
import type { BookmarkFileFrontMatter, RaindropBookmark } from "./types";
import { stringifyYaml } from "obsidian";

type RenderHighlight = {
Expand Down Expand Up @@ -49,10 +49,10 @@ export default class Renderer {
}
}

renderContent(article: RaindropArticle, newArticle = true) {
renderContent(bookmark: RaindropBookmark, newArticle = true) {
const dateTimeFormat = this.plugin.settings.dateTimeFormat;

const renderHighlights: RenderHighlight[] = article.highlights.map((hl) => {
const renderHighlights: RenderHighlight[] = bookmark.highlights.map((hl) => {
const renderHighlight: RenderHighlight = {
id: hl.id,
color: hl.color,
Expand All @@ -66,33 +66,33 @@ export default class Renderer {

// sync() should keep the latest collection data in local in the beginning
const renderCollection: RenderCollection = {
title: this.plugin.settings.syncCollections[article.collectionId].title,
title: this.plugin.settings.syncCollections[bookmark.collectionId].title,
}

const context: RenderTemplate = {
is_new_article: newArticle,
id: article.id,
title: article.title,
excerpt: article.excerpt,
link: article.link,
id: bookmark.id,
title: bookmark.title,
excerpt: bookmark.excerpt,
link: bookmark.link,
highlights: renderHighlights,
collection: renderCollection,
tags: article.tags,
cover: article.cover,
created: Moment(article.created).format(dateTimeFormat),
type: article.type,
important: article.important,
tags: bookmark.tags,
cover: bookmark.cover,
created: Moment(bookmark.created).format(dateTimeFormat),
type: bookmark.type,
important: bookmark.important,
};

const template = this.plugin.settings.template;
const content = nunjucks.renderString(template, context);
return content;
}

renderFullPost(article: RaindropArticle) {
const newMdContent = this.renderContent(article, true);
const frontmatter: ArticleFileFrontMatter = {
raindrop_id: article.id,
renderFullPost(bookmark: RaindropBookmark) {
const newMdContent = this.renderContent(bookmark, true);
const frontmatter: BookmarkFileFrontMatter = {
raindrop_id: bookmark.id,
raindrop_last_update: (new Date()).toISOString(),
};
const frontmatterStr = stringifyYaml(frontmatter);
Expand Down
14 changes: 14 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class RaindropSettingTab extends PluginSettingTab {
this.connect();
}
this.ribbonIcon();
this.onlyBookmarksWithHl();
this.appendMode();
this.highlightsFolder();
this.collections();
Expand Down Expand Up @@ -70,6 +71,19 @@ export class RaindropSettingTab extends PluginSettingTab {
});
}

private onlyBookmarksWithHl(): void {
new Setting(this.containerEl)
.setName('Only sync bookmarks with highlights')
.addToggle((toggle) => {
return toggle
.setValue(this.plugin.settings.ribbonIcon)
.onChange(async (value) => {
this.plugin.settings.onlyBookmarksWithHl = value;
await this.plugin.saveSettings();
});
});
}

private connect(): void {
new Setting(this.containerEl)
.setName('Connect to Raindrop.io')
Expand Down
Loading

0 comments on commit 519ebce

Please sign in to comment.