@@ -10,8 +10,8 @@ import {
10
10
Article ,
11
11
compareHighlightsInFile ,
12
12
getHighlightLocation ,
13
- getHighlightPoint ,
14
13
loadArticles ,
14
+ markdownEscape ,
15
15
PageType ,
16
16
} from './util'
17
17
import { DateTime } from 'luxon'
@@ -130,7 +130,9 @@ const fetchOmnivore = async (inBackground = false) => {
130
130
const articleBatch : IBatchBlock [ ] = [ ]
131
131
for ( const article of articles ) {
132
132
// Build content string
133
- let content = `[${ article . title } ](https://omnivore.app/me/${ article . slug } )`
133
+ let content = `[**${ markdownEscape (
134
+ article . title
135
+ ) } **](https://omnivore.app/me/${ article . slug } )`
134
136
content += '\ncollapsed:: true'
135
137
136
138
const displaySiteName =
@@ -154,16 +156,6 @@ const fetchOmnivore = async (inBackground = false) => {
154
156
preferredDateFormat
155
157
) } `
156
158
157
- // remove existing block for the same article
158
- const existingBlocks = await logseq . DB . q < BlockEntity > (
159
- `"${ article . slug } "`
160
- )
161
- if ( existingBlocks ) {
162
- for ( const block of existingBlocks ) {
163
- block . uuid && ( await logseq . Editor . removeBlock ( block . uuid ) )
164
- }
165
- }
166
-
167
159
// sort highlights by location if selected in options
168
160
highlightOrder === HighlightOrder . LOCATION &&
169
161
article . highlights ?. sort ( ( a , b ) => {
@@ -181,21 +173,96 @@ const fetchOmnivore = async (inBackground = false) => {
181
173
return compareHighlightsInFile ( a , b )
182
174
}
183
175
} )
184
-
185
- const highlightBatch = article . highlights ?. map ( ( it ) => {
186
- const noteChild = it . annotation
187
- ? { content : it . annotation }
188
- : undefined
189
- return {
190
- content : `>> ${ it . quote } [⤴️](https://omnivore.app/me/${ article . slug } #${ it . id } )` ,
191
- children : noteChild ? [ noteChild ] : undefined ,
176
+ const highlightBatch : IBatchBlock [ ] =
177
+ article . highlights ?. map ( ( it ) => {
178
+ const noteChild = it . annotation
179
+ ? { content : it . annotation }
180
+ : undefined
181
+ return {
182
+ content : `>> ${ markdownEscape (
183
+ it . quote
184
+ ) } [⤴️](https://omnivore.app/me/${ article . slug } #${ it . id } )`,
185
+ children : noteChild ? [ noteChild ] : undefined ,
186
+ }
187
+ } ) || [ ]
188
+
189
+ let isNewArticle = true
190
+ // update existing block if article is already in the page
191
+ const existingBlocks = (
192
+ await logseq . DB . datascriptQuery < BlockEntity [ ] > (
193
+ `[:find (pull ?b [*])
194
+ :where
195
+ [?b :block/page ?p]
196
+ [?p :block/original-name "${ pageName } "]
197
+ [?b :block/content ?c]
198
+ [(clojure.string/includes? ?c "${ article . slug } ")]]`
199
+ )
200
+ ) . flat ( )
201
+ if ( existingBlocks . length > 0 ) {
202
+ isNewArticle = false
203
+ const existingBlock = existingBlocks [ 0 ]
204
+ // update existing block
205
+ if ( existingBlock . content !== content ) {
206
+ await logseq . Editor . updateBlock ( existingBlock . uuid , content )
192
207
}
193
- } )
208
+ if ( highlightBatch . length > 0 ) {
209
+ // append highlights to existing block
210
+ for ( const highlight of highlightBatch ) {
211
+ const existingHighlights = (
212
+ await logseq . DB . datascriptQuery < BlockEntity [ ] > (
213
+ `[:find (pull ?b [*])
214
+ :where
215
+ [?b :block/parent ?p]
216
+ [?p :block/uuid ?u]
217
+ [(str ?u) ?s]
218
+ [(= ?s "${ existingBlock . uuid } ")]
219
+ [?b :block/content ?c]
220
+ [(= ?c "${ highlight . content } ")]]`
221
+ )
222
+ ) . flat ( )
223
+ if ( existingHighlights . length > 0 ) {
224
+ const existingHighlight = existingHighlights [ 0 ]
225
+ // update existing highlight
226
+ const noteChild = highlight . children ?. [ 0 ]
227
+ if ( noteChild ) {
228
+ const existingNotes = (
229
+ await logseq . DB . datascriptQuery < BlockEntity [ ] > (
230
+ `[:find (pull ?b [*])
231
+ :where
232
+ [?b :block/parent ?p]
233
+ [?p :block/uuid ?u]
234
+ [(str ?u) ?s]
235
+ [(= ?s "${ existingHighlight . uuid } ")]
236
+ [?b :block/content ?c]
237
+ [(= ?c "${ noteChild . content } ")]]`
238
+ )
239
+ ) . flat ( )
240
+ if ( existingNotes . length == 0 ) {
241
+ // append new note
242
+ await logseq . Editor . insertBlock (
243
+ existingHighlight . uuid ,
244
+ noteChild . content ,
245
+ { sibling : false }
246
+ )
247
+ }
248
+ }
249
+ } else {
250
+ // append new highlight
251
+ await logseq . Editor . insertBatchBlock (
252
+ existingBlock . uuid ,
253
+ highlight ,
254
+ { sibling : false }
255
+ )
256
+ }
257
+ }
258
+ }
259
+ }
194
260
195
- articleBatch . unshift ( {
196
- content,
197
- children : highlightBatch ,
198
- } )
261
+ isNewArticle &&
262
+ articleBatch . unshift ( {
263
+ content,
264
+ children : highlightBatch ,
265
+ } )
199
266
}
200
267
201
268
articleBatch . length > 0 &&
0 commit comments