Skip to content

Commit 363a0bd

Browse files
committed
Added Sync Log functionality
1 parent 9d301cf commit 363a0bd

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.1 (2021-08-01)
4+
### Added
5+
- Added Sync Log functionality. Creates a file (configurable, with a default filename of `Sync.md`) in the Readwise library root folder, which stores a time-based log listing when Readwise sources have synced new highlights
6+
37
## 1.1.0 (2021-06-24)
48
### Added
59
- Added tag support, both in highlights and sources (books, articles, etc)

main.ts

+67-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface PluginSettings {
1010
lastUpdated: string | null;
1111
autoSync: boolean;
1212
highlightSortOldestToNewest: boolean;
13+
logFile: boolean;
14+
logFileName: string;
1315
}
1416

1517
const DEFAULT_SETTINGS: PluginSettings = {
@@ -18,6 +20,8 @@ const DEFAULT_SETTINGS: PluginSettings = {
1820
lastUpdated: null,
1921
autoSync: true,
2022
highlightSortOldestToNewest: true,
23+
logFile: true,
24+
logFileName: 'Sync.md',
2125
};
2226

2327
export default class ReadwiseMirror extends Plugin {
@@ -50,6 +54,40 @@ ${text} ${book.category === 'books' ? locationBlock : ''}${color ? ` %% Color: $
5054
return dateStr.split('T')[0];
5155
}
5256

57+
async writeLogToMarkdown(library: Library) {
58+
const vault = this.app.vault;
59+
60+
let path = `${this.settings.baseFolderName}/${this.settings.logFileName}`;
61+
const abstractFile = vault.getAbstractFileByPath(path);
62+
63+
const now = spacetime.now();
64+
let logString = `# [[${now.format('iso-short')}]] *(${now.time()})*`;
65+
66+
for (let bookId in library['books']) {
67+
const book = library['books'][bookId];
68+
69+
const { title, num_highlights } = book;
70+
const sanitizedTitle = `${title.replace(':', '-').replace(/[<>"'\/\\|?*]+/g, '')}`;
71+
const contents = `\n- [[${sanitizedTitle}]] *(${num_highlights} highlights)*`;
72+
logString += contents;
73+
}
74+
75+
try {
76+
if (abstractFile) {
77+
// If log file already exists, append to the content instead of overwriting
78+
const logFile = vault.getFiles().filter((file) => file.name === this.settings.logFileName)[0];
79+
console.log('logFile:', logFile);
80+
81+
const logFileContents = await vault.read(logFile);
82+
vault.modify(logFile, logFileContents + '\n\n' + logString);
83+
} else {
84+
vault.create(path, logString);
85+
}
86+
} catch (err) {
87+
console.error(`Readwise: Error writing to sync log file`, err);
88+
}
89+
}
90+
5391
async writeLibraryToMarkdown(library: Library) {
5492
const vault = this.app.vault;
5593

@@ -81,7 +119,7 @@ ${text} ${book.category === 'books' ? locationBlock : ''}${color ? ` %% Color: $
81119
highlights,
82120
last_highlight_at,
83121
source_url,
84-
tags
122+
tags,
85123
} = book;
86124
const sanitizedTitle = `${title.replace(':', '-').replace(/[<>"'\/\\|?*]+/g, '')}`;
87125

@@ -111,7 +149,7 @@ Updated: ${this.formatDate(updated)}
111149
# About
112150
Title: [[${sanitizedTitle}]]
113151
${authors.length > 1 ? 'Authors' : 'Author'}: ${authorStr}
114-
Category: #${category}${tags.length > 1 ? ('\nTags: ' + this.formatTags(tags)): ''}
152+
Category: #${category}${tags.length > 1 ? '\nTags: ' + this.formatTags(tags) : ''}
115153
Number of Highlights: ==${num_highlights}==
116154
Last Highlighted: *${last_highlight_at ? this.formatDate(last_highlight_at) : 'Never'}*
117155
Readwise URL: ${highlights_url}${category === 'articles' ? `\nSource URL: ${source_url}\n` : ''}
@@ -175,6 +213,9 @@ Readwise URL: ${highlights_url}${category === 'articles' ? `\nSource URL: ${sour
175213

176214
if (Object.keys(library.books).length > 0) {
177215
this.writeLibraryToMarkdown(library);
216+
217+
if (this.settings.logFile) this.writeLogToMarkdown(library);
218+
178219
this.notify.notice(
179220
`Readwise: Downloaded ${library.highlightCount} Highlights from ${Object.keys(library.books).length} Sources`
180221
);
@@ -351,5 +392,29 @@ class ReadwiseMirrorSettingTab extends PluginSettingTab {
351392
await this.plugin.saveSettings();
352393
})
353394
);
395+
396+
new Setting(containerEl)
397+
.setName('Sync Log')
398+
.setDesc('Save sync log to file in Library')
399+
.addToggle((toggle) =>
400+
toggle.setValue(this.plugin.settings.logFile).onChange(async (value) => {
401+
this.plugin.settings.logFile = value;
402+
await this.plugin.saveSettings();
403+
})
404+
);
405+
406+
new Setting(containerEl)
407+
.setName('Sync Log File Name')
408+
.setDesc('Default: Sync.md')
409+
.addText((text) =>
410+
text
411+
.setPlaceholder('Sync.md')
412+
.setValue(this.plugin.settings.logFileName)
413+
.onChange(async (value) => {
414+
if (!value) return;
415+
this.plugin.settings.logFileName = value;
416+
await this.plugin.saveSettings();
417+
})
418+
);
354419
}
355420
}

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "readwise-mirror",
33
"name": "Readwise Mirror",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"minAppVersion": "0.11.0",
66
"description": "Mirror your Readwise library directly to an Obsidian vault",
77
"author": "jsonmartin",

readwiseApi.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class ReadwiseApi {
130130
}
131131
} while (data.next);
132132

133-
console.info(`Readwise: Processed ${results.length} total ${contentType} results successfully`);
133+
if (results.length > 0) console.info(`Readwise: Processed ${results.length} total ${contentType} results successfully`);
134134
return results;
135135
}
136136

0 commit comments

Comments
 (0)