diff --git a/src/index.ts b/src/index.ts index a2e0bbb..a2957e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -126,22 +126,30 @@ const getOmnivorePage = async (pageName: string): Promise => { return newOmnivorePage } -const getOmnivoreBlock = async ( +const getOmnivoreBlockIdentity = async ( pageName: string, title: string -): Promise => { +): Promise => { 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') + throw new Error('Failed to create block') } - return newTargetBlock + return newTargetBlock.uuid } const fetchOmnivore = async (inBackground = false) => { @@ -158,6 +166,7 @@ const fetchOmnivore = async (inBackground = false) => { loading, endpoint, isSinglePage, + headingBlockTitle, } = logseq.settings as Settings // prevent multiple fetches if (loading) { @@ -193,7 +202,7 @@ const fetchOmnivore = async (inBackground = false) => { return } - const blockTitle = t('## 🔖 Articles') + const blockTitle = t(headingBlockTitle) const fetchingTitle = t('🚀 Fetching articles ...') const highlightTitle = t('### Highlights') @@ -214,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 }) } @@ -245,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 @@ -324,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) { @@ -394,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, @@ -414,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) diff --git a/src/settings/index.ts b/src/settings/index.ts index d205c33..0396c53 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -30,6 +30,7 @@ export interface Settings { endpoint: string isSinglePage: boolean version: string + headingBlockTitle: string } export const getQueryFromFilter = ( @@ -174,6 +175,17 @@ export const settingsSchema = async (): Promise => [ description: t('This page will be created if it does not exist.'), default: 'Omnivore', }, + { + key: 'headingBlockTitle', + type: 'string', + title: t( + 'Enter the title of the heading block to place synced articles under' + ), + description: t( + 'This heading block will be created if it does not exist. Default is "## 🔖 Articles". Leave blank to not create a heading block.' + ), + default: '## 🔖 Articles', + }, { key: 'endpoint', type: 'string',