Fork of atp.tools as a universal profile for people on the ATmosphere
1import React from "preact/compat";
2
3export function preprocessText(text: string): React.ReactNode[] {
4 // URL regex pattern
5 const urlPattern = /(https?:\/\/[^\s]+)/g;
6
7 // Split the text by URLs
8 const parts = text.split(urlPattern);
9
10 // Process each part and create React elements
11 return parts.map((part, index) => {
12 // Check if this part is a URL
13 if (urlPattern.test(part)) {
14 return (
15 <a
16 className="text-blue-700 dark:text-blue-400"
17 key={index}
18 href={part}
19 target="_blank"
20 rel="noopener noreferrer"
21 >
22 {part}
23 </a>
24 );
25 }
26
27 // Handle newlines in text parts
28 if (part) {
29 return part.split("\n").map((line, lineIndex, array) => (
30 <React.Fragment key={`${index}-${lineIndex}`}>
31 {line}
32 {lineIndex < array.length - 1 && <br />}
33 </React.Fragment>
34 ));
35 }
36
37 return null;
38 });
39}