Created
August 20, 2017 10:17
-
-
Save erika-dike/a538e7ec01faa39a099504e3fedf26a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React from 'react'; | |
import getUrls from 'get-urls'; | |
import linkifyHtml from 'linkifyjs/html'; | |
import PropTypes from 'prop-types'; | |
import { | |
PostMediaContainer, | |
PostTextContainer, | |
} from './components'; | |
const PostContent = ({ content }) => { | |
const urls = Array.from(getUrls(content)); // returns a set | |
let contentToBeRendered = content; | |
let mappedUrls; | |
if (urls.length) { | |
contentToBeRendered = linkifyHtml(content); | |
mappedUrls = urls.map((url, index) => | |
<PostMediaContainer key={index} url={url} />, | |
); | |
} | |
return ( | |
<div> | |
<PostTextContainer text={contentToBeRendered} /> | |
{mappedUrls} | |
</div> | |
); | |
}; | |
PostContent.propTypes = { | |
content: PropTypes.string.isRequired, | |
}; | |
export default PostContent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment