-
Notifications
You must be signed in to change notification settings - Fork 20
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
1 parent
e14c940
commit 1ea41a6
Showing
4 changed files
with
64 additions
and
63 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 was deleted.
Oops, something went wrong.
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,58 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import styled from 'styled-components'; | ||
import Link from '@splunk/react-ui/Link'; | ||
|
||
|
||
const MarkdownWrapper = styled.span` | ||
color: ${props => props.color}; | ||
`; | ||
|
||
function MarkdownMessage(props) { | ||
// flatMap adds the link to the part of text that has been split from text | ||
// and returns a new array formed by applying a given function to each element of the array, | ||
// and then flattening the result by one level | ||
function flatMap(array, fn) { | ||
var markdownText = []; | ||
for (var i = 0; i < array.length; i++) { | ||
var mapping = fn(array[i]); | ||
markdownText = markdownText.concat(mapping); | ||
} | ||
return markdownText; | ||
} | ||
|
||
function getMarkdownText(){ | ||
var markdownText = props.text; | ||
if (props.markdownType === 'link') { | ||
markdownText = <Link to={props.link}>{props.text}</Link> | ||
} else if (props.markdownType === 'hybrid') { | ||
// markdownType hybrid is for support of both text and link | ||
markdownText = flatMap(markdownText.split(props.token), function (part) { | ||
return [part, <Link key= {part} to={props.link}>{props.linkText}</Link>]; | ||
}); | ||
markdownText.pop(); | ||
} else if (props.markdownType === 'text'){ | ||
markdownText = <MarkdownWrapper color={props.color}>{props.text}</MarkdownWrapper> | ||
} | ||
|
||
return markdownText; | ||
} | ||
|
||
return ( | ||
<div data-test="msg-markdown"> | ||
{getMarkdownText()} | ||
</div> | ||
); | ||
} | ||
|
||
|
||
MarkdownMessage.propTypes = { | ||
text: PropTypes.string, | ||
link: PropTypes.string, | ||
color: PropTypes.string, | ||
markdownType: PropTypes.string, | ||
token: PropTypes.string, | ||
linkText: PropTypes.string | ||
}; | ||
|
||
export default React.memo(MarkdownMessage); |