@@ -41,15 +41,18 @@ export interface CodeBlockMessageProps {
4141 collapsedText ?: string ;
4242}
4343
44+ const DEFAULT_EXPANDED_TEXT = 'Show less' ;
45+ const DEFAULT_COLLAPSED_TEXT = 'Show more' ;
46+
4447const CodeBlockMessage = ( {
4548 children,
4649 className,
4750 'aria-label' : ariaLabel ,
4851 isExpandable = false ,
4952 expandableSectionProps,
5053 expandableSectionToggleProps,
51- expandedText = 'Show less' ,
52- collapsedText = 'Show more' ,
54+ expandedText = DEFAULT_EXPANDED_TEXT ,
55+ collapsedText = DEFAULT_COLLAPSED_TEXT ,
5356 ...props
5457} : CodeBlockMessageProps ) => {
5558 const [ copied , setCopied ] = useState ( false ) ;
@@ -63,6 +66,24 @@ const CodeBlockMessage = ({
6366
6467 const language = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ?. [ 1 ] ;
6568
69+ // Get custom toggle text from data attributes if available - for use with rehype plugins
70+ const customExpandedText = props [ 'data-expanded-text' ] ;
71+ const customCollapsedText = props [ 'data-collapsed-text' ] ;
72+
73+ const finalExpandedText = customExpandedText || expandedText ;
74+ const finalCollapsedText = customCollapsedText || collapsedText ;
75+
76+ if (
77+ ( customExpandedText && expandedText !== DEFAULT_EXPANDED_TEXT ) ||
78+ ( customCollapsedText && collapsedText !== DEFAULT_COLLAPSED_TEXT )
79+ ) {
80+ // eslint-disable-next-line no-console
81+ console . error (
82+ 'Message:' ,
83+ 'Custom rehype plugins that rely on data-expanded-text or data-collapsed-text will override expandedText and collapsedText props if both are passed in.'
84+ ) ;
85+ }
86+
6687 const onToggle = ( isExpanded ) => {
6788 setIsExpanded ( isExpanded ) ;
6889 } ;
@@ -164,7 +185,7 @@ const CodeBlockMessage = ({
164185 className = "pf-chatbot__message-code-toggle"
165186 { ...expandableSectionToggleProps }
166187 >
167- { isExpanded ? expandedText : collapsedText }
188+ { isExpanded ? finalExpandedText : finalCollapsedText }
168189 </ ExpandableSectionToggle >
169190 ) }
170191 </ CodeBlock >
0 commit comments