import { Fragment, type ReactNode } from "react";
const URL_REGEX = /(https?:\/\/[^\s<>"]+[^\s<>".,;:!?)\]}'])/gi;
export function autolink(text: string): ReactNode[] {
if (!text) return [];
const parts: ReactNode[] = [];
let lastIndex = 0;
let match: RegExpExecArray | null;
URL_REGEX.lastIndex = 0;
while ((match = URL_REGEX.exec(text)) !== null) {
if (match.index > lastIndex) {
parts.push(
{text.slice(lastIndex, match.index)}
,
);
}
const url = match[0];
parts.push(
{url}
,
);
lastIndex = match.index + url.length;
}
if (lastIndex < text.length) {
parts.push(
{text.slice(lastIndex)},
);
}
return parts;
}