Skip to content

Commit 1d286a3

Browse files
authored
fix: Improve URL detection in OG messages(#221)
1 parent 1980656 commit 1d286a3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/ui/Word/index.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ export default function Word(props: WordProps): JSX.Element {
4646
/>
4747
);
4848
} else if (type === StringObjType.url) {
49+
const urlRegex = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
50+
const targetUrl = urlRegex.exec(value)?.[0];
51+
const stringUrl = { front: '', url: '', back: '' };
52+
if (targetUrl) {
53+
const targetUrlIndex = value.indexOf(targetUrl);
54+
if (targetUrlIndex > 0) {
55+
stringUrl.front = value.slice(0, targetUrlIndex);
56+
}
57+
stringUrl.url = value.slice(targetUrlIndex, targetUrlIndex + targetUrl.length);
58+
if (targetUrlIndex + targetUrl.length < value.length) {
59+
stringUrl.back = value.slice(targetUrlIndex + targetUrl.length);
60+
}
61+
}
62+
if (targetUrl) {
63+
return [
64+
stringUrl.front ? stringUrl.front : '',
65+
stringUrl.url ? (
66+
<LinkLabel
67+
className="sendbird-word__url"
68+
key={uuidv4()}
69+
src={stringUrl.url}
70+
type={LabelTypography.BODY_1}
71+
color={isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1}
72+
>
73+
{stringUrl.url}
74+
</LinkLabel>
75+
) : null,
76+
stringUrl.back ? stringUrl.back : '',
77+
];
78+
}
4979
return (
5080
<LinkLabel
5181
className="sendbird-word__url"

0 commit comments

Comments
 (0)