Skip to content

Commit 98f2102

Browse files
authored
Merge pull request #145 from omnivore-app/develop
Release
2 parents 21df637 + dddb771 commit 98f2102

6 files changed

Lines changed: 121 additions & 75 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"scripts": {
1616
"dev": "parcel public/index.html --public-url ./",
1717
"build": "parcel build --public-url . --no-source-maps public/index.html",
18-
"test": "yarn mocha -r ts-node/register tests/*.ts"
18+
"test": "yarn mocha -r ts-node/register tests/*.ts",
19+
"lint": "eslint --ext .ts src"
1920
},
2021
"license": "MIT",
2122
"logseq": {
@@ -24,7 +25,7 @@
2425
"icon": "./public/icon.png"
2526
},
2627
"dependencies": {
27-
"@logseq/libs": "^0.0.14",
28+
"@logseq/libs": "^0.0.15",
2829
"date-fns": "^2.29.3",
2930
"diff-match-patch": "^1.0.5",
3031
"lodash": "^4.17.21",

src/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface Highlight {
7979
labels?: Label[]
8080
type: HighlightType
8181
highlightPositionPercent?: number
82+
color?: string
8283
}
8384

8485
const ENDPOINT = 'https://api-prod.omnivore.app/api/graphql'
@@ -136,6 +137,7 @@ export const getOmnivoreArticles = async (
136137
name
137138
}
138139
type
140+
color
139141
}
140142
labels {
141143
name

src/index.ts

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import {
44
IBatchBlock,
55
LSPluginBaseInfo,
66
} from '@logseq/libs/dist/LSPlugin'
7-
import { setup as l10nSetup, t } from "logseq-l10n" //https://github.com/sethyuan/logseq-l10n
8-
import ja from "./translations/ja.json"
9-
import zhCN from "./translations/zh-CN.json"
107
import { PageEntity } from '@logseq/libs/dist/LSPlugin.user'
8+
import { setup as l10nSetup, t } from 'logseq-l10n' //https://github.com/sethyuan/logseq-l10n
119
import { DateTime } from 'luxon'
1210
import {
1311
Article,
@@ -28,6 +26,8 @@ import {
2826
renderHighlightContent,
2927
renderPageName,
3028
} from './settings/template'
29+
import ja from './translations/ja.json'
30+
import zhCN from './translations/zh-CN.json'
3131
import {
3232
DATE_FORMAT,
3333
compareHighlightsInFile,
@@ -115,7 +115,9 @@ const getOmnivorePage = async (pageName: string): Promise<PageEntity> => {
115115
})
116116
if (!newOmnivorePage) {
117117
await logseq.UI.showMsg(
118-
t("Failed to create Omnivore page. Please check the pageName in the settings"),
118+
t(
119+
'Failed to create Omnivore page. Please check the pageName in the settings'
120+
),
119121
'error'
120122
)
121123
throw new Error('Failed to create Omnivore page')
@@ -124,22 +126,30 @@ const getOmnivorePage = async (pageName: string): Promise<PageEntity> => {
124126
return newOmnivorePage
125127
}
126128

127-
const getOmnivoreBlock = async (
129+
const getOmnivoreBlockIdentity = async (
128130
pageName: string,
129131
title: string
130-
): Promise<BlockEntity> => {
132+
): Promise<string> => {
131133
const page = await getOmnivorePage(pageName)
134+
if (!title) {
135+
// return the page uuid if no title is provided
136+
return page.uuid
137+
}
138+
132139
const targetBlock = await getBlockByContent(pageName, page.uuid, title)
133140
if (targetBlock) {
134-
return targetBlock
141+
return targetBlock.uuid
135142
}
136-
const newTargetBlock = await logseq.Editor.appendBlockInPage(page.uuid, title)
143+
const newTargetBlock = await logseq.Editor.prependBlockInPage(
144+
page.uuid,
145+
title
146+
)
137147
if (!newTargetBlock) {
138-
await logseq.UI.showMsg(t("Failed to create Omnivore block"), 'error')
139-
throw new Error('Failed to create Omnivore block')
148+
await logseq.UI.showMsg(t('Failed to create Omnivore block'), 'error')
149+
throw new Error('Failed to create block')
140150
}
141151

142-
return newTargetBlock
152+
return newTargetBlock.uuid
143153
}
144154

145155
const fetchOmnivore = async (inBackground = false) => {
@@ -156,18 +166,19 @@ const fetchOmnivore = async (inBackground = false) => {
156166
loading,
157167
endpoint,
158168
isSinglePage,
169+
headingBlockTitle,
159170
} = logseq.settings as Settings
160171
// prevent multiple fetches
161172
if (loading) {
162-
await logseq.UI.showMsg(t("Omnivore is already syncing"), 'warning', {
173+
await logseq.UI.showMsg(t('Omnivore is already syncing'), 'warning', {
163174
timeout: 3000,
164175
})
165176
return
166177
}
167178
logseq.updateSettings({ loading: true })
168179

169180
if (!apiKey) {
170-
await logseq.UI.showMsg(t("Missing Omnivore api key"), 'warning', {
181+
await logseq.UI.showMsg(t('Missing Omnivore api key'), 'warning', {
171182
timeout: 3000,
172183
}).then(() => {
173184
logseq.showSettingsUI()
@@ -180,16 +191,20 @@ const fetchOmnivore = async (inBackground = false) => {
180191

181192
if (!(await isValidCurrentGraph())) {
182193
await logseq.UI.showMsg(
183-
t("Omnivore is configured to sync into your \"") + graph + t("\" graph which is not currently active.\nPlease switch to graph \"") + graph + t("\" to sync Omnivore articles."),
194+
t('Omnivore is configured to sync into your "') +
195+
graph +
196+
t('" graph which is not currently active.\nPlease switch to graph "') +
197+
graph +
198+
t('" to sync Omnivore articles.'),
184199
'error'
185200
)
186201

187202
return
188203
}
189204

190-
const blockTitle = t("## 🔖 Articles")
191-
const fetchingTitle = t("🚀 Fetching articles ...")
192-
const highlightTitle = t("### Highlights")
205+
const blockTitle = t(headingBlockTitle)
206+
const fetchingTitle = t('🚀 Fetching articles ...')
207+
const highlightTitle = t('### Highlights')
193208

194209
const userConfigs = await logseq.App.getUserConfigs()
195210
const preferredDateFormat: string = userConfigs.preferredDateFormat
@@ -208,7 +223,7 @@ const fetchOmnivore = async (inBackground = false) => {
208223
if (isSinglePage) {
209224
// create a single page for all articles
210225
pageName = pageNameTemplate
211-
targetBlockId = (await getOmnivoreBlock(pageName, blockTitle)).uuid
226+
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)
212227
!inBackground && logseq.App.pushState('page', { name: pageName })
213228
}
214229

@@ -239,7 +254,7 @@ const fetchOmnivore = async (inBackground = false) => {
239254
pageName = replaceIllegalChars(
240255
renderPageName(article, pageNameTemplate, preferredDateFormat)
241256
)
242-
targetBlockId = (await getOmnivoreBlock(pageName, blockTitle)).uuid
257+
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)
243258
}
244259
const articleBatch = articleBatchMap.get(targetBlockId) || []
245260
// render article content
@@ -318,7 +333,7 @@ const fetchOmnivore = async (inBackground = false) => {
318333
// check if highlight title block exists
319334
const existingHighlightTitleBlock = await getBlockByContent(
320335
pageName,
321-
existingArticleBlock.uuid,
336+
parentBlockId,
322337
highlightTitleBlock.content
323338
)
324339
if (existingHighlightTitleBlock) {
@@ -388,7 +403,7 @@ const fetchOmnivore = async (inBackground = false) => {
388403
}
389404
}
390405

391-
for await (const [targetBlockId, articleBatch] of articleBatchMap) {
406+
for (const [targetBlockId, articleBatch] of articleBatchMap) {
392407
await logseq.Editor.insertBatchBlock(targetBlockId, articleBatch, {
393408
before: true,
394409
sibling: false,
@@ -408,14 +423,14 @@ const fetchOmnivore = async (inBackground = false) => {
408423
parseDateTime(syncAt).toISO(),
409424
endpoint
410425
)
411-
for await (const deletedArticle of deletedArticles) {
426+
for (const deletedArticle of deletedArticles) {
412427
if (!isSinglePage) {
413428
pageName = renderPageName(
414429
deletedArticle,
415430
pageNameTemplate,
416431
preferredDateFormat
417432
)
418-
targetBlockId = (await getOmnivoreBlock(pageName, blockTitle)).uuid
433+
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)
419434

420435
// delete page if article is synced to a separate page and page is not a journal
421436
const existingPage = await logseq.Editor.getPage(pageName)
@@ -439,14 +454,14 @@ const fetchOmnivore = async (inBackground = false) => {
439454

440455
if (!inBackground) {
441456
logseq.UI.closeMsg(fetchingMsgKey)
442-
await logseq.UI.showMsg(t("🔖 Articles fetched"), 'success', {
457+
await logseq.UI.showMsg(t('🔖 Articles fetched'), 'success', {
443458
timeout: 2000,
444459
})
445460
}
446461
logseq.updateSettings({ syncAt: DateTime.local().toFormat(DATE_FORMAT) })
447462
} catch (e) {
448463
!inBackground &&
449-
(await logseq.UI.showMsg(t("Failed to fetch articles"), 'error'))
464+
(await logseq.UI.showMsg(t('Failed to fetch articles'), 'error'))
450465
console.error(e)
451466
} finally {
452467
resetLoadingState()
@@ -460,7 +475,7 @@ const fetchOmnivore = async (inBackground = false) => {
460475
const main = async (baseInfo: LSPluginBaseInfo) => {
461476
console.log('logseq-omnivore loaded')
462477

463-
await l10nSetup({ builtinTranslations: { ja, "zh-CN": zhCN } }); // logseq-l10n setup (translations)
478+
await l10nSetup({ builtinTranslations: { ja, 'zh-CN': zhCN } }) // logseq-l10n setup (translations)
464479

465480
logseq.useSettingsSchema(await settingsSchema())
466481
// update version if needed
@@ -469,9 +484,13 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
469484
if (latestVersion !== currentVersion) {
470485
logseq.updateSettings({ version: latestVersion })
471486
// show release notes
472-
const releaseNotes = `${t("Omnivore plugin is upgraded to")} ${latestVersion}.
487+
const releaseNotes = `${t(
488+
'Omnivore plugin is upgraded to'
489+
)} ${latestVersion}.
473490
474-
${t("What's new")}: https://github.com/omnivore-app/logseq-omnivore/blob/main/CHANGELOG.md
491+
${t(
492+
"What's new"
493+
)}: https://github.com/omnivore-app/logseq-omnivore/blob/main/CHANGELOG.md
475494
`
476495
await logseq.UI.showMsg(releaseNotes, 'success', {
477496
timeout: 10000,
@@ -515,7 +534,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
515534
logseq.App.registerCommandPalette(
516535
{
517536
key: 'omnivore-sync',
518-
label: t("Sync Omnivore"),
537+
label: t('Sync Omnivore'),
519538
},
520539
() => {
521540
void (async () => {
@@ -527,13 +546,13 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
527546
logseq.App.registerCommandPalette(
528547
{
529548
key: 'omnivore-resync',
530-
label: t("Resync all Omnivore articles"),
549+
label: t('Resync all Omnivore articles'),
531550
},
532551
() => {
533552
void (async () => {
534553
// reset the last sync time
535554
logseq.updateSettings({ syncAt: '' })
536-
await logseq.UI.showMsg(t("Omnivore Last Sync reset"), 'warning', {
555+
await logseq.UI.showMsg(t('Omnivore Last Sync reset'), 'warning', {
537556
timeout: 3000,
538557
})
539558

0 commit comments

Comments
 (0)