Skip to content

Commit b4d5056

Browse files
authored
Fix:Url should be identified even when merged with words(#222)
1 parent f3e480f commit b4d5056

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/ui/OGMessageItemBody/index.tsx

+47-15
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,53 @@ export default function OGMessageItemBody({
4747
>
4848
<p className="sendbird-og-message-item-body__text-bubble">
4949
{
50-
message?.message.split(' ').map((word: string) => (
51-
isUrl(word)
52-
? (
53-
<LinkLabel
54-
className="sendbird-og-message-item-body__text-bubble__message"
55-
key={uuidv4()}
56-
src={word}
57-
type={LabelTypography.BODY_1}
58-
color={isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1}
59-
>
60-
{word}
61-
</LinkLabel>
62-
)
63-
: (`${word} `)
64-
))
50+
message?.message.split(' ').map((word: string) => {
51+
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]+)?(/.*)?");
52+
const targetUrl = urlRegex.exec(word)?.[0];
53+
const stringUrl = { front: '', url: '', back: '' };
54+
if (targetUrl) {
55+
const targetUrlIndex = word.indexOf(targetUrl);
56+
if (targetUrlIndex > 0) {
57+
stringUrl.front = word.slice(0, targetUrlIndex);
58+
}
59+
stringUrl.url = word.slice(targetUrlIndex, targetUrlIndex + targetUrl.length);
60+
if (targetUrlIndex + targetUrl.length < word.length) {
61+
stringUrl.back = word.slice(targetUrlIndex + targetUrl.length);
62+
}
63+
}
64+
if (targetUrl) {
65+
return [
66+
stringUrl.front ? stringUrl.front : '',
67+
stringUrl.url ? (
68+
<LinkLabel
69+
className="sendbird-word__url"
70+
key={uuidv4()}
71+
src={stringUrl.url}
72+
type={LabelTypography.BODY_1}
73+
color={isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1}
74+
>
75+
{stringUrl.url}
76+
</LinkLabel>
77+
) : null,
78+
stringUrl.back ? stringUrl.back : '',
79+
];
80+
}
81+
return (
82+
isUrl(word)
83+
? (
84+
<LinkLabel
85+
className="sendbird-og-message-item-body__text-bubble__message"
86+
key={uuidv4()}
87+
src={word}
88+
type={LabelTypography.BODY_1}
89+
color={isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1}
90+
>
91+
{word}
92+
</LinkLabel>
93+
)
94+
: (`${word} `)
95+
);
96+
})
6597
}
6698
{
6799
isEditedMessage(message) && (

0 commit comments

Comments
 (0)