Skip to content

Commit

Permalink
fix: remove new lines from the end of contextual text blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
absorpheus committed Jul 11, 2024
1 parent dc78460 commit c1ab57b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:: <u>{{{highlight}}}</u>{{/if}}
{{#if (eq highlightStyle "1")}}- 🎯 Highlight:: <mark style="background:rgb(175,213,151); color:#000; padding:2px;">{{{highlight}}}</mark>{{/if}}
{{#if (eq highlightStyle "2")}}- 🎯 Highlight:: <mark style="background:rgb(181,205,238); color:#000; padding:2px;">{{{highlight}}}</mark>{{/if}}
{{#if (eq highlightStyle "3")}}- 🎯 Highlight:: <mark style="background:rgb(249,213,108); color:#000; padding:2px;">{{{highlight}}}</mark>{{/if}}
{{#if (eq highlightStyle "4")}}- 🎯 Highlight:: <mark style="background:rgb(242,178,188); color:#000; padding:2px;">{{{highlight}}}</mark>{{/if}}
{{#if (eq highlightStyle "5")}}- 🎯 Highlight:: <mark style="background:rgb(214,192,238); color:#000; padding:2px;">{{{highlight}}}</mark>{{/if}}
{{#if (eq highlightStyle "0")}}- 🎯 Highlight:: <u>{{{highlight}}}</u>
{{else if (eq highlightStyle "1")}}- 🎯 Highlight:: <mark style="background:rgb(175,213,151); color:#000; padding:2px;">{{{highlight}}}</mark>
{{else if (eq highlightStyle "2")}}- 🎯 Highlight:: <mark style="background:rgb(181,205,238); color:#000; padding:2px;">{{{highlight}}}</mark>
{{else if (eq highlightStyle "3")}}- 🎯 Highlight:: <mark style="background:rgb(249,213,108); color:#000; padding:2px;">{{{highlight}}}</mark>
{{else if (eq highlightStyle "4")}}- 🎯 Highlight:: <mark style="background:rgb(242,178,188); color:#000; padding:2px;">{{{highlight}}}</mark>
{{else if (eq highlightStyle "5")}}- 🎯 Highlight:: <mark style="background:rgb(214,192,238); color:#000; padding:2px;">{{{highlight}}}</mark>
{{/if}}
- 📝 Note:: {{#if note}}{{{note}}}{{else}}N/A{{/if}}
- <small>📅 Highlight taken on:: {{dateFormat highlightCreationDate "YYYY-MM-DD hh:mm:ss A Z"}}</small>
- <small>📅 Highlight modified on:: {{dateFormat highlightModificationDate "YYYY-MM-DD hh:mm:ss A Z"}}</small>
Expand Down
3 changes: 2 additions & 1 deletion src/methods/aggregateDetails.ts
Original file line number Diff line number Diff line change
@@ -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<ICombinedBooksAndHighlights[]> => {
const books = await getBooks();
Expand Down Expand Up @@ -30,7 +31,7 @@ export const aggregateBookAndHighlightDetails = async (): Promise<ICombinedBooks

return {
chapter: annotation.ZFUTUREPROOFING5,
contextualText: textForContext ? preserveNewlineIndentation(textForContext) : textForContext,
contextualText: textForContext ? removeAllLastNewlines(preserveNewlineIndentation(textForContext)) : textForContext,
highlight: preserveNewlineIndentation(annotation.ZANNOTATIONSELECTEDTEXT),
note: userNote ? preserveNewlineIndentation(userNote) : userNote,
highlightStyle: annotation.ZANNOTATIONSTYLE,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/removeAllLastNewLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Handler of all new line characters (\n) at the end of text blocks to prevent new lines appearing at the end of text blocks
export const removeAllLastNewlines = (textBlock: string): string => {
const stringAllLastNewLines = /\n+$/;

return stringAllLastNewLines.test(textBlock) ? textBlock.replace(stringAllLastNewLines, "") : textBlock;
}
36 changes: 36 additions & 0 deletions test/removeAllLastNewlines.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit c1ab57b

Please sign in to comment.