Skip to content

Commit

Permalink
Feature: Remove new lines from the end of contextual text blocks (#28) (
Browse files Browse the repository at this point in the history
  • Loading branch information
absorpheus committed Jul 17, 2024
1 parent c9d0009 commit dcdce55
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 13 deletions.
10 changes: 2 additions & 8 deletions 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 { preserveNewlineIndentation, removeTrailingSpaces } from 'src/utils'

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 ? removeTrailingSpaces(preserveNewlineIndentation(textForContext)) : textForContext,
highlight: preserveNewlineIndentation(annotation.ZANNOTATIONSELECTEDTEXT),
note: userNote ? preserveNewlineIndentation(userNote) : userNote,
highlightLocation: annotation.ZANNOTATIONLOCATION,
Expand All @@ -47,10 +48,3 @@ export const aggregateBookAndHighlightDetails = async (): Promise<ICombinedBooks

return resultingHighlights;
};

// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks
const preserveNewlineIndentation = (textBlock: string): string => {
const stringWithNewLines = /\n+\s*/g;

return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock;
}
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export interface IHighlight {
highlight: string;
highlightLocation: string;
note: string;
highlightStyle: IBookAnnotation['ZANNOTATIONSTYLE'],
highlightCreationDate: number;
highlightModificationDate: number;
}
export interface ICombinedBooksAndHighlights {
bookTitle: string;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './preserveNewlineIndentation'
export * from './removeTrailingSpaces'
6 changes: 6 additions & 0 deletions src/utils/preserveNewlineIndentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks
export const preserveNewlineIndentation = (textBlock: string): string => {
const stringWithNewLines = /\n+\s*/g;

return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock;
}
6 changes: 6 additions & 0 deletions src/utils/removeTrailingSpaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Handler of all space, tab or newline characters at the end of text blocks to prevent new lines appearing
export const removeTrailingSpaces = (textBlock: string): string => {
const endLineSpaces = /\s+$/;

return endLineSpaces.test(textBlock) ? textBlock.replace(endLineSpaces, '') : textBlock;
}
18 changes: 18 additions & 0 deletions test/mocks/detailsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const highlights = [{
"bookTitle": "Designing Data-Intensive Applications",
"bookId": "28AEDF62F12B289C88BD6659BD6E50CC",
"bookAuthor": "Kleppmann, Martin",
"bookGenre": "Technology",
"bookLanguage": "EN",
"bookLastOpenedDate": 731876693.002279,
"bookCoverUrl": '',
"annotations": [{
"chapter": '',
"contextualText": `Chapter 1 introduces the terminology and approach\nthat we're going to use throughout this book. It examines what we actually mean by\nwords like reliability, scalability, and maintainability, and how\nwe can try to achieve these goals.`,
"highlight": `Chapter 1 introduces the terminology and approach\nthat we're going to use throughout this book. It examines what we actually mean by\nwords like reliability, scalability, and maintainability, and how\nwe can try to achieve these goals.`,
"note": `Test note for the hightlight from Designing Data-Intensive Applications`,
"highlightStyle": 3,
"highlightCreationDate": 731876693.002279,
"highlightModificationDate": 731876693.002279
}]
}]
17 changes: 17 additions & 0 deletions test/mocks/rawTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ Number of annotations:: {{annotations.length}}
{{/each}}
`;

export const rawCustomTemplateWrappedTextBlockMock = `Title:: 📕 {{{bookTitle}}}
Author:: {{{bookAuthor}}}
Link:: [Apple Books Link](ibooks://assetid/{{bookId}})
## Annotations
Number of annotations:: {{annotations.length}}
{{#each annotations}}
----
> [!QUOTE]
> {{{highlight}}}
{{/each}}
`;
18 changes: 18 additions & 0 deletions test/mocks/renderedTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,21 @@ along with a new line to test the preservation of indentation
- <small>📅 Highlight modified on:: 2024-03-11 03:04:53 PM -04:00</small>
`;

export const renderedCustomTemplateWrappedTextBlockMock = `Title:: 📕 Designing Data-Intensive Applications
Author:: Kleppmann, Martin
Link:: [Apple Books Link](ibooks://assetid/28AEDF62F12B289C88BD6659BD6E50CC)
## Annotations
Number of annotations:: 1
----
> [!QUOTE]
> Chapter 1 introduces the terminology and approach
that we're going to use throughout this book. It examines what we actually mean by
words like reliability, scalability, and maintainability, and how
we can try to achieve these goals.
`;
31 changes: 31 additions & 0 deletions test/preserveNewlineIndentation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, test } from 'vitest'
import { preserveNewlineIndentation } from 'src/utils'
import { renderHighlightsTemplate } from 'src/methods/renderHighlightsTemplate'
import { rawCustomTemplateWrappedTextBlockMock } from './mocks/rawTemplates'
import { renderedCustomTemplateWrappedTextBlockMock } from './mocks/renderedTemplate'
import { highlights } from './mocks/detailsData'
import { ICombinedBooksAndHighlights } from 'src/types'

describe('preserveNewlineIndentation', () => {
test('Should handle double new line characters to preserve proper indentation in text', () => {
const text = `This is an example text to test the handling of double newline\n\ncharacters in text.`
const actual = preserveNewlineIndentation(text)
const expected = `This is an example text to test the handling of double newline\ncharacters in text.`

expect(actual).toEqual(expected)
})

test('Should handle multiple double new line characters to preserve proper indentation in text', () => {
const text = `This is an example\n\ntext to test\n\nthe handling of multiple double newline\n\ncharacters in text.`
const actual = preserveNewlineIndentation(text)
const expected = `This is an example\ntext to test\nthe handling of multiple double newline\ncharacters in text.`

expect(actual).toEqual(expected)
})

test('Should render a custom template and preserve the proper indentation when a text block is wrapped', async () => {
const renderedTemplate = await renderHighlightsTemplate(highlights[0] as ICombinedBooksAndHighlights, rawCustomTemplateWrappedTextBlockMock);

expect(renderedTemplate).toEqual(renderedCustomTemplateWrappedTextBlockMock);
});
})
156 changes: 156 additions & 0 deletions test/removeTrailingSpaces.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { describe, expect, test } from 'vitest'
import { removeTrailingSpaces } from 'src/utils'

describe('removeTrailingSpaces', () => {
test('Should remove a newline character at the end of text', () => {
const text = `This is an example text to test the removal of a newline character at the end of the text.\n`
const actual = removeTrailingSpaces(text)
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 double newline characters at the end of text', () => {
const text = `This is an example text to test the removal of double newline characters at the end of the text.\n\n`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of double newline characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove triple newline characters at the end of text', () => {
const text = `This is an example text to test the removal of triple newline characters at the end of the text.\n\n\n`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of triple newline characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove many newline characters at the end of text', () => {
const text = `This is an example text to test the removal of many newline characters at the end of the text.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many newline characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove many newline characters at the end of text while preserving space, tab and newline characters within the text', () => {
const text = `This is an example text to test the removal of many newline characters at the end of the text while preserving space , tab\t and newline\n characters within the text.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many newline characters at the end of the text while preserving space , tab\t and newline\n characters within the text.`

expect(actual).toEqual(expected)
})

test('Should remove a tab character at the end of text', () => {
const text = `This is an example text to test the removal of a tab character at the end of the text.\t`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of a tab character at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove double tab characters at the end of text', () => {
const text = `This is an example text to test the removal of double tab characters at the end of the text.\t\t`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of double tab characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove triple tab characters at the end of text', () => {
const text = `This is an example text to test the removal of triple tab characters at the end of the text.\t\t\t`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of triple tab characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove many tab characters at the end of text', () => {
const text = `This is an example text to test the removal of many tab characters at the end of the text.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many tab characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove many tab characters at the end of text while preserving space, tab and newline characters within the text', () => {
const text = `This is an example text to test the removal of many tab characters at the end of the text while preserving space , tab\t and newline\n characters within the text.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many tab characters at the end of the text while preserving space , tab\t and newline\n characters within the text.`

expect(actual).toEqual(expected)
})

test('Should remove a space character at the end of text', () => {
const text = `This is an example text to test the removal of a space character at the end of the text. `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of a space character at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove double space characters at the end of text', () => {
const text = `This is an example text to test the removal of double space characters at the end of the text. `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of double space characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove triple space characters at the end of text', () => {
const text = `This is an example text to test the removal of triple space characters at the end of the text. `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of triple space characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove many space characters at the end of text', () => {
const text = `This is an example text to test the removal of many space characters at the end of the text. `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many space characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove many space characters at the end of text while preserving space, tab and newline characters at the end of the text.', () => {
const text = `This is an example text to test the removal of many space characters at the end of the text preserving space , tab\b and newline\n characters at the end of the text. `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many space characters at the end of the text preserving space , tab\b and newline\n characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove multiple space, tab and newline characters at the end of text', () => {
const text = `This is an example text to test the removal of many space, tab and newline characters at the end of the text. \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many space, tab and newline characters at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should remove multiple space, tab and newline characters at the end of text while preserving space, tab and newline characters within text', () => {
const text = `This is an example text to test the removal of many space , tab\t and newline\n characters at the end of the text while preserving space, tab and newline characters within text. \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t `
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test the removal of many space , tab\t and newline\n characters at the end of the text while preserving space, tab and newline characters within text.`

expect(actual).toEqual(expected)
})

test('Should return the text when no space, tab or newline characters exist at the end of the text', () => {
const text = `This is an example text to test that the text is returned when no space, tab or newline characters exist at the end of the text.`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test that the text is returned when no space, tab or newline characters exist at the end of the text.`

expect(actual).toEqual(expected)
})

test('Should return the text when no space, tab or newline characters exist at the end of the text while preserving space, tab and newline characters within text', () => {
const text = `This is an example text to test that the text is returned when no space , tab\t or newline\n characters exist at the end of the text while preserving space, tab and newline characters within text.`
const actual = removeTrailingSpaces(text)
const expected = `This is an example text to test that the text is returned when no space , tab\t or newline\n characters exist at the end of the text while preserving space, tab and newline characters within text.`

expect(actual).toEqual(expected)
})
})
5 changes: 3 additions & 2 deletions test/renderHighlightsTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { aggregatedHighlights } from './mocks/aggregatedDetailsData';
import { rawCustomTemplateMock } from './mocks/rawTemplates';
import { defaultTemplateMock, renderedCustomTemplateMock } from './mocks/renderedTemplate';
import defaultTemplate from '../src/template';
import { ICombinedBooksAndHighlights } from 'src/types';

describe('renderHighlightsTemplate', () => {
const helpers = Handlebars.helpers;
Expand All @@ -18,15 +19,15 @@ describe('renderHighlightsTemplate', () => {

describe('Template rendering', () => {
test('Should render a default template with the provided data', async () => {
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0], defaultTemplate);
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0] as ICombinedBooksAndHighlights, defaultTemplate);

expect(renderedTemplate).toEqual(defaultTemplateMock);
});

test('Should render a custom template with the provided data', async () => {
tzSpy.mockImplementation(() => 'America/New_York');

const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0], rawCustomTemplateMock);
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0] as ICombinedBooksAndHighlights, rawCustomTemplateMock);

expect(renderedTemplate).toEqual(renderedCustomTemplateMock);
});
Expand Down
7 changes: 4 additions & 3 deletions test/saveHighlightsToVault.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SaveHighlights from '../src/methods/saveHighlightsToVault';
import { AppleBooksHighlightsImportPluginSettings } from '../src/settings';
import { aggregatedHighlights } from './mocks/aggregatedDetailsData';
import { defaultTemplateMock } from './mocks/renderedTemplate';
import { ICombinedBooksAndHighlights } from '../src/types'

const mockVault = {
getAbstractFileByPath: vi.fn(),
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('Save highlights to vault', () => {
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, settings);
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('ibooks-highlights');

await saveHighlights.saveHighlightsToVault(aggregatedHighlights);
await saveHighlights.saveHighlightsToVault(aggregatedHighlights as ICombinedBooksAndHighlights[]);

expect(spyGetAbstractFileByPath).toHaveBeenCalledTimes(1);
expect(spyGetAbstractFileByPath).toHaveBeenCalledWith('ibooks-highlights');
Expand All @@ -66,7 +67,7 @@ describe('Save highlights to vault', () => {
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, { ...settings, highlightsFolder: '' });
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('');

await saveHighlights.saveHighlightsToVault(aggregatedHighlights);
await saveHighlights.saveHighlightsToVault(aggregatedHighlights as ICombinedBooksAndHighlights[]);

expect(spyGetAbstractFileByPath).toHaveBeenCalledTimes(1);
expect(spyGetAbstractFileByPath).toHaveBeenCalledWith('');
Expand All @@ -92,7 +93,7 @@ describe('Save highlights to vault', () => {
};
});

await saveHighlights.saveHighlightsToVault(aggregatedHighlights);
await saveHighlights.saveHighlightsToVault(aggregatedHighlights as ICombinedBooksAndHighlights[]);

expect(spyList).toHaveBeenCalledTimes(1);
expect(spyList).toReturnWith({
Expand Down

0 comments on commit dcdce55

Please sign in to comment.