-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #173: Implement customizable highlighting of text output
- Loading branch information
1 parent
5bf69f7
commit bd0eb65
Showing
6 changed files
with
110 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { memo } from 'react'; | ||
import { Badge, Tooltip, useStyleConfig } from '@chakra-ui/react'; | ||
import isEqual from 'react-fast-compare'; | ||
import replace from 'react-string-replace'; | ||
|
||
import type { TooltipProps } from '@chakra-ui/react'; | ||
import type { Highlight as HighlightConfig } from '~/types'; | ||
|
||
interface HighlightedProps { | ||
patterns: HighlightConfig[]; | ||
children: string; | ||
} | ||
|
||
interface HighlightProps { | ||
label: string | null; | ||
colorScheme: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
const Highlight = (props: HighlightProps): JSX.Element => { | ||
const { colorScheme, label, children } = props; | ||
const { bg, color } = useStyleConfig('Button', { colorScheme }) as TooltipProps; | ||
return ( | ||
<Tooltip label={label} bg={bg} color={color} hasArrow> | ||
<Badge colorScheme={colorScheme}>{children}</Badge> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
const _Highlighted = (props: HighlightedProps): JSX.Element => { | ||
const { patterns, children } = props; | ||
let result: React.ReactNodeArray = []; | ||
let times: number = 0; | ||
|
||
for (const config of patterns) { | ||
let toReplace: string | React.ReactNodeArray = children; | ||
if (times !== 0) { | ||
toReplace = result; | ||
} | ||
result = replace(toReplace, new RegExp(`(${config.pattern})`, 'gm'), (m, i) => ( | ||
<Highlight key={`${m + i}`} label={config.label} colorScheme={config.color}> | ||
{m} | ||
</Highlight> | ||
)); | ||
times++; | ||
} | ||
|
||
return ( | ||
<> | ||
{result.map(r => ( | ||
<>{r}</> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export const Highlighted = memo(_Highlighted, isEqual); |
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
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