Created
May 13, 2023 15:37
-
-
Save kaosf/5192f89d67af1abb30d7ffc34b638776 to your computer and use it in GitHub Desktop.
React component for multiple lines string, with converting URL to "a" tag.
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 * as React from "react"; | |
const Lines: React.FC<{ body: string }> = (props) => ( | |
<> | |
{props.body.split("\n").map((line) => { | |
const result = line.match(/(.*)(https?:\/\/[^\s]*)(.*)/); | |
if (result === null) { | |
return ( | |
<> | |
{line} | |
<br /> | |
</> | |
); | |
} else { | |
const [_, part1, part2, part3] = result; | |
return ( | |
<> | |
{part1} | |
<a href={part2}>{part2}</a> | |
{part3} | |
<br /> | |
</> | |
); | |
} | |
})} | |
</> | |
); | |
export default Lines; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
trackback https://twitter.com/ka_/status/1657411476406300673