Skip to content

Commit 538ed4c

Browse files
committed
chore(docs): Add info on how to use plugins
1 parent 5abf296 commit 538ed4c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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. We use [react-markdown](https://github.com/remarkjs/react-markdown) to handle this mapping. This library supports both [rehype](https://unifiedjs.com/explore/package/rehype/) and [remark](https://unifiedjs.com/explore/package/remark/) plugins for further customization of the output.
19+
20+
Remark handles parsing Markdown as input and serializing 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: 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 depending on the link text. The `<Message>` prop `rehypePlugins` will take any custom rehype plugins for the Message component. We use these plugins already within the extension for [rendering images a certain way](https://www.npmjs.com/package/rehype-unwrap-images) and [handling external links](https://www.npmjs.com/package/rehype-external-links). There are many open-source libraries available for other common use cases as well.
23+
24+
If you can't find one for your use case, [Unified](https://unifiedjs.com/) has [a helpful guide](https://unifiedjs.com/learn/guide/create-a-rehype-plugin/) on how to create custom plugins. 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:
25+
26+
```
27+
import { visit } from 'unist-util-visit';
28+
29+
export const rehypeLinkHrefUpdater = (options) => (tree) => {
30+
visit(tree, 'element', (node) => {
31+
if (node.tagName === 'a' && node.properties) {
32+
if (node.properties.href.includes('react')) {
33+
node.properties.href = '#';
34+
}
35+
}
36+
});
37+
};
38+
39+
```
40+
41+
You could then pass `[rehypeLinkHrefUpdater]` to the `additionalRehypePlugins` prop on `<Message>` to have this applied to all relevant links read by that `<Message>` component.
42+
43+
# Modifying dynamic content
44+
45+
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`. These libraries are for static tree manipulation - JavaScript is done through the extension itself.
46+
47+
We offer many props on `<Message>` to allow for passing in custom behavior. As an 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}}`. This would apply to every link in the Message, but you should be able to grab the event.target and add your own custom onClick logic based on the innerText, innerHTML, or other attributes of the event.target.

0 commit comments

Comments
 (0)