Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 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,8 @@ 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)
console.log('targetBlockId', targetBlockId)
!inBackground && logseq.App.pushState('page', { name: pageName })
}

Expand Down Expand Up @@ -245,7 +255,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 +334,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 +404,8 @@ const fetchOmnivore = async (inBackground = false) => {
}
}

for await (const [targetBlockId, articleBatch] of articleBatchMap) {
for (const [targetBlockId, articleBatch] of articleBatchMap) {
console.log('targetBlockId', targetBlockId)
await logseq.Editor.insertBatchBlock(targetBlockId, articleBatch, {
before: true,
sibling: false,
Expand All @@ -414,14 +425,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