Skip to content

Commit 965d44d

Browse files
authored
Merge pull request #557 from rebeccaalpert/rehype
chore(docs): Add info on how to use plugins
2 parents 9406cd5 + 6dc2a88 commit 965d44d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
# Sidenav top-level section
3+
# should be the same for all markdown files
4+
section: PatternFly-AI
5+
subsection: ChatBot
6+
# Sidenav secondary level section
7+
# should be the same for all markdown files
8+
id: Customizing messages
9+
source: Customizing messages
10+
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
11+
# If you use typescript, the name of the interface to display props for
12+
# These are found through the sourceProps function provided in patternfly-docs.source.js
13+
sortValue: 60
14+
---
15+
16+
## Modifying static content
17+
18+
The ChatBot extension `<Message>` component transforms Markdown to PatternFly React components via [react-markdown](https://github.com/remarkjs/react-markdown), which supports both [rehype](https://unifiedjs.com/explore/package/rehype/) and [remark](https://unifiedjs.com/explore/package/remark/) plugins for further output customization.
19+
20+
remark parses Markdown as input and serializes Markdown as output, while rehype does the same for HTML. This allows you to target and make transformations at specific checkpoints in the transformation process: either while the text tree is still in Markdown, or while the text tree is in HTML format. For more detailed information on the architecture of this library, see the [react-markdown documentation](https://github.com/remarkjs/react-markdown?tab=readme-ov-file#architecture).
21+
22+
You may need these plugins if, for example, you wanted to change the `href` on a link based on the link text. The `<Message>` prop `rehypePlugins` will accept any custom rehype plugins. We use already use these plugins in ChatBot to [render images a certain way](https://www.npmjs.com/package/rehype-unwrap-images) and [handle external links](https://www.npmjs.com/package/rehype-external-links).
23+
24+
There are many open source libraries available for other common customization needs. If you can't find one for your use case, [Unified](https://unifiedjs.com/) has a helpful [guide on creating custom plugins](https://unifiedjs.com/learn/guide/create-a-rehype-plugin/).
25+
For a more direct example, if we wanted to change the `href` on links that included the text string `react`, we could write a very basic plugin like this:
26+
27+
```
28+
import { visit } from 'unist-util-visit';
29+
30+
export const rehypeLinkHrefUpdater = (options) => (tree) => {
31+
visit(tree, 'element', (node) => {
32+
if (node.tagName === 'a' && node.properties) {
33+
if (node.properties.href.includes('react')) {
34+
node.properties.href = '#';
35+
}
36+
}
37+
});
38+
};
39+
40+
```
41+
42+
You could then pass `[rehypeLinkHrefUpdater]` to the `additionalRehypePlugins` prop on `<Message>` to have this applied to all relevant links read by that `<Message>` component.
43+
44+
## Modifying dynamic content
45+
46+
Props can also be used to modify dynamic content in messages.
47+
48+
Continuing with our link modification example, let's say you also wanted to add custom `onClick` events. In this case, you would not use `remark` or `rehype`, since they're for static tree manipulation.
49+
50+
Instead, you can utilize ChatBot features to customize dynamic content. We offer many `<Message>` props that allow you to implement custom behavior.
51+
For example, you can use `linkProps` to pass `onClick` events and other PatternFly `<Button>` props down to links. A very basic example would be `linkProps={{onClick: (event) => console.log(event}}`, which would apply to every link in the `<Message>`. Or, you could use the `event.target`, adding your own custom `onClick` logic based on the `innerText`, `innerHTML`, or other attributes of the `event.target`.

0 commit comments

Comments
 (0)