-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
|
||
export type FindAndReplacerProps = { part: string } | ||
|
||
type FindAndReplaceTuple = [string, React.FC<FindAndReplacerProps> | React.ReactNode] | ||
|
||
export type FindAndReplaceProps = { | ||
source: string | ||
findAndReplace: [FindAndReplaceTuple, ...FindAndReplaceTuple[]] | ||
} | ||
|
||
export function FindAndReplace(props: FindAndReplaceProps) { | ||
const { source, findAndReplace } = props | ||
|
||
// Create a regex pattern that matches any of the strings to be replaced | ||
const pattern = new RegExp( | ||
`(${findAndReplace.map(([find]) => find.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')})`, | ||
'g', | ||
) | ||
|
||
// Split the string and map parts to either original string or replaced content | ||
const parts = source.split(pattern) | ||
|
||
return parts.map((part, index) => { | ||
const key = `${part}-${index}` | ||
// Check if this part matches any of our replacement keys | ||
const Replacement = findAndReplace.find(([find]) => find === part)?.[1] | ||
|
||
if (!Replacement) return <React.Fragment key={key}>{part}</React.Fragment> | ||
if (typeof Replacement === 'function') { | ||
return <Replacement part={part} key={key} /> | ||
} | ||
return <React.Fragment key={key}>{Replacement}</React.Fragment> | ||
}) | ||
} |
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