From 0df230ab6643369ec586b215c72b7e52e0cd07f1 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 10 Nov 2023 15:18:00 +0800 Subject: [PATCH] fix: importing article {{{ content }}} stops at first section (#154) * fix: sync article content into another block if turned on in the settings * add localization for zh-CN * fix: add Japanese localization --- src/api.ts | 2 +- src/index.ts | 88 +++++++++++++++++++++++++++---------- src/settings/index.ts | 10 +++++ src/settings/template.ts | 4 +- src/translations/ja.json | 7 ++- src/translations/zh-CN.json | 7 ++- 6 files changed, 88 insertions(+), 30 deletions(-) diff --git a/src/api.ts b/src/api.ts index b400072..54e7c98 100644 --- a/src/api.ts +++ b/src/api.ts @@ -51,7 +51,7 @@ export interface Article { updatedAt: string savedAt: string pageType: PageType - content: string + content?: string publishedAt?: string readAt?: string readingProgressPercent: number diff --git a/src/index.ts b/src/index.ts index a2957e4..29c41c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +22,7 @@ import { } from './settings' import { preParseTemplate, - renderArticleContent, + renderArticle, renderHighlightContent, renderPageName, } from './settings/template' @@ -167,6 +167,7 @@ const fetchOmnivore = async (inBackground = false) => { endpoint, isSinglePage, headingBlockTitle, + syncContent, } = logseq.settings as Settings // prevent multiple fetches if (loading) { @@ -205,6 +206,7 @@ const fetchOmnivore = async (inBackground = false) => { const blockTitle = t(headingBlockTitle) const fetchingTitle = t('🚀 Fetching articles ...') const highlightTitle = t('### Highlights') + const contentTitle = t('### Content') const userConfigs = await logseq.App.getUserConfigs() const preferredDateFormat: string = userConfigs.preferredDateFormat @@ -243,11 +245,11 @@ const fetchOmnivore = async (inBackground = false) => { size, parseDateTime(syncAt).toISO(), getQueryFromFilter(filter, customQuery), - true, + syncContent, 'highlightedMarkdown', endpoint ) - const articleBatchMap: Map = new Map() + const articleBatchBlockMap: Map = new Map() for (const article of articles) { if (!isSinglePage) { // create a new page for each article @@ -256,13 +258,23 @@ const fetchOmnivore = async (inBackground = false) => { ) targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle) } - const articleBatch = articleBatchMap.get(targetBlockId) || [] - // render article content - const articleContent = renderArticleContent( + const articleBatchBlock = articleBatchBlockMap.get(targetBlockId) || [] + // render article + const renderedArticle = renderArticle( articleTemplate, article, preferredDateFormat ) + // create original content title block + const contentBlock: IBatchBlock = { + content: contentTitle, + children: [ + { + // escape # to prevent creating subpages + content: article.content?.replaceAll('#', '\\#') || '', + }, + ], + } // filter out notes and redactions const highlights = article.highlights?.filter( (h) => h.type === HighlightType.Highlight @@ -303,7 +315,7 @@ const fetchOmnivore = async (inBackground = false) => { }) || [] // create highlight title block - const highlightTitleBlock: IBatchBlock = { + const highlightBlock: IBatchBlock = { content: highlightTitle, children: highlightBatch, } @@ -315,7 +327,7 @@ const fetchOmnivore = async (inBackground = false) => { ) if (existingArticleBlock) { const existingArticleProperties = existingArticleBlock.properties - const newArticleProperties = parseBlockProperties(articleContent) + const newArticleProperties = parseBlockProperties(renderedArticle) // update the existing article block if any of the properties have changed if ( isBlockPropertiesChanged( @@ -325,22 +337,43 @@ const fetchOmnivore = async (inBackground = false) => { ) { await logseq.Editor.updateBlock( existingArticleBlock.uuid, - articleContent + renderedArticle + ) + } + if (syncContent) { + // delete existing content block + const existingContentBlock = await getBlockByContent( + pageName, + existingArticleBlock.uuid, + contentTitle + ) + if (existingContentBlock) { + await logseq.Editor.removeBlock(existingContentBlock.uuid) + } + + // append new content block + await logseq.Editor.insertBatchBlock( + existingArticleBlock.uuid, + contentBlock, + { + sibling: false, + before: true, + } ) } if (highlightBatch.length > 0) { let parentBlockId = existingArticleBlock.uuid // check if highlight title block exists - const existingHighlightTitleBlock = await getBlockByContent( + const existingHighlightBlock = await getBlockByContent( pageName, parentBlockId, - highlightTitleBlock.content + highlightBlock.content ) - if (existingHighlightTitleBlock) { - parentBlockId = existingHighlightTitleBlock.uuid + if (existingHighlightBlock) { + parentBlockId = existingHighlightBlock.uuid } else { // append new highlight title block - const newHighlightTitleBlock = await logseq.Editor.insertBlock( + const newHighlightBlock = await logseq.Editor.insertBlock( existingArticleBlock.uuid, highlightTitle, { @@ -348,7 +381,7 @@ const fetchOmnivore = async (inBackground = false) => { before: true, } ) - if (newHighlightTitleBlock) { + if (newHighlightBlock) { const existingArticleBlockWithChildren = await logseq.Editor.getBlock(existingArticleBlock.uuid, { includeChildren: true, @@ -360,13 +393,13 @@ const fetchOmnivore = async (inBackground = false) => { for (const highlight of existingHighlightBlocks) { await logseq.Editor.moveBlock( highlight.uuid, - newHighlightTitleBlock.uuid, + newHighlightBlock.uuid, { children: true, } ) } - parentBlockId = newHighlightTitleBlock.uuid + parentBlockId = newHighlightBlock.uuid } } // append new highlights to existing article block @@ -394,16 +427,27 @@ const fetchOmnivore = async (inBackground = false) => { } } } else { + const children = [] + + // add content block if sync content is selected + syncContent && children.push(contentBlock) + + // add highlight title block if there are highlights + highlightBatch.length > 0 && children.push(highlightBlock) + // append new article block - articleBatch.unshift({ - content: articleContent, - children: highlightBatch.length > 0 ? [highlightTitleBlock] : [], // add highlight title block if there are highlights + articleBatchBlock.unshift({ + content: renderedArticle, + children, + properties: { + id: article.id, + }, }) - articleBatchMap.set(targetBlockId, articleBatch) + articleBatchBlockMap.set(targetBlockId, articleBatchBlock) } } - for (const [targetBlockId, articleBatch] of articleBatchMap) { + for (const [targetBlockId, articleBatch] of articleBatchBlockMap) { await logseq.Editor.insertBatchBlock(targetBlockId, articleBatch, { before: true, sibling: false, diff --git a/src/settings/index.ts b/src/settings/index.ts index f80428e..ea77e02 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -31,6 +31,7 @@ export interface Settings { isSinglePage: boolean version: string headingBlockTitle: string + syncContent: boolean } export const getQueryFromFilter = ( @@ -136,6 +137,15 @@ export const settingsSchema = async (): Promise => [ default: defaultHighlightTemplate, inputAs: 'textarea', }, + { + key: 'syncContent', + type: 'boolean', + title: t('Sync article content'), + description: t( + 'Sync article content into the content block. If this is not selected, only highlights will be synced.' + ), + default: false, + }, { key: 'advancedSettings', type: 'heading', diff --git a/src/settings/template.ts b/src/settings/template.ts index 67ee884..660f47e 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -22,7 +22,6 @@ export type ArticleView = author?: string omnivoreUrl: string siteName: string - content: string originalUrl: string note?: string type: PageType @@ -155,7 +154,6 @@ const createArticleView = ( author: article.author, labels: article.labels, dateSaved, - content: article.content, datePublished, note: note?.annotation ?? undefined, type: article.pageType, @@ -170,7 +168,7 @@ const createArticleView = ( } } -export const renderArticleContent = ( +export const renderArticle = ( template: string, article: Article, preferredDateFormat: string diff --git a/src/translations/ja.json b/src/translations/ja.json index 48012f4..0d5ab5d 100644 --- a/src/translations/ja.json +++ b/src/translations/ja.json @@ -43,5 +43,8 @@ "Advanced Settings": "詳細設定", "API Endpoint": "APIエンドポイント", "Enter the Omnivore server's API endpoint here (default: https://api-prod.omnivore.app/api/graphql )": "OmnivoreサーバーのAPIエンドポイントをここに入力してください(デフォルト: https://api-prod.omnivore.app/api/graphql )", - "Failed to create Omnivore page. Please check the pageName in the settings": "Omnivoreページの作成に失敗しました。設定のページ名を確認してください" -} \ No newline at end of file + "Failed to create Omnivore page. Please check the pageName in the settings": "Omnivoreページの作成に失敗しました。設定のページ名を確認してください", + "### Content": "### 内容", + "Sync article content": "記事の内容を同期する", + "Sync article content into the content block. If this is not selected, only highlights will be synced.": "記事の内容をコンテンツブロックに同期します。これが選択されていない場合、ハイライトのみが同期されます。" +} diff --git a/src/translations/zh-CN.json b/src/translations/zh-CN.json index a4561c5..d917e34 100644 --- a/src/translations/zh-CN.json +++ b/src/translations/zh-CN.json @@ -43,5 +43,8 @@ "Advanced Settings": "高级设置", "API Endpoint": "API 终点", "Enter the Omnivore server's API endpoint here (default: https://api-prod.omnivore.app/api/graphql )": "在此处输入 Omnivore 服务器的 API 终点(默认: https://api-prod.omnivore.app/api/graphql )", - "Failed to create Omnivore page. Please check the pageName in the settings": "创建 Omnivore 页面失败。请检查设置中的页面名称" -} \ No newline at end of file + "Failed to create Omnivore page. Please check the pageName in the settings": "创建 Omnivore 页面失败。请检查设置中的页面名称", + "### Content": "### 内容", + "Sync article content": "同步文章内容", + "Sync article content into the content block. If this is not selected, only highlights will be synced.": "将文章内容同步到内容块。如果未选择此选项,将仅同步高亮。" +}