Skip to content

Commit 866528f

Browse files
committed
Remove note and redactions from highlights
1 parent df1ebea commit 866528f

File tree

1 file changed

+79
-90
lines changed

1 file changed

+79
-90
lines changed

src/index.ts

Lines changed: 79 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ const fetchOmnivore = async (inBackground = false) => {
198198
const datePublished = article.publishedAt
199199
? formatDate(new Date(article.publishedAt), preferredDateFormat)
200200
: undefined
201+
const note = article.highlights?.find(
202+
(h) => h.type === HighlightType.Note
203+
)
201204
// Build content string based on template
202-
const articleView = {
205+
const articleVariables = {
203206
title: article.title,
204207
omnivoreUrl: `https://omnivore.app/me/${article.slug}`,
205208
siteName,
@@ -209,19 +212,16 @@ const fetchOmnivore = async (inBackground = false) => {
209212
dateSaved,
210213
content: article.content,
211214
datePublished,
212-
note: '',
215+
note: note?.annotation,
213216
}
217+
// filter out notes and redactions
218+
const highlights = article.highlights?.filter(
219+
(h) => h.type === HighlightType.Highlight
220+
)
214221
// 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) => {
217224
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-
}
225225
if (article.pageType === PageType.File) {
226226
// sort by location in file
227227
return compareHighlightsInFile(a, b)
@@ -235,44 +235,33 @@ const fetchOmnivore = async (inBackground = false) => {
235235
return compareHighlightsInFile(a, b)
236236
}
237237
})
238+
}
238239
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+
),
272252
})
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+
276265
let isNewArticle = true
277266
// update existing block if article is already in the page
278267
const existingBlocks = (
@@ -289,21 +278,22 @@ const fetchOmnivore = async (inBackground = false) => {
289278
[(clojure.string/includes? ?c "${article.slug}")]]`
290279
)
291280
).flat()
281+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
282+
const articleContent = render(articleTemplate, articleVariables)
292283
if (existingBlocks.length > 0) {
293284
isNewArticle = false
294285
const existingBlock = existingBlocks[0]
295286
// 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)
298289
}
299290
// delete the rest of the existing blocks
300291
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 [*])
307297
:where
308298
[?b :block/parent ?p]
309299
[?p :block/uuid ?u]
@@ -313,23 +303,23 @@ const fetchOmnivore = async (inBackground = false) => {
313303
[(clojure.string/includes? ?c "${
314304
highlight.properties?.id as string
315305
}")]]`
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 [*])
333323
:where
334324
[?b :block/parent ?p]
335325
[?p :block/uuid ?u]
@@ -339,32 +329,31 @@ const fetchOmnivore = async (inBackground = false) => {
339329
[(= ?c "${escapeQuotationMarks(
340330
noteChild.content
341331
)}")]]`
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+
)
352341
}
353-
} else {
354-
// append new highlight
355-
await logseq.Editor.insertBatchBlock(
356-
existingBlock.uuid,
357-
highlight,
358-
{ sibling: false }
359-
)
360342
}
343+
} else {
344+
// append new highlight
345+
await logseq.Editor.insertBatchBlock(
346+
existingBlock.uuid,
347+
highlight,
348+
{ sibling: false }
349+
)
361350
}
362351
}
363352
}
364353

365354
isNewArticle &&
366355
articleBatch.unshift({
367-
content,
356+
content: articleContent,
368357
children: highlightBatch,
369358
})
370359
}

0 commit comments

Comments
 (0)