diff --git a/plugins/exporter.koplugin/clip.lua b/plugins/exporter.koplugin/clip.lua index 195a2f1a4..64f3e130d 100644 --- a/plugins/exporter.koplugin/clip.lua +++ b/plugins/exporter.koplugin/clip.lua @@ -256,6 +256,7 @@ function MyClipping:parseHighlight(highlights, bookmarks, book) clipping.time = self:getTime(item.datetime or "") clipping.text = self:getText(item.text) clipping.chapter = item.chapter + clipping.drawer = item.drawer for _, bookmark in pairs(bookmarks) do if bookmark.datetime == item.datetime and bookmark.text then local bookmark_quote = bookmark.text:match(pattern) diff --git a/plugins/exporter.koplugin/main.lua b/plugins/exporter.koplugin/main.lua index bf7b93a5c..02cff0502 100644 --- a/plugins/exporter.koplugin/main.lua +++ b/plugins/exporter.koplugin/main.lua @@ -609,10 +609,47 @@ function Exporter:exportBooknotesToHTML(title, booknotes) end end +function Exporter:prepareBooknotesForJSON(booknotes) + local exportable = { + title = booknotes.title, + author = booknotes.author, + entries = {}, + exported = booknotes.exported, + file = booknotes.file + } + for _, entry in ipairs(booknotes) do + table.insert(exportable.entries, entry[1]) + end + return exportable +end + +-- This function should handle both multidocument export and single exports. +-- For Single Exports, it will create a JSON file with a object ({}) as root node. +-- For Multidocument export, it will create a JSON file with an array ([]) as root node. +function Exporter:exportToJSON(clippings) + local file = io.open(self.json_clipping_file, "a") + if file then + local exportable = {} + if table.getn(clippings) == 1 then + -- We will handle single document export here. + exportable = self:prepareBooknotesForJSON(clippings[0]) + else + for _, booknotes in ipairs(clippings) do + table.insert(exportable, self:prepareBooknotesForJSON(booknotes)) + end + end + file:write(json.encode(exportable)) + file:write("\n") + file:close() + end +end + function Exporter:exportBooknotesToJSON(title, booknotes) + logger.dbg("booknotes", booknotes) local file = io.open(self.json_clipping_file, "a") if file then - file:write(json.encode(booknotes)) + local exportable = self:prepareBooknotesForJSON(booknotes) + file:write(json.encode(exportable)) file:write("\n") file:close() end