@@ -516,8 +516,43 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
516516 remarkNormalizeLinksAndTagInlineCode ,
517517] satisfies NonNullable < ReactMarkdownOptions [ "remarkPlugins" ] > ;
518518
519+ function rehypeRawMath ( ) {
520+ const parseRaw = rehypeRaw ( { passThrough : [ "chatMath" ] } ) ;
521+ return ( ...[ tree , file ] : Parameters < typeof parseRaw > ) => {
522+ const protect = ( node : MarkdownImageHastNode ) => {
523+ if (
524+ node . type === "element" &&
525+ node . tagName === "code" &&
526+ typeof node . properties ?. dataMathSource === "string"
527+ ) {
528+ // Raw HTML can create elements and attributes, but not this AST node type.
529+ node . type = "chatMath" ;
530+ }
531+ node . children ?. forEach ( protect ) ;
532+ } ;
533+ protect ( tree ) ;
534+ const parsed = parseRaw ( tree , file ) ;
535+ const restore = ( node : MarkdownImageHastNode ) => {
536+ if ( node . type === "chatMath" ) {
537+ node . type = "element" ;
538+ } else if ( node . type === "element" && node . properties ) {
539+ delete node . properties . dataMathSource ;
540+ const classes = node . properties . className ;
541+ if ( Array . isArray ( classes ) ) {
542+ node . properties . className = classes . filter (
543+ ( name ) => name !== "math-inline" && name !== "math-display" ,
544+ ) ;
545+ }
546+ }
547+ node . children ?. forEach ( restore ) ;
548+ } ;
549+ restore ( parsed ) ;
550+ return parsed ;
551+ } ;
552+ }
553+
519554const CHAT_MARKDOWN_REHYPE_PLUGINS = [
520- rehypeRaw ,
555+ rehypeRawMath ,
521556 rehypePreserveImageSourceMeta ,
522557 [ rehypeSanitize , CHAT_MARKDOWN_SANITIZE_SCHEMA ] ,
523558] satisfies NonNullable < ReactMarkdownOptions [ "rehypePlugins" ] > ;
0 commit comments