@@ -10,6 +10,8 @@ interface PluginSettings {
10
10
lastUpdated : string | null ;
11
11
autoSync : boolean ;
12
12
highlightSortOldestToNewest : boolean ;
13
+ logFile : boolean ;
14
+ logFileName : string ;
13
15
}
14
16
15
17
const DEFAULT_SETTINGS : PluginSettings = {
@@ -18,6 +20,8 @@ const DEFAULT_SETTINGS: PluginSettings = {
18
20
lastUpdated : null ,
19
21
autoSync : true ,
20
22
highlightSortOldestToNewest : true ,
23
+ logFile : true ,
24
+ logFileName : 'Sync.md' ,
21
25
} ;
22
26
23
27
export default class ReadwiseMirror extends Plugin {
@@ -50,6 +54,40 @@ ${text} ${book.category === 'books' ? locationBlock : ''}${color ? ` %% Color: $
50
54
return dateStr . split ( 'T' ) [ 0 ] ;
51
55
}
52
56
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
+
53
91
async writeLibraryToMarkdown ( library : Library ) {
54
92
const vault = this . app . vault ;
55
93
@@ -81,7 +119,7 @@ ${text} ${book.category === 'books' ? locationBlock : ''}${color ? ` %% Color: $
81
119
highlights,
82
120
last_highlight_at,
83
121
source_url,
84
- tags
122
+ tags,
85
123
} = book ;
86
124
const sanitizedTitle = `${ title . replace ( ':' , '-' ) . replace ( / [ < > " ' \/ \\ | ? * ] + / g, '' ) } ` ;
87
125
@@ -111,7 +149,7 @@ Updated: ${this.formatDate(updated)}
111
149
# About
112
150
Title: [[${ sanitizedTitle } ]]
113
151
${ 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 ) : '' }
115
153
Number of Highlights: ==${ num_highlights } ==
116
154
Last Highlighted: *${ last_highlight_at ? this . formatDate ( last_highlight_at ) : 'Never' } *
117
155
Readwise URL: ${ highlights_url } ${ category === 'articles' ? `\nSource URL: ${ source_url } \n` : '' }
@@ -175,6 +213,9 @@ Readwise URL: ${highlights_url}${category === 'articles' ? `\nSource URL: ${sour
175
213
176
214
if ( Object . keys ( library . books ) . length > 0 ) {
177
215
this . writeLibraryToMarkdown ( library ) ;
216
+
217
+ if ( this . settings . logFile ) this . writeLogToMarkdown ( library ) ;
218
+
178
219
this . notify . notice (
179
220
`Readwise: Downloaded ${ library . highlightCount } Highlights from ${ Object . keys ( library . books ) . length } Sources`
180
221
) ;
@@ -351,5 +392,29 @@ class ReadwiseMirrorSettingTab extends PluginSettingTab {
351
392
await this . plugin . saveSettings ( ) ;
352
393
} )
353
394
) ;
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
+ ) ;
354
419
}
355
420
}
0 commit comments