Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^10.2.0",
"prettier": "^2.5.1",
"vite": "^4.5.3",
"vite-plugin-logseq": "^1.1.2",
"semantic-release": "^19.0.3",
"typescript": "^4.4.3"
"typescript": "^4.4.3",
"vite": "^4.5.3",
"vite-plugin-logseq": "^1.1.2"
},
"volta": {
"node": "18.16.1",
Expand Down
84 changes: 52 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const fetchOmnivore = async (inBackground = false) => {
isSinglePage,
headingBlockTitle,
syncContent,
fixedHighlightId,
} = logseq.settings as Settings
// prevent multiple fetches
if (loading) {
Expand Down Expand Up @@ -242,22 +243,49 @@ const fetchOmnivore = async (inBackground = false) => {
const itemBatchBlocksMap: Map<string, IBatchBlock[]> = new Map()
for (const item of items) {
if (!isSinglePage) {
// create a new page for each article
pageName = replaceIllegalChars(
renderPageName(item, pageNameTemplate, preferredDateFormat)
)

if (fixedHighlightId) {
// If fixed highlight ID is enabled, delete existing page and recreate
const existingPage = await logseq.Editor.getPage(pageName)
if (existingPage) {
await logseq.Editor.deletePage(pageName)
console.log(`Deleted existing page: ${pageName}`)
}

const newPage = await logseq.Editor.createPage(pageName, {}, {
createFirstBlock: false,
redirect: false
})

if (!newPage) {
console.error(`Failed to create new page: ${pageName}`)
continue
}

targetBlockId = newPage.uuid
} else {
// If fixed highlight ID is not enabled, use the original logic
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)
}
} else {
targetBlockId = await getOmnivoreBlockIdentity(pageName, blockTitle)
}

const itemBatchBlocks = itemBatchBlocksMap.get(targetBlockId) || []
// render article
const renderedItem = renderItem(
articleTemplate,
item,
preferredDateFormat
)

// escape # to prevent creating subpages
const articleContent = item.content?.replaceAll('#', '\\#') || ''
console.log("articleContent")
console.log(articleContent)
// create original content title block
const contentBlock: IBatchBlock = {
content: contentTitle,
Expand Down Expand Up @@ -294,7 +322,6 @@ const fetchOmnivore = async (inBackground = false) => {
}
const highlightBatchBlocks: IBatchBlock[] =
highlights?.map((it) => {
// Render highlight content string based on highlight template
const content = renderHighlightContent(
highlightTemplate,
it,
Expand All @@ -303,9 +330,7 @@ const fetchOmnivore = async (inBackground = false) => {
)
return {
content,
properties: {
id: it.id,
},
properties: fixedHighlightId ? { id: it.id } : {}
}
}) || []

Expand Down Expand Up @@ -358,6 +383,7 @@ const fetchOmnivore = async (inBackground = false) => {
{
sibling: false,
before: true,
keepUUID: true
}
)
}
Expand All @@ -372,40 +398,33 @@ const fetchOmnivore = async (inBackground = false) => {
)
if (existingHighlightBlock) {
parentBlockId = existingHighlightBlock.uuid
// append new highlights to existing article block
// process highlights
for (const highlight of highlightBatchBlocks) {
// check if highlight block exists
const existingHighlightsBlock = await getBlockByContent(
pageName,
parentBlockId,
highlight.properties?.id as string
)
if (existingHighlightsBlock) {
// update existing highlight if content is different
if (existingHighlightsBlock.content !== highlight.content) {
await logseq.Editor.updateBlock(
existingHighlightsBlock.uuid,
highlight.content
const highlightId = highlight.properties?.id as string
if (highlightId) {
// Find existing highlight block with the same id
const existingHighlightBlock = await logseq.Editor.getBlockProperty(parentBlockId, highlightId)
if (existingHighlightBlock) {
// If exists, update content
await logseq.Editor.updateBlock(existingHighlightBlock.uuid, highlight.content)
} else {
// If not exists, create new highlight block
await logseq.Editor.insertBlock(
parentBlockId,
highlight.content,
{ properties: { id: highlight.properties?.id } } // Update id property
)
}
} else {
// append new highlights to existing article block
await logseq.Editor.insertBatchBlock(
parentBlockId,
highlight,
{
sibling: false,
}
)
}
}
} else {
// append new highlights block
// If highlight title block doesn't exist, create new highlight title block and its children
await logseq.Editor.insertBatchBlock(
existingItemBlock.uuid,
highlightsBlock,
{
sibling: false,
keepUUID: true
}
)
}
Expand All @@ -423,18 +442,19 @@ const fetchOmnivore = async (inBackground = false) => {
itemBatchBlocks.unshift({
content: renderedItem,
children,
properties: {
id: item.id,
},
properties: {},
})
itemBatchBlocksMap.set(targetBlockId, itemBatchBlocks)
console.log("itemBatchBlocksMap")
console.log(itemBatchBlocksMap)
}
}

for (const [targetBlockId, articleBatch] of itemBatchBlocksMap) {
await logseq.Editor.insertBatchBlock(targetBlockId, articleBatch, {
before: true,
sibling: false,
keepUUID: true
})
}

Expand Down
8 changes: 8 additions & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface Settings {
version: string
headingBlockTitle: string
syncContent: boolean
fixedHighlightId: boolean
}

export const getQueryFromFilter = (
Expand Down Expand Up @@ -205,4 +206,11 @@ export const settingsSchema = async (): Promise<SettingSchemaDesc[]> => [
),
default: 'https://api-prod.omnivore.app/api/graphql',
},
{
key: 'fixedHighlightId',
type: 'boolean',
default: false,
title: t('Fixed Highlight ID'),
description: t('If checked, highlight blocks will have fixed IDs. This will cause the entire page to be recreated when updating articles.'),
},
]
2 changes: 2 additions & 0 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type HighlightView =
text: string
labels?: Label[]
highlightUrl: string
highlightId: string
dateHighlighted: string
rawDateHighlighted: string
note?: string
Expand Down Expand Up @@ -184,6 +185,7 @@ export const renderHighlightContent = (
text: formatHighlightQuote(highlight.quote, template),
labels: highlight.labels || [],
highlightUrl: `https://omnivore.app/me/${item.slug}#${highlight.id}`,
highlightId: highlight.id,
dateHighlighted,
rawDateHighlighted,
note: highlight.annotation ?? undefined,
Expand Down
4 changes: 3 additions & 1 deletion src/translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@
"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.": "記事の内容をコンテンツブロックに同期します。これが選択されていない場合、ハイライトのみが同期されます。"
"Sync article content into the content block. If this is not selected, only highlights will be synced.": "記事の内容をコンテンツブロックに同期します。これが選択されていない場合、ハイライトのみが同期されます。",
"Fixed Highlight ID": "固定ハイライトID",
"If checked, highlight blocks will have fixed IDs. This will cause the entire page to be recreated when updating articles.": "チェックすると、ハイライトブロックに固定IDが付与されます。これにより、記事を更新する際にページ全体が再作成されます。"
}
4 changes: 3 additions & 1 deletion src/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@
"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.": "将文章内容同步到内容块。如果未选择此选项,将仅同步高亮。"
"Sync article content into the content block. If this is not selected, only highlights will be synced.": "将文章内容同步到内容块。如果未选择此选项,将仅同步高亮。",
"Fixed Highlight ID": "固定高亮ID",
"If checked, highlight blocks will have fixed IDs. This will cause the entire page to be recreated when updating articles.": "如果选中,高亮块将具有固定ID。这将导致更新文章时重新创建整个页面。"
}
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getHighlightLocation = (patch: string | null): number => {
}
const dmp = new diff_match_patch()
const patches = dmp.patch_fromText(patch)
return patches[0].start1 || 0
return (patches[0] as any).start1 || 0
}

export const getHighlightPoint = (patch: string | null): HighlightPoint => {
Expand Down