generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove new lines from the end of contextual text blocks
- Loading branch information
1 parent
dc78460
commit c1ab57b
Showing
4 changed files
with
51 additions
and
7 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,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; | ||
} |
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,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) | ||
}) | ||
}) |