This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/components/Highlight.js
Tom Moor 032d843f5b Fix isInternalUrl for subdomain support
Refactor / reduce plumbing
2018-11-18 18:43:11 -08:00

45 lines
909 B
JavaScript

// @flow
import * as React from 'react';
import replace from 'string-replace-to-array';
import styled from 'styled-components';
type Props = {
highlight: ?string | RegExp,
processResult?: (tag: string) => string,
text: string,
caseSensitive?: boolean,
};
function Highlight({
highlight,
processResult,
caseSensitive,
text,
...rest
}: Props) {
let regex;
if (highlight instanceof RegExp) {
regex = highlight;
} else {
regex = new RegExp(
(highlight || '').replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'),
caseSensitive ? 'g' : 'gi'
);
}
return (
<span {...rest}>
{highlight
? replace(text, regex, (tag, index) => (
<Mark key={index}>{processResult ? processResult(tag) : tag}</Mark>
))
: text}
</span>
);
}
const Mark = styled.mark`
background: ${props => props.theme.yellow};
`;
export default Highlight;