Skip to content

Commit

Permalink
Merge pull request #145 from omnivore-app/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
sywhb authored Oct 25, 2023
2 parents 21df637 + dddb771 commit 98f2102
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 75 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"scripts": {
"dev": "parcel public/index.html --public-url ./",
"build": "parcel build --public-url . --no-source-maps public/index.html",
"test": "yarn mocha -r ts-node/register tests/*.ts"
"test": "yarn mocha -r ts-node/register tests/*.ts",
"lint": "eslint --ext .ts src"
},
"license": "MIT",
"logseq": {
Expand All @@ -24,7 +25,7 @@
"icon": "./public/icon.png"
},
"dependencies": {
"@logseq/libs": "^0.0.14",
"@logseq/libs": "^0.0.15",
"date-fns": "^2.29.3",
"diff-match-patch": "^1.0.5",
"lodash": "^4.17.21",
Expand Down
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface Highlight {
labels?: Label[]
type: HighlightType
highlightPositionPercent?: number
color?: string
}

const ENDPOINT = 'https://api-prod.omnivore.app/api/graphql'
Expand Down Expand Up @@ -136,6 +137,7 @@ export const getOmnivoreArticles = async (
name
}
type
color
}
labels {
name
Expand Down
81 changes: 50 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {
IBatchBlock,
LSPluginBaseInfo,
} from '@logseq/libs/dist/LSPlugin'
import { setup as l10nSetup, t } from "logseq-l10n" //https://github.com/sethyuan/logseq-l10n
import ja from "./translations/ja.json"
import zhCN from "./translations/zh-CN.json"
import { PageEntity } from '@logseq/libs/dist/LSPlugin.user'
import { setup as l10nSetup, t } from 'logseq-l10n' //https://github.com/sethyuan/logseq-l10n
import { DateTime } from 'luxon'
import {
Article,
Expand All @@ -28,6 +26,8 @@ import {
renderHighlightContent,
renderPageName,
} from './settings/template'
import ja from './translations/ja.json'
import zhCN from './translations/zh-CN.json'
import {
DATE_FORMAT,
compareHighlightsInFile,
Expand Down Expand Up @@ -115,7 +115,9 @@ const getOmnivorePage = async (pageName: string): Promise<PageEntity> => {
})
if (!newOmnivorePage) {
await logseq.UI.showMsg(
t("Failed to create Omnivore page. Please check the pageName in the settings"),
t(
'Failed to create Omnivore page. Please check the pageName in the settings'
),
'error'
)
throw new Error('Failed to create Omnivore page')
Expand All @@ -124,22 +126,30 @@ const getOmnivorePage = async (pageName: string): Promise<PageEntity> => {
return newOmnivorePage
}

const getOmnivoreBlock = async (
const getOmnivoreBlockIdentity = async (
pageName: string,
title: string
): Promise<BlockEntity> => {
): Promise<string> => {
const page = await getOmnivorePage(pageName)
if (!title) {
// return the page uuid if no title is provided
return page.uuid
}

const targetBlock = await getBlockByContent(pageName, page.uuid, title)
if (targetBlock) {
return targetBlock
return targetBlock.uuid
}
const newTargetBlock = await logseq.Editor.appendBlockInPage(page.uuid, title)
const newTargetBlock = await logseq.Editor.prependBlockInPage(
page.uuid,
title
)
if (!newTargetBlock) {
await logseq.UI.showMsg(t("Failed to create Omnivore block"), 'error')
throw new Error('Failed to create Omnivore block')
await logseq.UI.showMsg(t('Failed to create Omnivore block'), 'error')
throw new Error('Failed to create block')
}

return newTargetBlock
return newTargetBlock.uuid
}

const fetchOmnivore = async (inBackground = false) => {
Expand All @@ -156,18 +166,19 @@ const fetchOmnivore = async (inBackground = false) => {
loading,
endpoint,
isSinglePage,
headingBlockTitle,
} = logseq.settings as Settings
// prevent multiple fetches
if (loading) {
await logseq.UI.showMsg(t("Omnivore is already syncing"), 'warning', {
await logseq.UI.showMsg(t('Omnivore is already syncing'), 'warning', {
timeout: 3000,
})
return
}
logseq.updateSettings({ loading: true })

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

if (!(await isValidCurrentGraph())) {
await logseq.UI.showMsg(
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."),
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.'),
'error'
)

return
}

const blockTitle = t("## 🔖 Articles")
const fetchingTitle = t("🚀 Fetching articles ...")
const highlightTitle = t("### Highlights")
const blockTitle = t(headingBlockTitle)
const fetchingTitle = t('🚀 Fetching articles ...')
const highlightTitle = t('### Highlights')

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

Expand Down Expand Up @@ -239,7 +254,7 @@ const fetchOmnivore = async (inBackground = false) => {
pageName = replaceIllegalChars(
renderPageName(article, pageNameTemplate, preferredDateFormat)
)
targetBlockId = (await getOmnivoreBlock(pageName, blockTitle)).uuid
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)
}
const articleBatch = articleBatchMap.get(targetBlockId) || []
// render article content
Expand Down Expand Up @@ -318,7 +333,7 @@ const fetchOmnivore = async (inBackground = false) => {
// check if highlight title block exists
const existingHighlightTitleBlock = await getBlockByContent(
pageName,
existingArticleBlock.uuid,
parentBlockId,
highlightTitleBlock.content
)
if (existingHighlightTitleBlock) {
Expand Down Expand Up @@ -388,7 +403,7 @@ const fetchOmnivore = async (inBackground = false) => {
}
}

for await (const [targetBlockId, articleBatch] of articleBatchMap) {
for (const [targetBlockId, articleBatch] of articleBatchMap) {
await logseq.Editor.insertBatchBlock(targetBlockId, articleBatch, {
before: true,
sibling: false,
Expand All @@ -408,14 +423,14 @@ const fetchOmnivore = async (inBackground = false) => {
parseDateTime(syncAt).toISO(),
endpoint
)
for await (const deletedArticle of deletedArticles) {
for (const deletedArticle of deletedArticles) {
if (!isSinglePage) {
pageName = renderPageName(
deletedArticle,
pageNameTemplate,
preferredDateFormat
)
targetBlockId = (await getOmnivoreBlock(pageName, blockTitle)).uuid
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)

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

if (!inBackground) {
logseq.UI.closeMsg(fetchingMsgKey)
await logseq.UI.showMsg(t("🔖 Articles fetched"), 'success', {
await logseq.UI.showMsg(t('🔖 Articles fetched'), 'success', {
timeout: 2000,
})
}
logseq.updateSettings({ syncAt: DateTime.local().toFormat(DATE_FORMAT) })
} catch (e) {
!inBackground &&
(await logseq.UI.showMsg(t("Failed to fetch articles"), 'error'))
(await logseq.UI.showMsg(t('Failed to fetch articles'), 'error'))
console.error(e)
} finally {
resetLoadingState()
Expand All @@ -460,7 +475,7 @@ const fetchOmnivore = async (inBackground = false) => {
const main = async (baseInfo: LSPluginBaseInfo) => {
console.log('logseq-omnivore loaded')

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

logseq.useSettingsSchema(await settingsSchema())
// update version if needed
Expand All @@ -469,9 +484,13 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
if (latestVersion !== currentVersion) {
logseq.updateSettings({ version: latestVersion })
// show release notes
const releaseNotes = `${t("Omnivore plugin is upgraded to")} ${latestVersion}.
const releaseNotes = `${t(
'Omnivore plugin is upgraded to'
)} ${latestVersion}.
${t("What's new")}: https://github.com/omnivore-app/logseq-omnivore/blob/main/CHANGELOG.md
${t(
"What's new"
)}: https://github.com/omnivore-app/logseq-omnivore/blob/main/CHANGELOG.md
`
await logseq.UI.showMsg(releaseNotes, 'success', {
timeout: 10000,
Expand Down Expand Up @@ -515,7 +534,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
logseq.App.registerCommandPalette(
{
key: 'omnivore-sync',
label: t("Sync Omnivore"),
label: t('Sync Omnivore'),
},
() => {
void (async () => {
Expand All @@ -527,13 +546,13 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
logseq.App.registerCommandPalette(
{
key: 'omnivore-resync',
label: t("Resync all Omnivore articles"),
label: t('Resync all Omnivore articles'),
},
() => {
void (async () => {
// reset the last sync time
logseq.updateSettings({ syncAt: '' })
await logseq.UI.showMsg(t("Omnivore Last Sync reset"), 'warning', {
await logseq.UI.showMsg(t('Omnivore Last Sync reset'), 'warning', {
timeout: 3000,
})

Expand Down
Loading

0 comments on commit 98f2102

Please sign in to comment.