Skip to content

Commit

Permalink
feat: allow defining custom title of heading block in the settings or…
Browse files Browse the repository at this point in the history
… leave blank to not create one (#144)

* feat: allow defining custom title of heading block in the settings or leave blank to not create one

* remove some debugging logs
  • Loading branch information
sywhb committed Oct 25, 2023
1 parent 3877519 commit dddb771
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,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')
throw new Error('Failed to create block')
}

return newTargetBlock
return newTargetBlock.uuid
}

const fetchOmnivore = async (inBackground = false) => {
Expand All @@ -158,6 +166,7 @@ const fetchOmnivore = async (inBackground = false) => {
loading,
endpoint,
isSinglePage,
headingBlockTitle,
} = logseq.settings as Settings
// prevent multiple fetches
if (loading) {
Expand Down Expand Up @@ -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')

Expand All @@ -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 })
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Settings {
endpoint: string
isSinglePage: boolean
version: string
headingBlockTitle: string
}

export const getQueryFromFilter = (
Expand Down Expand Up @@ -174,6 +175,17 @@ export const settingsSchema = async (): Promise<SettingSchemaDesc[]> => [
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',
Expand Down

0 comments on commit dddb771

Please sign in to comment.