From 49b2e511bdb0f3318efe12dd5ecc917ea6d8dc83 Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 28 Jun 2024 21:47:37 +0300 Subject: [PATCH] fix: new lines no longer break wrapped highlight blocks --- manifest.json | 4 ++-- package.json | 4 ++-- src/methods/aggregateDetails.ts | 16 +++++++++++++--- test/mocks/aggregatedDetailsData.ts | 18 ++++++++++++++++++ test/mocks/renderedTemplate.ts | 26 ++++++++++++++++++++++++-- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/manifest.json b/manifest.json index 37898dc..508e589 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { "id": "apple-books-import-highlights", "name": "Apple Books - Import Highlights", - "version": "1.2.3", + "version": "1.2.4", "minAppVersion": "0.15.0", "description": "Import your Apple Books highlights and notes to Obsidian.", "author": "bandantonio", "authorUrl": "https://github.com/bandantonio", "isDesktopOnly": true -} \ No newline at end of file +} diff --git a/package.json b/package.json index ad84445..7ccc668 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-apple-books-highlights-plugin", - "version": "1.2.3", + "version": "1.2.4", "description": "Import highlights and notes from your Apple Books to Obsidian", "main": "main.js", "scripts": { @@ -58,4 +58,4 @@ "dayjs": "^1.11.10", "handlebars": "^4.7.8" } -} \ No newline at end of file +} diff --git a/src/methods/aggregateDetails.ts b/src/methods/aggregateDetails.ts index 63a994a..bc584e8 100644 --- a/src/methods/aggregateDetails.ts +++ b/src/methods/aggregateDetails.ts @@ -25,11 +25,14 @@ export const aggregateBookAndHighlightDetails = async (): Promise { + const textForContext = annotation.ZANNOTATIONREPRESENTATIVETEXT; + const userNote = annotation.ZANNOTATIONNOTE; + return { chapter: annotation.ZFUTUREPROOFING5, - contextualText: annotation.ZANNOTATIONREPRESENTATIVETEXT, - highlight: annotation.ZANNOTATIONSELECTEDTEXT, - note: annotation.ZANNOTATIONNOTE, + contextualText: textForContext ? preserveNewlineIndentation(textForContext) : textForContext, + highlight: preserveNewlineIndentation(annotation.ZANNOTATIONSELECTEDTEXT), + note: userNote ? preserveNewlineIndentation(userNote) : userNote, highlightStyle: annotation.ZANNOTATIONSTYLE, highlightCreationDate: annotation.ZANNOTATIONCREATIONDATE, highlightModificationDate: annotation.ZANNOTATIONMODIFICATIONDATE @@ -43,3 +46,10 @@ export const aggregateBookAndHighlightDetails = async (): Promise { + const stringWithNewLines = /\n+\s*/g; + + return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; +} diff --git a/test/mocks/aggregatedDetailsData.ts b/test/mocks/aggregatedDetailsData.ts index 882cf07..8459df7 100644 --- a/test/mocks/aggregatedDetailsData.ts +++ b/test/mocks/aggregatedDetailsData.ts @@ -18,6 +18,16 @@ export const annotationsToAggregate = [{ "ZANNOTATIONMODIFICATIONDATE": 731876693.002279, "ZANNOTATIONSTYLE": 3, "ZANNOTATIONDELETED": 0 +}, { + "ZANNOTATIONASSETID": "THBFYNJKTGFTTVCGSAE5", + "ZFUTUREPROOFING5": "Another aggregated Introduction", + "ZANNOTATIONREPRESENTATIVETEXT": "This is a contextual text for the aggregated hightlight from the Apple iPhone User Guide\n\ncontaining a new line to test the preservation of indentation", + "ZANNOTATIONSELECTEDTEXT": "aggregated hightlight from the Apple iPhone User Guide\n\ncontaining a new line to test the preservation of indentation", + "ZANNOTATIONNOTE": "Test note for the aggregated hightlight from the Apple iPhone User Guide\n\nalong with a new line to test the preservation of indentation", + "ZANNOTATIONCREATIONDATE": 731876693.002279, + "ZANNOTATIONMODIFICATIONDATE": 731876693.002279, + "ZANNOTATIONSTYLE": 3, + "ZANNOTATIONDELETED": 0 }]; export const aggregatedHighlights = [{ @@ -36,5 +46,13 @@ export const aggregatedHighlights = [{ "highlightStyle": 3, "highlightCreationDate": 731876693.002279, "highlightModificationDate": 731876693.002279 + }, { + "chapter": "Another aggregated Introduction", + "contextualText": "This is a contextual text for the aggregated hightlight from the Apple iPhone User Guide\ncontaining a new line to test the preservation of indentation", + "highlight": "aggregated hightlight from the Apple iPhone User Guide\ncontaining a new line to test the preservation of indentation", + "note": "Test note for the aggregated hightlight from the Apple iPhone User Guide\nalong with a new line to test the preservation of indentation", + "highlightStyle": 3, + "highlightCreationDate": 731876693.002279, + "highlightModificationDate": 731876693.002279 }] }]; diff --git a/test/mocks/renderedTemplate.ts b/test/mocks/renderedTemplate.ts index adec8ee..2d0622b 100644 --- a/test/mocks/renderedTemplate.ts +++ b/test/mocks/renderedTemplate.ts @@ -4,7 +4,7 @@ Link:: [Apple Books Link](ibooks://assetid/THBFYNJKTGFTTVCGSAE5) ## Annotations -Number of annotations:: 1 +Number of annotations:: 2 ---- @@ -13,6 +13,16 @@ Number of annotations:: 1 - 🎯 Highlight:: aggregated hightlight from the Apple iPhone User Guide - 📝 Note:: Test note for the aggregated hightlight from the Apple iPhone User Guide +---- + +- 📖 Chapter:: Another aggregated Introduction +- 🔖 Context:: This is a contextual text for the aggregated hightlight from the Apple iPhone User Guide +containing a new line to test the preservation of indentation +- 🎯 Highlight:: aggregated hightlight from the Apple iPhone User Guide +containing a new line to test the preservation of indentation +- 📝 Note:: Test note for the aggregated hightlight from the Apple iPhone User Guide +along with a new line to test the preservation of indentation + `; export const renderedCustomTemplateMock = `Title:: 📕 Apple iPhone - User Guide - Instructions - with - restricted - symbols - in - title @@ -26,7 +36,7 @@ Link:: [Apple Books Link](ibooks://assetid/THBFYNJKTGFTTVCGSAE5) ## Annotations -Number of annotations:: 1 +Number of annotations:: 2 ---- @@ -37,4 +47,16 @@ Number of annotations:: 1 - 📅 Highlight taken on:: 2024-03-11 03:04:53 PM -04:00 - 📅 Highlight modified on:: 2024-03-11 03:04:53 PM -04:00 +---- + +- 📖 Chapter:: Another aggregated Introduction +- 🔖 Context:: This is a contextual text for the aggregated hightlight from the Apple iPhone User Guide +containing a new line to test the preservation of indentation +- 🎯 Highlight:: aggregated hightlight from the Apple iPhone User Guide +containing a new line to test the preservation of indentation +- 📝 Note:: Test note for the aggregated hightlight from the Apple iPhone User Guide +along with a new line to test the preservation of indentation +- 📅 Highlight taken on:: 2024-03-11 03:04:53 PM -04:00 +- 📅 Highlight modified on:: 2024-03-11 03:04:53 PM -04:00 + `;