@@ -10,15 +10,31 @@ import { validateProps } from '@msinternal/botframework-webchat-react-valibot';
1010import { useStyleOptions } from 'botframework-webchat-api/hook.js' ;
1111import { micromark } from 'micromark' ;
1212import React , { useCallback , useMemo } from 'react' ;
13- import { array , function_ , object , optional , pipe , readonly , string , type InferInput } from 'valibot' ;
13+ import {
14+ args ,
15+ array ,
16+ function_ ,
17+ instance ,
18+ object ,
19+ optional ,
20+ parse ,
21+ pipe ,
22+ readonly ,
23+ string ,
24+ tuple ,
25+ union ,
26+ type InferInput
27+ } from 'valibot' ;
1428
1529import { useStyleToEmotionObject } from '../hooks/internal/styleToEmotionObject' ;
1630import createCustomEvent from './createCustomEvent' ;
1731
32+ const referenceEventSchema = union ( [ instance ( Event ) , object ( { data : string ( ) } ) ] ) ;
33+
1834const inlineMarkdownPropsSchema = pipe (
1935 object ( {
2036 markdown : string ( ) ,
21- onReference : function_ ( ) ,
37+ onReference : pipe ( function_ ( ) , args ( tuple ( [ referenceEventSchema ] ) ) ) ,
2238 references : optional ( array ( string ( ) ) )
2339 } ) ,
2440 readonly ( )
@@ -71,16 +87,13 @@ const InlineMarkdown = (props: InlineMarkdownProps) => {
7187
7288 const documentFragment = parseDocumentFragmentFromString ( micromark ( markdownWithLinkReferenceDefinitions ) ) ;
7389
74- // Turn "<a href="#retry">Retry</a>" into "<button type="button" value=" retry">Retry</button>"
90+ // Turn "<a href="#retry">Retry</a>" into "<button type="button" data-markdown-ref="# retry">Retry</button>"
7591 betterLinkDocumentMod ( documentFragment , href => {
7692 if ( href . startsWith ( '#' ) ) {
77- return { asButton : true } ;
93+ return { asButton : true , dataset : { markdownHref : href } } ;
7894 }
7995 } ) ;
8096
81- // TODO: [P*] Instead of <button value="">, it need to be <button data-markdown-href="a1b2c"> to trigger the CSS.
82- // TODO: [P*] Remove trailing <p></p>.
83-
8497 return { __html : stripParagraphContainer ( serializeDocumentFragmentIntoString ( documentFragment ) ) } ;
8598 } , [ markdown , references ] ) ;
8699
@@ -90,7 +103,12 @@ const InlineMarkdown = (props: InlineMarkdownProps) => {
90103
91104 const href = event . target . getAttribute ( 'value' ) ?? undefined ;
92105
93- href && onReference ?.( createCustomEvent ( 'reference' , { data : href } ) ) ;
106+ if ( href ) {
107+ const event = createCustomEvent ( 'reference' , { data : href } ) ;
108+
109+ parse ( referenceEventSchema , event ) ;
110+ onReference ?.( event ) ;
111+ }
94112 } ,
95113 [ onReference ]
96114 ) ;
0 commit comments