Skip to content

Commit 519ebce

Browse files
committed
v0.0.13
2 parents c6c043f + 1701939 commit 519ebce

10 files changed

+106
-94
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-raindrop-highlights",
33
"name": "Raindrop Highlights",
4-
"version": "0.0.12",
4+
"version": "0.0.13",
55
"minAppVersion": "0.14.0",
66
"description": "Sync your Raindrop.io highlights.",
77
"author": "kaiiiz",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-raindrop-highlights",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "Sync your Raindrop.io highlights.",
55
"main": "main.js",
66
"scripts": {

src/api.ts

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Notice, type App } from "obsidian";
22
import axios from "axios";
3-
import type { RaindropArticle, RaindropCollection, RaindropHighlight, RaindropUser } from "./types";
3+
import type { RaindropBookmark, RaindropCollection, RaindropHighlight, RaindropUser } from "./types";
44
import TokenManager from "./tokenManager";
55

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

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

47-
let collections: RaindropCollection[] = res.items.map((collection: any) => {
47+
let collections: RaindropCollection[] = [
48+
{ id: -1, title: 'Unsorted' },
49+
{ id: -99, title: 'Trash' },
50+
];
51+
res.items.forEach((collection: any) => {
4852
const id = collection['_id'];
4953
const title = collection['title'];
5054
collectionMap[id] = title;
51-
return {
55+
collections.push({
5256
title: title,
5357
id: id,
54-
};
55-
})
58+
});
59+
});
5660

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

75-
async getRaindropsAfter(collectionId: number, lastSync?: Date): Promise<RaindropArticle[]> {
79+
async getRaindropsAfter(collectionId: number, lastSync?: Date): Promise<RaindropBookmark[]> {
7680
const notice = new Notice("Fetch Raindrops highlights", 0);
7781
let res = await this.get(`${BASEURL}/raindrops/${collectionId}`, {
7882
"page": 0,
7983
"sort": "-lastUpdate"
8084
});
8185
let raindropsCnt = res.count;
82-
let articles = this.parseArticles(res.items);
86+
let bookmarks = this.parseRaindrops(res.items);
8387
let remainPages = Math.ceil(raindropsCnt / 25) - 1;
8488
let totalPages = Math.ceil(raindropsCnt / 25) - 1;
8589
let page = 1;
@@ -89,37 +93,37 @@ export class RaindropAPI {
8993
"page": page,
9094
"sort": "-lastUpdate"
9195
});
92-
articles = articles.concat(this.parseArticles(res.items));
96+
bookmarks = bookmarks.concat(this.parseRaindrops(res.items));
9397
}
9498

95-
if (articles.length > 0) {
99+
if (bookmarks.length > 0) {
96100
if (lastSync === undefined) { // sync all
97101
while (remainPages--) {
98102
notice.setMessage(`Sync Raindrop pages: ${totalPages - remainPages}/${totalPages}`)
99103
await addNewPages(page++);
100104
}
101105
} else { // sync article after lastSync
102-
while (articles[articles.length - 1].lastUpdate >= lastSync && remainPages--) {
106+
while (bookmarks[bookmarks.length - 1].lastUpdate >= lastSync && remainPages--) {
103107
notice.setMessage(`Sync Raindrop pages: ${totalPages - remainPages}/${totalPages}`)
104108
await addNewPages(page++);
105109
}
106-
articles = articles.filter(article => {
107-
return article.lastUpdate >= lastSync;
110+
bookmarks = bookmarks.filter(bookmark => {
111+
return bookmark.lastUpdate >= lastSync;
108112
});
109113
}
110114
}
111115

112116
// get real highlights (raindrop returns only 3 highlights in /raindrops/${collectionId} endpoint)
113-
for (let [idx, article] of articles.entries()) {
114-
notice.setMessage(`Sync Raindrop articles: ${idx + 1}/${articles.length}`)
115-
if (article.highlights.length == 3) {
116-
let res = await this.get(`${BASEURL}/raindrop/${article.id}`, {});
117-
article['highlights'] = this.parseHighlights(res.item.highlights);
117+
for (let [idx, bookmark] of bookmarks.entries()) {
118+
notice.setMessage(`Sync Raindrop bookmarks: ${idx + 1}/${bookmarks.length}`)
119+
if (bookmark.highlights.length == 3) {
120+
let res = await this.get(`${BASEURL}/raindrop/${bookmark.id}`, {});
121+
bookmark['highlights'] = this.parseHighlights(res.item.highlights);
118122
}
119123
}
120124

121125
notice.hide();
122-
return articles;
126+
return bookmarks;
123127
}
124128

125129
async getUser(): Promise<RaindropUser> {
@@ -151,20 +155,20 @@ export class RaindropAPI {
151155
};
152156
}
153157

154-
async getArticle(id: number): Promise<RaindropArticle> {
158+
async getRaindrop(id: number): Promise<RaindropBookmark> {
155159
const res = await this.get(`${BASEURL}/raindrop/${id}`, {});
156-
const article = this.parseArticle(res.item);
157-
return article;
160+
const bookmark = this.parseRaindrop(res.item);
161+
return bookmark;
158162
}
159163

160-
private parseArticles(articles: any): RaindropArticle[] {
161-
return articles.map((raindrop: any) => {
162-
return this.parseArticle(raindrop);
164+
private parseRaindrops(bookmarks: any): RaindropBookmark[] {
165+
return bookmarks.map((raindrop: any) => {
166+
return this.parseRaindrop(raindrop);
163167
});
164168
}
165169

166-
private parseArticle(raindrop: any): RaindropArticle {
167-
const article: RaindropArticle = {
170+
private parseRaindrop(raindrop: any): RaindropBookmark {
171+
const bookmark: RaindropBookmark = {
168172
id: raindrop['_id'],
169173
collectionId: raindrop['collectionId'],
170174
title: raindrop['title'],
@@ -178,7 +182,7 @@ export class RaindropAPI {
178182
type: raindrop['type'],
179183
important: raindrop['important'],
180184
};
181-
return article;
185+
return bookmark;
182186
}
183187

184188
private parseHighlights(highlights: any): RaindropHighlight[] {

src/constants.ts

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import DEFAULT_TEMPLATE from './assets/defaultTemplate.njk';
22
import type { RaindropPluginSettings } from "./types";
33

4-
export const VERSION = '0.0.12';
4+
export const VERSION = '0.0.13';
55

66
export const DEFAULT_SETTINGS: RaindropPluginSettings = {
77
version: VERSION,
88
username: undefined,
99
isConnected: false,
1010
ribbonIcon: true,
1111
appendMode: true,
12+
onlyBookmarksWithHl: false,
1213
highlightsFolder: '/',
13-
syncCollections: {
14-
'-1': {
15-
id: -1,
16-
title: 'Unsorted',
17-
sync: false,
18-
lastSyncDate: undefined,
19-
},
20-
'-99': {
21-
id: -99,
22-
title: 'Trash',
23-
sync: false,
24-
lastSyncDate: undefined,
25-
}
26-
},
14+
syncCollections: {},
2715
template: DEFAULT_TEMPLATE,
2816
dateTimeFormat: 'YYYY/MM/DD HH:mm:ss',
2917
autoSyncInterval: 0,

src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export default class RaindropPlugin extends Plugin {
5757
if (file) {
5858
const fmc = app.metadataCache.getFileCache(file)?.frontmatter;
5959
if (fmc?.raindrop_id) {
60-
const article = await this.api.getArticle(fmc.raindrop_id);
61-
window.open(`https://app.raindrop.io/my/${article.collectionId}/item/${article.id}/edit`);
60+
const bookmark = await this.api.getRaindrop(fmc.raindrop_id);
61+
window.open(`https://app.raindrop.io/my/${bookmark.collectionId}/item/${bookmark.id}/edit`);
6262
} else {
6363
new Notice("This is not a Raindrop article file")
6464
}

src/renderer.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nunjucks from "nunjucks";
22
import Moment from "moment";
33
import type RaindropPlugin from "./main";
4-
import type { ArticleFileFrontMatter, RaindropArticle } from "./types";
4+
import type { BookmarkFileFrontMatter, RaindropBookmark } from "./types";
55
import { stringifyYaml } from "obsidian";
66

77
type RenderHighlight = {
@@ -49,10 +49,10 @@ export default class Renderer {
4949
}
5050
}
5151

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

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

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

7272
const context: RenderTemplate = {
7373
is_new_article: newArticle,
74-
id: article.id,
75-
title: article.title,
76-
excerpt: article.excerpt,
77-
link: article.link,
74+
id: bookmark.id,
75+
title: bookmark.title,
76+
excerpt: bookmark.excerpt,
77+
link: bookmark.link,
7878
highlights: renderHighlights,
7979
collection: renderCollection,
80-
tags: article.tags,
81-
cover: article.cover,
82-
created: Moment(article.created).format(dateTimeFormat),
83-
type: article.type,
84-
important: article.important,
80+
tags: bookmark.tags,
81+
cover: bookmark.cover,
82+
created: Moment(bookmark.created).format(dateTimeFormat),
83+
type: bookmark.type,
84+
important: bookmark.important,
8585
};
8686

8787
const template = this.plugin.settings.template;
8888
const content = nunjucks.renderString(template, context);
8989
return content;
9090
}
9191

92-
renderFullPost(article: RaindropArticle) {
93-
const newMdContent = this.renderContent(article, true);
94-
const frontmatter: ArticleFileFrontMatter = {
95-
raindrop_id: article.id,
92+
renderFullPost(bookmark: RaindropBookmark) {
93+
const newMdContent = this.renderContent(bookmark, true);
94+
const frontmatter: BookmarkFileFrontMatter = {
95+
raindrop_id: bookmark.id,
9696
raindrop_last_update: (new Date()).toISOString(),
9797
};
9898
const frontmatterStr = stringifyYaml(frontmatter);

src/settings.ts

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class RaindropSettingTab extends PluginSettingTab {
3030
this.connect();
3131
}
3232
this.ribbonIcon();
33+
this.onlyBookmarksWithHl();
3334
this.appendMode();
3435
this.highlightsFolder();
3536
this.collections();
@@ -70,6 +71,19 @@ export class RaindropSettingTab extends PluginSettingTab {
7071
});
7172
}
7273

74+
private onlyBookmarksWithHl(): void {
75+
new Setting(this.containerEl)
76+
.setName('Only sync bookmarks with highlights')
77+
.addToggle((toggle) => {
78+
return toggle
79+
.setValue(this.plugin.settings.ribbonIcon)
80+
.onChange(async (value) => {
81+
this.plugin.settings.onlyBookmarksWithHl = value;
82+
await this.plugin.saveSettings();
83+
});
84+
});
85+
}
86+
7387
private connect(): void {
7488
new Setting(this.containerEl)
7589
.setName('Connect to Raindrop.io')

0 commit comments

Comments
 (0)