From c1ab57bb932f1bcb973455807460dd6923ab770c Mon Sep 17 00:00:00 2001 From: Abdullah Malek <19255312+absorpheus@users.noreply.github.com> Date: Thu, 11 Jul 2024 16:41:54 +0100 Subject: [PATCH] fix: remove new lines from the end of contextual text blocks --- README.md | 13 ++++++----- src/methods/aggregateDetails.ts | 3 ++- src/utils/removeAllLastNewLines.ts | 6 +++++ test/removeAllLastNewlines.spec.ts | 36 ++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/utils/removeAllLastNewLines.ts create mode 100644 test/removeAllLastNewlines.spec.ts diff --git a/README.md b/README.md index 5cf9acf..d152c92 100644 --- a/README.md +++ b/README.md @@ -125,12 +125,13 @@ Number of annotations:: {{annotations.length}} - 📖 Chapter:: {{#if chapter}}{{{chapter}}}{{else}}N/A{{/if}} - 🔖 Context:: {{#if contextualText}}{{{contextualText}}}{{else}}N/A{{/if}} -{{#if (eq highlightStyle "0")}}- 🎯 Highlight:: {{{highlight}}}{{/if}} -{{#if (eq highlightStyle "1")}}- 🎯 Highlight:: {{{highlight}}}{{/if}} -{{#if (eq highlightStyle "2")}}- 🎯 Highlight:: {{{highlight}}}{{/if}} -{{#if (eq highlightStyle "3")}}- 🎯 Highlight:: {{{highlight}}}{{/if}} -{{#if (eq highlightStyle "4")}}- 🎯 Highlight:: {{{highlight}}}{{/if}} -{{#if (eq highlightStyle "5")}}- 🎯 Highlight:: {{{highlight}}}{{/if}} +{{#if (eq highlightStyle "0")}}- 🎯 Highlight:: {{{highlight}}} +{{else if (eq highlightStyle "1")}}- 🎯 Highlight:: {{{highlight}}} +{{else if (eq highlightStyle "2")}}- 🎯 Highlight:: {{{highlight}}} +{{else if (eq highlightStyle "3")}}- 🎯 Highlight:: {{{highlight}}} +{{else if (eq highlightStyle "4")}}- 🎯 Highlight:: {{{highlight}}} +{{else if (eq highlightStyle "5")}}- 🎯 Highlight:: {{{highlight}}} +{{/if}} - 📝 Note:: {{#if note}}{{{note}}}{{else}}N/A{{/if}} - 📅 Highlight taken on:: {{dateFormat highlightCreationDate "YYYY-MM-DD hh:mm:ss A Z"}} - 📅 Highlight modified on:: {{dateFormat highlightModificationDate "YYYY-MM-DD hh:mm:ss A Z"}} diff --git a/src/methods/aggregateDetails.ts b/src/methods/aggregateDetails.ts index bc584e8..713a277 100644 --- a/src/methods/aggregateDetails.ts +++ b/src/methods/aggregateDetails.ts @@ -1,6 +1,7 @@ import { IBook, IBookAnnotation, ICombinedBooksAndHighlights } from '../types'; import { getBooks } from './getBooks'; import { getAnnotations } from './getAnnotations'; +import { removeAllLastNewlines } from 'src/utils/removeAllLastNewLines'; export const aggregateBookAndHighlightDetails = async (): Promise => { const books = await getBooks(); @@ -30,7 +31,7 @@ export const aggregateBookAndHighlightDetails = async (): Promise { + const stringAllLastNewLines = /\n+$/; + + return stringAllLastNewLines.test(textBlock) ? textBlock.replace(stringAllLastNewLines, "") : textBlock; +} diff --git a/test/removeAllLastNewlines.spec.ts b/test/removeAllLastNewlines.spec.ts new file mode 100644 index 0000000..18445a8 --- /dev/null +++ b/test/removeAllLastNewlines.spec.ts @@ -0,0 +1,36 @@ +import { describe, expect, test } from 'vitest' +import { removeAllLastNewlines } from 'src/utils/removeAllLastNewLines' + +describe('removeAllLastNewlines', () => { + test('Should remove a newline character at the end of text', () => { + const contextualText = "This is an example text to test the removal of a newline character at the end of the text\n" + const actual = removeAllLastNewlines(contextualText) + const expected = "This is an example text to test the removal of a newline character at the end of the text" + + expect(actual).toEqual(expected) + }) + + test('Should remove multiple newline characters at the end of text', () => { + const contextualText = "This is an example text to test the removal of multiple newline characters at the end of the text\n\n" + const actual = removeAllLastNewlines(contextualText) + const expected = "This is an example text to test the removal of multiple newline characters at the end of the text" + + expect(actual).toEqual(expected) + }) + + test('Should preserve all other newline characters', () => { + const contextualText = "This is an example text to test the removal of a newline character at the end of the text\n while preserving other newline characters\n" + const actual = removeAllLastNewlines(contextualText) + const expected = "This is an example text to test the removal of a newline character at the end of the text\n while preserving other newline characters" + + expect(actual).toEqual(expected) + }) + + test('Should return the text when no newline characters exist at the end of the text', () => { + const contextualText = "This is an example text to test that the text is returned when no newline characters exist at the end of the text" + const actual = removeAllLastNewlines(contextualText) + const expected = "This is an example text to test that the text is returned when no newline characters exist at the end of the text" + + expect(actual).toEqual(expected) + }) +})