27 lines
644 B
TypeScript
27 lines
644 B
TypeScript
import { Fragment, type ReactNode } from "react";
|
|
|
|
const URL_RE = /(https?:\/\/[^\s<>()]+[^\s<>().,;:!?])/g;
|
|
|
|
export function linkify(text: string): ReactNode[] {
|
|
if (!text) return [];
|
|
const parts = text.split(URL_RE);
|
|
return parts.map((part, i) => {
|
|
if (URL_RE.test(part)) {
|
|
URL_RE.lastIndex = 0;
|
|
return (
|
|
<a
|
|
key={i}
|
|
href={part}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="underline break-all"
|
|
style={{ color: "var(--foreground)" }}
|
|
>
|
|
{part}
|
|
</a>
|
|
);
|
|
}
|
|
return <Fragment key={i}>{part}</Fragment>;
|
|
});
|
|
}
|