Skip to content

Commit 92e7003

Browse files
authored
Merge pull request #807 from GetStream/renderText-options
Add renderText options
2 parents b42d62a + ccb87b1 commit 92e7003

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

examples/typescript-app/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ if (process.env.REACT_APP_CHAT_SERVER_ENDPOINT) {
3838
chatClient.connectUser({ id: userId }, userToken);
3939

4040
const App = () => (
41+
// @ts-expect-error TODO: find out why this occurs linked, but not with an npm version
4142
<Chat client={chatClient} theme={`messaging ${theme}`}>
4243
<ChannelList
4344
List={ChannelListMessenger}

src/components/Message/types.ts

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

3233
import type {
3334
CustomTrigger,
@@ -281,6 +282,7 @@ export type MessageUIComponentProps<
281282
renderText?: (
282283
text?: string,
283284
mentioned_users?: UserResponse<Us>[],
285+
options?: RenderTextOptions,
284286
) => JSX.Element | null;
285287
/** Whether or not the current Message is in a Thread */
286288
threadList?: boolean;

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: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,23 @@ 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
88+
className={`${isUrl ? 'str-chat__message-url-link' : ''}`}
89+
href={href}
90+
rel='nofollow noreferrer noopener'
91+
target='_blank'
92+
>
93+
{children}
8794
</a>
8895
);
8996
},
@@ -144,18 +151,24 @@ const mentionsMarkdownPlugin = <
144151
return transform;
145152
};
146153

147-
type MentionProps<Us extends DefaultUserType<Us> = DefaultUserType> = {
154+
export type MentionProps<Us extends DefaultUserType<Us> = DefaultUserType> = {
148155
mentioned_user: UserResponse<Us>;
149156
};
150157

151158
const Mention = <Us extends DefaultUserType<Us> = DefaultUserType>(
152159
props: PropsWithChildren<Us>,
153160
) => <span className='str-chat__message-mention'>{props.children}</span>;
154161

162+
export type RenderTextOptions = {
163+
customMarkDownRenderers?: {
164+
[nodeType: string]: React.ElementType;
165+
};
166+
};
167+
155168
export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
156169
text?: string,
157170
mentioned_users?: UserResponse<Us>[],
158-
MentionComponent: React.ComponentType<MentionProps<Us>> = Mention,
171+
options: RenderTextOptions = {},
159172
) => {
160173
// take the @ mentions and turn them into markdown?
161174
// translate links
@@ -188,7 +201,8 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
188201
if (noParsingNeeded.length > 0 || linkIsInBlock) return;
189202

190203
const displayLink =
191-
type === 'email' ? value : truncate(value.replace(detectHttp, ''), 20);
204+
type === 'email' ? value : value.replace(detectHttp, '');
205+
192206
newText = newText.replace(value, `[${displayLink}](${encodeURI(href)})`);
193207
});
194208

@@ -199,8 +213,9 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
199213
}
200214

201215
const renderers = {
216+
mention: Mention,
202217
...markDownRenderers,
203-
mention: MentionComponent,
218+
...options.customMarkDownRenderers,
204219
};
205220

206221
return (

0 commit comments

Comments
 (0)