Skip to content

Commit 8993b49

Browse files
authored
Merge pull request #48 from omnivore-app/fix/update-existing-block
fix/update existing block
2 parents b7a38bc + 8d8af29 commit 8993b49

File tree

4 files changed

+125
-26
lines changed

4 files changed

+125
-26
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"@logseq/libs": "^0.0.6",
2727
"diff-match-patch": "^1.0.5",
2828
"logseq-dateutils": "^0.0.22",
29-
"luxon": "^3.0.1"
29+
"luxon": "^3.0.1",
30+
"markdown-escape": "^1.1.0"
3031
},
3132
"devDependencies": {
3233
"@semantic-release/changelog": "^6.0.1",
@@ -35,6 +36,7 @@
3536
"@tsconfig/node16": "^1.0.1",
3637
"@types/diff-match-patch": "^1.0.32",
3738
"@types/luxon": "^2.3.2",
39+
"@types/markdown-escape": "^1.1.0",
3840
"@typescript-eslint/eslint-plugin": "^5.9.0",
3941
"@typescript-eslint/parser": "^5.9.0",
4042
"eslint": "^8.6.0",

src/index.ts

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
Article,
1111
compareHighlightsInFile,
1212
getHighlightLocation,
13-
getHighlightPoint,
1413
loadArticles,
14+
markdownEscape,
1515
PageType,
1616
} from './util'
1717
import { DateTime } from 'luxon'
@@ -130,7 +130,9 @@ const fetchOmnivore = async (inBackground = false) => {
130130
const articleBatch: IBatchBlock[] = []
131131
for (const article of articles) {
132132
// 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})`
134136
content += '\ncollapsed:: true'
135137

136138
const displaySiteName =
@@ -154,16 +156,6 @@ const fetchOmnivore = async (inBackground = false) => {
154156
preferredDateFormat
155157
)}`
156158

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-
167159
// sort highlights by location if selected in options
168160
highlightOrder === HighlightOrder.LOCATION &&
169161
article.highlights?.sort((a, b) => {
@@ -181,21 +173,96 @@ const fetchOmnivore = async (inBackground = false) => {
181173
return compareHighlightsInFile(a, b)
182174
}
183175
})
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)
192207
}
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+
}
194260

195-
articleBatch.unshift({
196-
content,
197-
children: highlightBatch,
198-
})
261+
isNewArticle &&
262+
articleBatch.unshift({
263+
content,
264+
children: highlightBatch,
265+
})
199266
}
200267

201268
articleBatch.length > 0 &&

src/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { diff_match_patch } from 'diff-match-patch'
2+
import escape from 'markdown-escape'
23

34
export interface GetArticleResponse {
45
data: {
@@ -127,3 +128,12 @@ export const compareHighlightsInFile = (a: Highlight, b: Highlight): number => {
127128
// sort by top
128129
return highlightPointA.top - highlightPointB.top
129130
}
131+
132+
export const markdownEscape = (text: string): string => {
133+
try {
134+
return escape(text)
135+
} catch (e) {
136+
console.error('markdownEscape error', e)
137+
return text
138+
}
139+
}

yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,11 @@
12101210
resolved "https://registry.yarnpkg.com/@types/diff-match-patch/-/diff-match-patch-1.0.32.tgz#d9c3b8c914aa8229485351db4865328337a3d09f"
12111211
integrity sha512-bPYT5ECFiblzsVzyURaNhljBH2Gh1t9LowgUwciMrNAhFewLkHT2H0Mto07Y4/3KCOGZHRQll3CTtQZ0X11D/A==
12121212

1213+
"@types/escape-latex@^0.1.30":
1214+
version "0.1.30"
1215+
resolved "https://registry.yarnpkg.com/@types/escape-latex/-/escape-latex-0.1.30.tgz#574f71a2716febffeb64a60a16c3a8c4804340fa"
1216+
integrity sha512-HRKSkPNGJuevkoDr8uuTRYUM9YIjEE9X7e7fP0l7uK6r1LUvnTFmYI2TrdSrZPrAPyoBUaq7KPC4c05204x0qQ==
1217+
12131218
"@types/json-schema@^7.0.9":
12141219
version "7.0.11"
12151220
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz"
@@ -1220,6 +1225,11 @@
12201225
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-2.3.2.tgz#8a3f2cdd4858ce698b56cd8597d9243b8e9d3c65"
12211226
integrity sha512-WOehptuhKIXukSUUkRgGbj2c997Uv/iUgYgII8U7XLJqq9W2oF0kQ6frEznRQbdurioz+L/cdaIm4GutTQfgmA==
12221227

1228+
"@types/markdown-escape@^1.1.0":
1229+
version "1.1.0"
1230+
resolved "https://registry.yarnpkg.com/@types/markdown-escape/-/markdown-escape-1.1.0.tgz#8bd940d6701880eb7229467542c029bc05a7b22f"
1231+
integrity sha512-bJZh73L6fSt8MmTON/btR8hiKRipoIoPfLO41lxr6l65QXdG1rwUjR8Hu5xU7XYHBwKHiJZ+sTQ8L2TKkQrBpw==
1232+
12231233
"@types/minimist@^1.2.0":
12241234
version "1.2.2"
12251235
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"
@@ -2122,6 +2132,11 @@ escalade@^3.1.1:
21222132
resolved "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz"
21232133
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==
21242134

2135+
escape-latex@^1.2.0:
2136+
version "1.2.0"
2137+
resolved "https://registry.yarnpkg.com/escape-latex/-/escape-latex-1.2.0.tgz#07c03818cf7dac250cce517f4fda1b001ef2bca1"
2138+
integrity sha512-nV5aVWW1K0wEiUIEdZ4erkGGH8mDxGyxSeqPzRNtWP7ataw+/olFObw7hujFWlVjNsaDFw5VZ5NzVSIqRgfTiw==
2139+
21252140
escape-string-regexp@^1.0.5:
21262141
version "1.0.5"
21272142
resolved "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz"
@@ -3254,6 +3269,11 @@ map-obj@^4.0.0:
32543269
resolved "https://registry.npmjs.org/map-obj/-/map-obj-4.3.0.tgz"
32553270
integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ==
32563271

3272+
markdown-escape@^1.1.0:
3273+
version "1.1.0"
3274+
resolved "https://registry.yarnpkg.com/markdown-escape/-/markdown-escape-1.1.0.tgz#228c932a47af251bb0a2d18d6001a235403777f2"
3275+
integrity sha512-f1+ARFbzLrBdC0Lj30uREn+zthrK/h1PO5UhN5IMDQvI2lSFn+8U06a5LHaxxYMhHD0mJoJ2BROJ/Sju5aw6+g==
3276+
32573277
marked-terminal@^5.0.0:
32583278
version "5.1.1"
32593279
resolved "https://registry.npmjs.org/marked-terminal/-/marked-terminal-5.1.1.tgz"

0 commit comments

Comments
 (0)