Skip to content

Instantly share code, notes, and snippets.

@kaosf
Created May 13, 2023 15:37
Show Gist options
  • Save kaosf/5192f89d67af1abb30d7ffc34b638776 to your computer and use it in GitHub Desktop.
Save kaosf/5192f89d67af1abb30d7ffc34b638776 to your computer and use it in GitHub Desktop.
React component for multiple lines string, with converting URL to "a" tag.
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;
@kaosf
Copy link
Author

kaosf commented May 13, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment