Skip to content

Commit da1d53b

Browse files
committed
add styling for markdown link
1 parent 4611c05 commit da1d53b

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/components/Message/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type {
2828
RetrySendMessage,
2929
StreamMessage,
3030
} from '../../context/ChannelContext';
31-
import type { MentionProps, RenderTextOptions } from '../../utils';
31+
import type { RenderTextOptions } from '../../utils';
3232

3333
import type {
3434
CustomTrigger,
@@ -282,7 +282,6 @@ export type MessageUIComponentProps<
282282
renderText?: (
283283
text?: string,
284284
mentioned_users?: UserResponse<Us>[],
285-
MentionComponent?: React.ComponentType<MentionProps<Us>>,
286285
options?: RenderTextOptions,
287286
) => JSX.Element | null;
288287
/** Whether or not the current Message is in a Thread */

src/styles/Message.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@
447447
font-weight: $heavy-font-weight;
448448
}
449449

450+
&-url-link {
451+
display: inline-block;
452+
max-width: 150px;
453+
text-overflow: ellipsis;
454+
overflow: hidden;
455+
white-space: nowrap;
456+
}
457+
450458
&--inner {
451459
display: flex;
452460
flex-direction: column;

src/utils.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,26 @@ type MarkDownRenderers = {
7474
const markDownRenderers: { [nodeType: string]: React.ElementType } = {
7575
// eslint-disable-next-line react/display-name
7676
link: (props: MarkDownRenderers) => {
77-
if (
78-
!props.href ||
79-
(!props.href.startsWith('http') && !props.href.startsWith('mailto:'))
80-
) {
81-
return props.children;
77+
const { children, href } = props;
78+
79+
const isEmail = href?.startsWith('mailto:');
80+
const isUrl = href?.startsWith('http');
81+
82+
if (!href || (!isEmail && !isUrl)) {
83+
return children;
8284
}
8385

8486
return (
85-
<a href={props.href} rel='nofollow noreferrer noopener' target='_blank'>
86-
{props.children}
87-
</a>
87+
<span>
88+
<a
89+
className={`${isUrl ? 'str-chat__message-url-link' : ''}`}
90+
href={href}
91+
rel='nofollow noreferrer noopener'
92+
target='_blank'
93+
>
94+
{children}
95+
</a>
96+
</span>
8897
);
8998
},
9099
span: 'span',
@@ -156,13 +165,11 @@ export type RenderTextOptions = {
156165
customMarkDownRenderers?: {
157166
[nodeType: string]: React.ElementType;
158167
};
159-
truncationLimit?: number;
160168
};
161169

162170
export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
163171
text?: string,
164172
mentioned_users?: UserResponse<Us>[],
165-
MentionComponent: React.ComponentType<MentionProps<Us>> = Mention,
166173
options: RenderTextOptions = {},
167174
) => {
168175
// take the @ mentions and turn them into markdown?
@@ -196,12 +203,8 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
196203
if (noParsingNeeded.length > 0 || linkIsInBlock) return;
197204

198205
const displayLink =
199-
type === 'email'
200-
? value
201-
: truncate(
202-
value.replace(detectHttp, ''),
203-
options.truncationLimit || 20,
204-
);
206+
type === 'email' ? value : value.replace(detectHttp, '');
207+
205208
newText = newText.replace(value, `[${displayLink}](${encodeURI(href)})`);
206209
});
207210

@@ -212,9 +215,9 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
212215
}
213216

214217
const renderers = {
218+
mention: Mention,
215219
...markDownRenderers,
216220
...options.customMarkDownRenderers,
217-
mention: MentionComponent,
218221
};
219222

220223
return (

0 commit comments

Comments
 (0)