Skip to content

Commit 998d3d7

Browse files
committed
add renderText options
1 parent b42d62a commit 998d3d7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
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/utils.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,18 @@ const Mention = <Us extends DefaultUserType<Us> = DefaultUserType>(
152152
props: PropsWithChildren<Us>,
153153
) => <span className='str-chat__message-mention'>{props.children}</span>;
154154

155+
type RenderTextOptions = {
156+
customMarkDownRenderers?: {
157+
[nodeType: string]: React.ElementType;
158+
};
159+
truncationLimit?: number;
160+
};
161+
155162
export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
156163
text?: string,
157164
mentioned_users?: UserResponse<Us>[],
158165
MentionComponent: React.ComponentType<MentionProps<Us>> = Mention,
166+
options: RenderTextOptions = {},
159167
) => {
160168
// take the @ mentions and turn them into markdown?
161169
// translate links
@@ -188,7 +196,12 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
188196
if (noParsingNeeded.length > 0 || linkIsInBlock) return;
189197

190198
const displayLink =
191-
type === 'email' ? value : truncate(value.replace(detectHttp, ''), 20);
199+
type === 'email'
200+
? value
201+
: truncate(
202+
value.replace(detectHttp, ''),
203+
options.truncationLimit || 20,
204+
);
192205
newText = newText.replace(value, `[${displayLink}](${encodeURI(href)})`);
193206
});
194207

@@ -200,6 +213,7 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
200213

201214
const renderers = {
202215
...markDownRenderers,
216+
...options.customMarkDownRenderers,
203217
mention: MentionComponent,
204218
};
205219

0 commit comments

Comments
 (0)