generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Remove new lines from the end of contextual text blocks (#28) (
#30)
- Loading branch information
1 parent
c9d0009
commit dcdce55
Showing
12 changed files
with
266 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './preserveNewlineIndentation' | ||
export * from './removeTrailingSpaces' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}] | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters