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: Add text block handlers. Refactor tests. (#30)
* add createTextBlockHandler * add createTextBlockHandlerChain * add handleTextForContext * refactor test
- Loading branch information
1 parent
9386bae
commit fbdc9be
Showing
9 changed files
with
51 additions
and
20 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,5 @@ | ||
export const createTextBlockHandler = (regex: RegExp, replaceValue: string) => { | ||
return (textBlock: string) => { | ||
return regex.test(textBlock) ? textBlock.replace(regex, replaceValue) : 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,9 @@ | ||
import { TextBlockHandler } from "src/types" | ||
|
||
export const createTextBlockHandlerChain = (...textBlockHandlers: TextBlockHandler[]) => { | ||
return (textBlock: string) => { | ||
return textBlockHandlers.reduce((text, textBlockHandler) => { | ||
return textBlockHandler(text) | ||
}, 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,9 @@ | ||
import { preserveNewlineIndentation} from "./preserveNewlineIndentation"; | ||
import { removeAllLastNewlines } from "./removeAllLastNewLines"; | ||
import { createTextBlockHandlerChain } from "./createTextBlockHandlerChain" | ||
|
||
|
||
export const handleTextForContext = createTextBlockHandlerChain( | ||
preserveNewlineIndentation, | ||
removeAllLastNewlines | ||
) |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './preserveNewlineIndentation' | ||
export * from './removeAllLastNewLines' | ||
export * from './createTextBlockHandler' | ||
export * from './handleTextForContext' | ||
export * from './createTextBlockHandlerChain' |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// 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; | ||
import { createTextBlockHandler } from './createTextBlockHandler' | ||
|
||
return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; | ||
} | ||
// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks | ||
export const preserveNewlineIndentation = createTextBlockHandler( | ||
/\n+\s*/g, | ||
'\n' | ||
) |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// 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+$/; | ||
import { createTextBlockHandler } from "./createTextBlockHandler"; | ||
|
||
return stringAllLastNewLines.test(textBlock) ? textBlock.replace(stringAllLastNewLines, "") : textBlock; | ||
} | ||
// 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 = createTextBlockHandler( | ||
/\n+$/g, | ||
"" | ||
) |
16 changes: 8 additions & 8 deletions
16
test/representativeText.spec.ts → test/handleTextForContext.spec.ts
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