Skip to content

Commit

Permalink
fix: importing article {{{ content }}} stops at first section (#154)
Browse files Browse the repository at this point in the history
* fix: sync article content into another block if turned on in the settings

* add localization for zh-CN

* fix: add Japanese localization
  • Loading branch information
sywhb committed Nov 10, 2023
1 parent b5944e1 commit 0df230a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface Article {
updatedAt: string
savedAt: string
pageType: PageType
content: string
content?: string
publishedAt?: string
readAt?: string
readingProgressPercent: number
Expand Down
88 changes: 66 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './settings'
import {
preParseTemplate,
renderArticleContent,
renderArticle,
renderHighlightContent,
renderPageName,
} from './settings/template'
Expand Down Expand Up @@ -167,6 +167,7 @@ const fetchOmnivore = async (inBackground = false) => {
endpoint,
isSinglePage,
headingBlockTitle,
syncContent,
} = logseq.settings as Settings
// prevent multiple fetches
if (loading) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -243,11 +245,11 @@ const fetchOmnivore = async (inBackground = false) => {
size,
parseDateTime(syncAt).toISO(),
getQueryFromFilter(filter, customQuery),
true,
syncContent,
'highlightedMarkdown',
endpoint
)
const articleBatchMap: Map<string, IBatchBlock[]> = new Map()
const articleBatchBlockMap: Map<string, IBatchBlock[]> = new Map()
for (const article of articles) {
if (!isSinglePage) {
// create a new page for each article
Expand All @@ -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
Expand Down Expand Up @@ -303,7 +315,7 @@ const fetchOmnivore = async (inBackground = false) => {
}) || []

// create highlight title block
const highlightTitleBlock: IBatchBlock = {
const highlightBlock: IBatchBlock = {
content: highlightTitle,
children: highlightBatch,
}
Expand All @@ -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(
Expand All @@ -325,30 +337,51 @@ 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,
{
sibling: false,
before: true,
}
)
if (newHighlightTitleBlock) {
if (newHighlightBlock) {
const existingArticleBlockWithChildren =
await logseq.Editor.getBlock(existingArticleBlock.uuid, {
includeChildren: true,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Settings {
isSinglePage: boolean
version: string
headingBlockTitle: string
syncContent: boolean
}

export const getQueryFromFilter = (
Expand Down Expand Up @@ -136,6 +137,15 @@ export const settingsSchema = async (): Promise<SettingSchemaDesc[]> => [
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',
Expand Down
4 changes: 1 addition & 3 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export type ArticleView =
author?: string
omnivoreUrl: string
siteName: string
content: string
originalUrl: string
note?: string
type: PageType
Expand Down Expand Up @@ -155,7 +154,6 @@ const createArticleView = (
author: article.author,
labels: article.labels,
dateSaved,
content: article.content,
datePublished,
note: note?.annotation ?? undefined,
type: article.pageType,
Expand All @@ -170,7 +168,7 @@ const createArticleView = (
}
}

export const renderArticleContent = (
export const renderArticle = (
template: string,
article: Article,
preferredDateFormat: string
Expand Down
7 changes: 5 additions & 2 deletions src/translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -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ページの作成に失敗しました。設定のページ名を確認してください"
}
"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.": "記事の内容をコンテンツブロックに同期します。これが選択されていない場合、ハイライトのみが同期されます。"
}
7 changes: 5 additions & 2 deletions src/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 页面失败。请检查设置中的页面名称"
}
"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.": "将文章内容同步到内容块。如果未选择此选项,将仅同步高亮。"
}

0 comments on commit 0df230a

Please sign in to comment.