@@ -198,8 +198,11 @@ const fetchOmnivore = async (inBackground = false) => {
198
198
const datePublished = article . publishedAt
199
199
? formatDate ( new Date ( article . publishedAt ) , preferredDateFormat )
200
200
: undefined
201
+ const note = article . highlights ?. find (
202
+ ( h ) => h . type === HighlightType . Note
203
+ )
201
204
// Build content string based on template
202
- const articleView = {
205
+ const articleVariables = {
203
206
title : article . title ,
204
207
omnivoreUrl : `https://omnivore.app/me/${ article . slug } ` ,
205
208
siteName,
@@ -209,19 +212,16 @@ const fetchOmnivore = async (inBackground = false) => {
209
212
dateSaved,
210
213
content : article . content ,
211
214
datePublished,
212
- note : '' ,
215
+ note : note ?. annotation ,
213
216
}
217
+ // filter out notes and redactions
218
+ const highlights = article . highlights ?. filter (
219
+ ( h ) => h . type === HighlightType . Highlight
220
+ )
214
221
// sort highlights by location if selected in options
215
- highlightOrder === HighlightOrder . LOCATION &&
216
- article . highlights ?. sort ( ( a , b ) => {
222
+ if ( highlightOrder === HighlightOrder . LOCATION ) {
223
+ highlights ?. sort ( ( a , b ) => {
217
224
try {
218
- // if either highlight is not a highlight, put it at the end
219
- if (
220
- a . type !== HighlightType . Highlight ||
221
- b . type !== HighlightType . Highlight
222
- ) {
223
- return 1
224
- }
225
225
if ( article . pageType === PageType . File ) {
226
226
// sort by location in file
227
227
return compareHighlightsInFile ( a , b )
@@ -235,44 +235,33 @@ const fetchOmnivore = async (inBackground = false) => {
235
235
return compareHighlightsInFile ( a , b )
236
236
}
237
237
} )
238
+ }
238
239
const highlightBatch : IBatchBlock [ ] =
239
- ( article . highlights
240
- ?. map ( ( it ) => {
241
- const highlightType = it . type
242
- // filter out notes and redactions
243
- if ( highlightType !== HighlightType . Highlight ) {
244
- // add note variable to article template
245
- if ( highlightType === HighlightType . Note ) {
246
- articleView . note = it . annotation
247
- }
248
- return undefined
249
- }
250
- // Build content string based on template
251
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
252
- const content = render ( highlightTemplate , {
253
- ...articleView ,
254
- text : it . quote ,
255
- labels : it . labels ,
256
- highlightUrl : `https://omnivore.app/me/${ article . slug } #${ it . id } ` ,
257
- dateHighlighted : formatDate (
258
- new Date ( it . updatedAt ) ,
259
- preferredDateFormat
260
- ) ,
261
- } )
262
- const noteChild = it . annotation
263
- ? { content : it . annotation }
264
- : undefined
265
- return {
266
- content,
267
- children : noteChild ? [ noteChild ] : undefined ,
268
- properties : {
269
- id : it . id ,
270
- } ,
271
- }
240
+ highlights ?. map ( ( it ) => {
241
+ // Build content string based on template
242
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
243
+ const content = render ( highlightTemplate , {
244
+ ...articleVariables ,
245
+ text : it . quote ,
246
+ labels : it . labels ,
247
+ highlightUrl : `https://omnivore.app/me/${ article . slug } #${ it . id } ` ,
248
+ dateHighlighted : formatDate (
249
+ new Date ( it . updatedAt ) ,
250
+ preferredDateFormat
251
+ ) ,
272
252
} )
273
- . filter ( ( it ) => it !== undefined ) as IBatchBlock [ ] ) || [ ]
274
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
275
- const content = render ( articleTemplate , articleView )
253
+ const noteChild = it . annotation
254
+ ? { content : it . annotation }
255
+ : undefined
256
+ return {
257
+ content,
258
+ children : noteChild ? [ noteChild ] : undefined ,
259
+ properties : {
260
+ id : it . id ,
261
+ } ,
262
+ }
263
+ } ) || [ ]
264
+
276
265
let isNewArticle = true
277
266
// update existing block if article is already in the page
278
267
const existingBlocks = (
@@ -289,21 +278,22 @@ const fetchOmnivore = async (inBackground = false) => {
289
278
[(clojure.string/includes? ?c "${ article . slug } ")]]`
290
279
)
291
280
) . flat ( )
281
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
282
+ const articleContent = render ( articleTemplate , articleVariables )
292
283
if ( existingBlocks . length > 0 ) {
293
284
isNewArticle = false
294
285
const existingBlock = existingBlocks [ 0 ]
295
286
// update the first existing block
296
- if ( existingBlock . content !== content ) {
297
- await logseq . Editor . updateBlock ( existingBlock . uuid , content )
287
+ if ( existingBlock . content !== articleContent ) {
288
+ await logseq . Editor . updateBlock ( existingBlock . uuid , articleContent )
298
289
}
299
290
// delete the rest of the existing blocks
300
291
await deleteBlocks ( existingBlocks . slice ( 1 ) )
301
- if ( highlightBatch . length > 0 ) {
302
- // append highlights to existing block
303
- for ( const highlight of highlightBatch ) {
304
- const existingHighlights = (
305
- await logseq . DB . datascriptQuery < BlockEntity [ ] > (
306
- `[:find (pull ?b [*])
292
+ // append highlights to existing block
293
+ for ( const highlight of highlightBatch ) {
294
+ const existingHighlights = (
295
+ await logseq . DB . datascriptQuery < BlockEntity [ ] > (
296
+ `[:find (pull ?b [*])
307
297
:where
308
298
[?b :block/parent ?p]
309
299
[?p :block/uuid ?u]
@@ -313,23 +303,23 @@ const fetchOmnivore = async (inBackground = false) => {
313
303
[(clojure.string/includes? ?c "${
314
304
highlight . properties ?. id as string
315
305
} ")]]`
316
- )
317
- ) . flat ( )
318
- if ( existingHighlights . length > 0 ) {
319
- const existingHighlight = existingHighlights [ 0 ]
320
- // update existing highlight if content is different
321
- existingHighlight . content !== highlight . content &&
322
- ( await logseq . Editor . updateBlock (
323
- existingHighlight . uuid ,
324
- highlight . content
325
- ) )
326
-
327
- // checking notes
328
- const noteChild = highlight . children ?. [ 0 ]
329
- if ( noteChild ) {
330
- const existingNotes = (
331
- await logseq . DB . datascriptQuery < BlockEntity [ ] > (
332
- `[:find (pull ?b [*])
306
+ )
307
+ ) . flat ( )
308
+ if ( existingHighlights . length > 0 ) {
309
+ const existingHighlight = existingHighlights [ 0 ]
310
+ // update existing highlight if content is different
311
+ existingHighlight . content !== highlight . content &&
312
+ ( await logseq . Editor . updateBlock (
313
+ existingHighlight . uuid ,
314
+ highlight . content
315
+ ) )
316
+
317
+ // checking notes
318
+ const noteChild = highlight . children ?. [ 0 ]
319
+ if ( noteChild ) {
320
+ const existingNotes = (
321
+ await logseq . DB . datascriptQuery < BlockEntity [ ] > (
322
+ `[:find (pull ?b [*])
333
323
:where
334
324
[?b :block/parent ?p]
335
325
[?p :block/uuid ?u]
@@ -339,32 +329,31 @@ const fetchOmnivore = async (inBackground = false) => {
339
329
[(= ?c "${ escapeQuotationMarks (
340
330
noteChild . content
341
331
) } ")]]`
342
- )
343
- ) . flat ( )
344
- if ( existingNotes . length == 0 ) {
345
- // append new note
346
- await logseq . Editor . insertBlock (
347
- existingHighlight . uuid ,
348
- noteChild . content ,
349
- { sibling : false }
350
- )
351
- }
332
+ )
333
+ ) . flat ( )
334
+ if ( existingNotes . length == 0 ) {
335
+ // append new note
336
+ await logseq . Editor . insertBlock (
337
+ existingHighlight . uuid ,
338
+ noteChild . content ,
339
+ { sibling : false }
340
+ )
352
341
}
353
- } else {
354
- // append new highlight
355
- await logseq . Editor . insertBatchBlock (
356
- existingBlock . uuid ,
357
- highlight ,
358
- { sibling : false }
359
- )
360
342
}
343
+ } else {
344
+ // append new highlight
345
+ await logseq . Editor . insertBatchBlock (
346
+ existingBlock . uuid ,
347
+ highlight ,
348
+ { sibling : false }
349
+ )
361
350
}
362
351
}
363
352
}
364
353
365
354
isNewArticle &&
366
355
articleBatch . unshift ( {
367
- content,
356
+ content : articleContent ,
368
357
children : highlightBatch ,
369
358
} )
370
359
}
0 commit comments