Skip to content

Commit dd6f6a1

Browse files
authored
docs: describe customization for allowed tag names with renderText (#2143)
1 parent 4a25912 commit dd6f6a1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docusaurus/docs/React/components/core-components/message-list.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,34 @@ const CustomMessageList = () => (
190190
);
191191
```
192192

193+
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:
194+
195+
```tsx
196+
import { findAndReplace } from 'hast-util-find-and-replace';
197+
import { u } from 'unist-builder';
198+
import { defaultAllowedTagNames, renderText, RenderTextPluginConfigurator } from 'stream-chat-react';
199+
200+
// wraps every letter b in <xxx></xxx> tags
201+
const customTagName = 'xxx';
202+
const replace = (match) => u('element', { tagName: customTagName }, [u('text', match)]);
203+
const customRehypePlugin = () => (tree) => findAndReplace(tree, /b/, replace);
204+
205+
const getRehypePlugins: RenderTextPluginConfigurator = (plugins) => {
206+
return [customRehypePlugin, ...plugins];
207+
}
208+
209+
210+
const customRenderText = (text, mentionedUsers) =>
211+
renderText(text, mentionedUsers, {
212+
allowedTagNames: [...defaultAllowedTagNames, customTagName],
213+
getRehypePlugins,
214+
});
215+
216+
const CustomMessageList = () => (
217+
<MessageList renderText={customRenderText}/>
218+
);
219+
```
220+
193221
## Props
194222

195223
### additionalMessageInputProps

0 commit comments

Comments
 (0)