Skip to content

Commit 8e62739

Browse files
Adjust renderText documentation
1 parent c737834 commit 8e62739

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

docusaurus/docs/React/components/message-components/render-text.mdx

+29-45
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: render-text
33
title: renderText function
44
---
55

6-
The `renderText` function is a core piece of functionality, which handles how the text of our message is going to be formatted/look like. The default [`renderText`](#render-text) function parses a markdown string and outputs a `ReactElement`. Under the hood, the output is generated by the `ReactMarkdown` component from [react-markdown library](https://github.com/remarkjs/react-markdown). The component transforms the markdown to `ReactElement` by using [`remark` parser](https://github.com/remarkjs/remark/tree/main) and [`remark`](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) and [`rehype`](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) plugins.
6+
The `renderText` function is a core piece of functionality, which handles how the text of our message is going to be formatted/look like. The default `renderText` function parses a markdown string and outputs a `ReactElement`. Under the hood, the output is generated by the `ReactMarkdown` component from [react-markdown library](https://github.com/remarkjs/react-markdown). The component transforms the markdown to `ReactElement` by using [`remark` parser](https://github.com/remarkjs/remark/tree/main) and [`remark`](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) and [`rehype`](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) plugins.
77

88
The default `remark` plugins used by SDK are:
99

@@ -14,53 +14,41 @@ The default `rehype` plugins (both specific to this SDK) are:
1414
1. plugin to render user mentions
1515
2. plugin to render emojis
1616

17-
## Overriding defaults
17+
## Overriding Defaults
1818

19-
### Custom `renderText` function
19+
### Custom `renderText` Function
2020

21-
If you don't want your chat implementation to support markdown syntax by default you can override the default behaviour by creating a custom `renderText` function which returns a React node and passing it down to the `MessageList` or `MessageSimple` component via `renderText` property.
21+
If you don't want your chat implementation to support markdown syntax by default you can override the default behaviour by creating a custom `renderText` function which returns a React node and passing it down to the [`MessageList`](../core-components/message-list.mdx) or [`MessageSimple`](../message-components/message-ui.mdx) component via `renderText` property.
2222

2323
For this particular example we'll create a very primitive one which takes the message text passed down to it as a first argument and returns it wrapped in `span` element:
2424

2525
```tsx
26+
import { MessageList } from 'stream-chat-react';
27+
2628
const customRenderText = (text) => {
2729
return <span>{text}</span>;
2830
};
2931

30-
const App = () => (
31-
<Chat client={client}>
32-
<Channel>
33-
<Window>
34-
<MessageList renderText={customRenderText} />
35-
</Window>
36-
</Channel>
37-
</Chat>
38-
);
32+
export const WrappedMessageList = () => <MessageList renderText={customRenderText} />;
3933
```
4034

41-
Here's also an example with `VirtualizedMessageList` which currently does not accept `renderText` directly:
35+
Here's also an example with [`VirtualizedMessageList`](../core-components/virtualized-list.mdx) which currently does not accept `renderText` directly:
4236

4337
```tsx
44-
import { MessageSimple } from 'stream-chat-react';
38+
import { VirtualizedMessageList, MessageSimple } from 'stream-chat-react';
4539

4640
const customRenderText = (text) => {
4741
return <span>{text}</span>;
4842
};
4943

5044
const CustomMessage = (props) => <MessageSimple {...props} renderText={customRenderText} />;
5145

52-
const App = () => (
53-
<Chat client={client}>
54-
<Channel>
55-
<Window>
56-
<VirtualizedMessageList Message={CustomMessage} />
57-
</Window>
58-
</Channel>
59-
</Chat>
46+
export const WrappedVirtualizedMessageList = () => (
47+
<VirtualizedMessageList Message={CustomMessage} />
6048
);
6149
```
6250

63-
### Custom element rendering
51+
### Custom Element Rendering
6452

6553
If you feel like the default output is sufficient, but you'd like to adjust how certain [ReactMarkdown components](https://github.com/remarkjs/react-markdown#appendix-b-components) look like (like `strong` element generated by typing \*\*strong\*\*) you can do so by passing down options to a third argument of the default `renderText` function:
6654

@@ -71,37 +59,33 @@ Types `mention` and `emoji` are special case component types generated by our SD
7159
```tsx
7260
import { renderText } from 'stream-chat-react';
7361

74-
const StrongComponent = ({ children }) => <b className='custom-strong-class-name'>{children}</b>;
62+
const CustomStrongComponent = ({ children }) => (
63+
<b className='custom-strong-class-name'>{children}</b>
64+
);
7565

76-
const MentionComponent = ({ children, node: { mentionedUser } }) => (
66+
const CustomMentionComponent = ({ children, node: { mentionedUser } }) => (
7767
<a data-user-id={mentionedUser.id} href={`/user-profile/${mentionedUser.id}`}>
7868
{children}
7969
</a>
8070
);
8171

82-
const App = () => (
83-
<Chat client={client}>
84-
<Channel>
85-
<Window>
86-
<MessageList
87-
renderText={(text, mentionedUsers) =>
88-
renderText(text, mentionedUsers, {
89-
customMarkDownRenderers: { strong: StrongComponent, mention: MentionComponent },
90-
})
91-
}
92-
/>
93-
</Window>
94-
</Channel>
95-
</Chat>
72+
export const WrappedMessageList = () => (
73+
<MessageList
74+
renderText={(text, mentionedUsers) =>
75+
renderText(text, mentionedUsers, {
76+
customMarkDownRenderers: { strong: CustomStrongComponent, mention: CustomMentionComponent },
77+
})
78+
}
79+
/>
9680
);
9781
```
9882

99-
### Custom remark and rehype plugins
83+
### Custom Remark and Rehype Plugins
10084

101-
If you would like to extend the array of plugins used to parse the markdown, you can provide your own lists of remark resp. rehype plugins. The logic that determines what plugins are used and in which order can be specified in custom `getRehypePlugins` and `getRemarkPlugins` functions. These receive the default array of rehype and remark plugins for further customization. Both custom functions ought to be passed to the third `renderText()` parameter. An example follows:
85+
If you would like to extend the array of plugins used to parse the markdown, you can provide your own lists of remark resp. rehype plugins. The logic that determines what plugins are used and in which order can be specified in custom `getRehypePlugins` and `getRemarkPlugins` functions. These receive the default array of rehype and remark plugins for further customization. Both custom functions ought to be passed to the third `renderText` parameter. An example follows:
10286

10387
:::note
104-
It is important to understand what constitutes a rehype or remark plugin. A good start is to learn about the library called [`react-remark`](https://github.com/remarkjs/react-remark) which is used under the hood in our `renderText()` function.
88+
It is important to understand what constitutes a rehype or remark plugin. A good start is to learn about the library called [`react-remark`](https://github.com/remarkjs/react-remark) which is used under the hood in our `renderText` function.
10589
:::
10690

10791
```tsx
@@ -122,7 +106,7 @@ const customRenderText = (text, mentionedUsers) =>
122106
getRemarkPlugins,
123107
});
124108

125-
const CustomMessageList = () => <MessageList renderText={customRenderText} />;
109+
const WrappedMessageList = () => <MessageList renderText={customRenderText} />;
126110
```
127111

128112
It is also possible to define your custom set of allowed tag names for the elements rendered from the parsed markdown. To perform the tree transformations, you will need to use libraries like [`unist-builder`](https://github.com/syntax-tree/unist-builder) to build the trees and [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit-parents) or [`hast-util-find-and-replace`](https://github.com/syntax-tree/hast-util-find-and-replace) to traverse the tree:
@@ -151,5 +135,5 @@ const customRenderText = (text, mentionedUsers) =>
151135
getRehypePlugins,
152136
});
153137

154-
const CustomMessageList = () => <MessageList renderText={customRenderText} />;
138+
const WrappedMessageList = () => <MessageList renderText={customRenderText} />;
155139
```

0 commit comments

Comments
 (0)