Skip to content

Commit 578ff3b

Browse files
committed
fix: sort highlights by location in the file if an error is catched
1 parent d5e412c commit 578ff3b

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/index.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { getDateForPage } from 'logseq-dateutils'
99
import {
1010
Article,
11+
compareHighlightsInFile,
1112
getHighlightLocation,
1213
getHighlightPoint,
1314
loadArticles,
@@ -166,19 +167,19 @@ const fetchOmnivore = async (inBackground = false) => {
166167
// sort highlights by location if selected in options
167168
highlightOrder === HighlightOrder.LOCATION &&
168169
article.highlights?.sort((a, b) => {
169-
if (article.pageType === PageType.File) {
170-
// get the position of the highlight in the file
171-
const highlightPointA = getHighlightPoint(a.patch)
172-
const highlightPointB = getHighlightPoint(b.patch)
173-
if (highlightPointA.top === highlightPointB.top) {
174-
// if top is same, sort by left
175-
return highlightPointA.left - highlightPointB.left
170+
try {
171+
if (article.pageType === PageType.File) {
172+
// sort by location in file
173+
return compareHighlightsInFile(a, b)
176174
}
177-
// sort by top
178-
return highlightPointA.top - highlightPointB.top
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)
179182
}
180-
// for web page, sort by location in the page
181-
return getHighlightLocation(a.patch) - getHighlightLocation(b.patch)
182183
})
183184

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

src/util.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,15 @@ export const getHighlightPoint = (patch: string): HighlightPoint => {
115115
}
116116
return { left: bbox[0], top: bbox[1] }
117117
}
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)