From 4e517f4839182ab78d199a3bc2beab6fc3ecd297 Mon Sep 17 00:00:00 2001 From: Utsob Roy Date: Sat, 23 Apr 2022 00:13:36 +0600 Subject: [PATCH] [plugin] Exporter: properly sort highlights when exporting (#9021) Since the bookmarks table is sorted based on the bookmarks' position in the book, it is possible to sort clippings in the exporter plugin based on that. --- plugins/exporter.koplugin/clip.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/exporter.koplugin/clip.lua b/plugins/exporter.koplugin/clip.lua index e1e000722..69ef11ca3 100644 --- a/plugins/exporter.koplugin/clip.lua +++ b/plugins/exporter.koplugin/clip.lua @@ -286,7 +286,15 @@ function MyClipping:parseHighlight(highlights, bookmarks, book) end end end - table.sort(book, function(v1, v2) return v1[1].page < v2[1].page end) + -- A table to map bookmarks timestamp to index in the bookmarks table + -- to facilitate sorting clippings by their position in the book + -- since highlights are not sorted by position while bookmarks are. + local bookmark_indexes = {} + for i, bookmark in ipairs(bookmarks) do + bookmark_indexes[self:getTime(bookmark.datetime)] = i + end + -- Sort clippings by their position in the book. + table.sort(book, function(v1, v2) return bookmark_indexes[v1[1].time] > bookmark_indexes[v2[1].time] end) end function MyClipping:parseHistoryFile(clippings, history_file, doc_file)