Skip to content

Commit 3145d27

Browse files
authored
Merge pull request #46 from omnivore-app/fix/retrieving-highlights-from-pdf
fix: failed to sort highlights in the file
2 parents 1c23ac6 + 578ff3b commit 3145d27

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import {
66
SettingSchemaDesc,
77
} from '@logseq/libs/dist/LSPlugin'
88
import { getDateForPage } from 'logseq-dateutils'
9-
import { Article, getHighlightLocation, loadArticles } from './util'
9+
import {
10+
Article,
11+
compareHighlightsInFile,
12+
getHighlightLocation,
13+
getHighlightPoint,
14+
loadArticles,
15+
PageType,
16+
} from './util'
1017
import { DateTime } from 'luxon'
1118

1219
enum Filter {
@@ -160,7 +167,19 @@ const fetchOmnivore = async (inBackground = false) => {
160167
// sort highlights by location if selected in options
161168
highlightOrder === HighlightOrder.LOCATION &&
162169
article.highlights?.sort((a, b) => {
163-
return getHighlightLocation(a.patch) - getHighlightLocation(b.patch)
170+
try {
171+
if (article.pageType === PageType.File) {
172+
// sort by location in file
173+
return compareHighlightsInFile(a, b)
174+
}
175+
// for web page, sort by location in the page
176+
return (
177+
getHighlightLocation(a.patch) - getHighlightLocation(b.patch)
178+
)
179+
} catch (e) {
180+
console.error(e)
181+
return compareHighlightsInFile(a, b)
182+
}
164183
})
165184

166185
const highlightBatch = article.highlights?.map((it) => {

src/util.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ export interface SearchResponse {
1919
}
2020
}
2121

22+
export enum PageType {
23+
Article = 'ARTICLE',
24+
Book = 'BOOK',
25+
File = 'FILE',
26+
Profile = 'PROFILE',
27+
Unknown = 'UNKNOWN',
28+
Website = 'WEBSITE',
29+
Highlights = 'HIGHLIGHTS',
30+
}
31+
2232
export interface Article {
2333
title: string
2434
siteName: string
@@ -30,6 +40,7 @@ export interface Article {
3040
highlights?: Highlight[]
3141
updatedAt: string
3242
savedAt: string
43+
pageType: PageType
3344
}
3445

3546
export interface Label {
@@ -43,6 +54,11 @@ export interface Highlight {
4354
patch: string
4455
}
4556

57+
export interface HighlightPoint {
58+
left: number
59+
top: number
60+
}
61+
4662
const endpoint = 'https://api-prod.omnivore.app/api/graphql'
4763

4864
export const loadArticle = async (
@@ -74,7 +90,7 @@ export const loadArticles = async (
7490
'content-type': 'application/json',
7591
authorization: apiKey,
7692
},
77-
body: `{"query":"\\n query Search($after: String, $first: Int, $query: String) {\\n search(first: $first, after: $after, query: $query) {\\n ... on SearchSuccess {\\n edges {\\n node {\\n title\\n slug\\n siteName\\n originalArticleUrl\\n url\\n author\\n updatedAt\\n description\\n savedAt\\n highlights {\\n id\\n quote\\n annotation\\n patch\\n }\\n labels {\\n name\\n }\\n }\\n }\\n pageInfo {\\n hasNextPage\\n }\\n }\\n ... on SearchError {\\n errorCodes\\n }\\n }\\n }\\n ","variables":{"after":"${after}","first":${first}, "query":"${
93+
body: `{"query":"\\n query Search($after: String, $first: Int, $query: String) {\\n search(first: $first, after: $after, query: $query) {\\n ... on SearchSuccess {\\n edges {\\n node {\\n title\\n slug\\n siteName\\n originalArticleUrl\\n url\\n author\\n updatedAt\\n description\\n savedAt\\n pageType\\n highlights {\\n id\\n quote\\n annotation\\n patch\\n }\\n labels {\\n name\\n }\\n }\\n }\\n pageInfo {\\n hasNextPage\\n }\\n }\\n ... on SearchError {\\n errorCodes\\n }\\n }\\n }\\n ","variables":{"after":"${after}","first":${first}, "query":"${
7894
updatedAt ? 'updated:' + updatedAt : ''
7995
} sort:saved-asc ${query}"}}`,
8096
method: 'POST',
@@ -91,3 +107,23 @@ export const getHighlightLocation = (patch: string): number => {
91107
const patches = dmp.patch_fromText(patch)
92108
return patches[0].start1 || 0
93109
}
110+
111+
export const getHighlightPoint = (patch: string): HighlightPoint => {
112+
const { bbox } = JSON.parse(patch) as { bbox: number[] }
113+
if (!bbox || bbox.length !== 4) {
114+
return { left: 0, top: 0 }
115+
}
116+
return { left: bbox[0], top: bbox[1] }
117+
}
118+
119+
export const compareHighlightsInFile = (a: Highlight, b: Highlight): number => {
120+
// get the position of the highlight in the file
121+
const highlightPointA = getHighlightPoint(a.patch)
122+
const highlightPointB = getHighlightPoint(b.patch)
123+
if (highlightPointA.top === highlightPointB.top) {
124+
// if top is same, sort by left
125+
return highlightPointA.left - highlightPointB.left
126+
}
127+
// sort by top
128+
return highlightPointA.top - highlightPointB.top
129+
}

0 commit comments

Comments
 (0)