@@ -90,13 +90,15 @@ import { parseComposerContextHref } from "@t3tools/shared/composerContextReferen
9090import { AssistantCitationChip } from "./chat/AssistantCitationChip" ;
9191import remarkGfm from "remark-gfm" ;
9292import { remarkGithubAlerts } from "../markdown-github-alerts" ;
93+ import { mathKind , mayContainMath , remarkChatMath } from "../markdown-math" ;
9394import {
9495 artifactTemplateFromHastProperties ,
9596 CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES ,
9697 remarkCodexDirectives ,
9798 renderCodexFileCitationsAsMarkdown ,
9899} from "@t3tools/client-runtime/codex-markdown-directives" ;
99100import { renderSkillInlineMarkdownChildren } from "./chat/SkillInlineText" ;
101+ import { MarkdownMath } from "./chat/MarkdownMath" ;
100102import {
101103 resolveMarkdownMediaPreview ,
102104 type ExpandedImagePreview ,
@@ -470,7 +472,12 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
470472 attributes : {
471473 ...defaultSchema . attributes ,
472474 "*" : ( defaultSchema . attributes ?. [ "*" ] ?? [ ] ) . filter ( ( attribute ) => attribute !== "title" ) ,
473- code : [ ...( defaultSchema . attributes ?. code ?? [ ] ) , "dataCodeMeta" , "dataInlineCode" ] ,
475+ // Fence languages plus the two math classes `remarkChatMath` emits.
476+ code : [
477+ [ "className" , / ^ l a n g u a g e - ./ , "math-inline" , "math-display" ] ,
478+ "dataCodeMeta" ,
479+ "dataInlineCode" ,
480+ ] ,
474481 blockquote : [ ...( defaultSchema . attributes ?. blockquote ?? [ ] ) , "dataAlert" ] ,
475482 div : [ ...( defaultSchema . attributes ?. div ?? [ ] ) , ...CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES ] ,
476483 a : [ ...( defaultSchema . attributes ?. a ?? [ ] ) , "dataPullRequestAutolink" ] ,
@@ -507,6 +514,22 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
507514 remarkNormalizeLinksAndTagInlineCode ,
508515] satisfies NonNullable < ReactMarkdownOptions [ "remarkPlugins" ] > ;
509516
517+ const CHAT_MARKDOWN_MATH_REMARK_PLUGINS = [ remarkChatMath , ...CHAT_MARKDOWN_REMARK_PLUGINS ] ;
518+
519+ const CHAT_MARKDOWN_MATH_REMARK_PLUGINS_WITH_BREAKS = [
520+ remarkChatMath ,
521+ ...CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS ,
522+ ] ;
523+
524+ function chatMarkdownRemarkPlugins ( lineBreaks : boolean , math : boolean ) {
525+ if ( math ) {
526+ return lineBreaks
527+ ? CHAT_MARKDOWN_MATH_REMARK_PLUGINS_WITH_BREAKS
528+ : CHAT_MARKDOWN_MATH_REMARK_PLUGINS ;
529+ }
530+ return lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS ;
531+ }
532+
510533const CHAT_MARKDOWN_REHYPE_PLUGINS = [
511534 rehypeRaw ,
512535 rehypePreserveImageSourceMeta ,
@@ -3315,6 +3338,19 @@ const CHAT_MARKDOWN_COMPONENTS = {
33153338 } ,
33163339} satisfies Components ;
33173340
3341+ const MarkdownCodeOrMath : NonNullable < Components [ "code" ] > = ( props ) => {
3342+ const math = mathKind ( props . className ) ;
3343+ if ( ! math ) return CHAT_MARKDOWN_COMPONENTS . code ( props ) ;
3344+ return < MarkdownMath tex = { nodeToPlainText ( props . children ) } display = { math === "display" } /> ;
3345+ } ;
3346+
3347+ // Only messages parsed with math enabled can typeset, so raw HTML carrying the
3348+ // math classes stays an ordinary code element while the setting is off.
3349+ const CHAT_MARKDOWN_MATH_COMPONENTS = {
3350+ ...CHAT_MARKDOWN_COMPONENTS ,
3351+ code : MarkdownCodeOrMath ,
3352+ } satisfies Components ;
3353+
33183354function ChatMarkdown ( {
33193355 text,
33203356 className,
@@ -3335,13 +3371,15 @@ function ChatMarkdown({
33353371 props . isStreaming === true &&
33363372 extraRemarkPlugins . length === 0 &&
33373373 / (?: ^ | \n ) { 0 , 3 } (?: ` { 3 } | ~ { 3 } ) / . test ( text ) ;
3374+ const math =
3375+ useClientSettings ( ( settings ) => settings . mathRenderingEnabled ) && mayContainMath ( text ) ;
33383376 const remarkPlugins = useMemo (
33393377 ( ) => [
3340- ...( lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS ) ,
3378+ ...chatMarkdownRemarkPlugins ( lineBreaks , math ) ,
33413379 ...extraRemarkPlugins ,
33423380 ...( incrementalParsing ? [ createIncrementalMarkdownPlugin ( ) ] : [ ] ) ,
33433381 ] ,
3344- [ extraRemarkPlugins , incrementalParsing , lineBreaks ] ,
3382+ [ extraRemarkPlugins , incrementalParsing , lineBreaks , math ] ,
33453383 ) ;
33463384
33473385 // react-markdown converts unparsed HTML nodes to text when skipHtml is false.
@@ -3363,7 +3401,7 @@ function ChatMarkdown({
33633401 remarkPlugins = { remarkPlugins }
33643402 rehypePlugins = { parseRawHtml ? CHAT_MARKDOWN_REHYPE_PLUGINS : undefined }
33653403 skipHtml = { false }
3366- components = { CHAT_MARKDOWN_COMPONENTS }
3404+ components = { math ? CHAT_MARKDOWN_MATH_COMPONENTS : CHAT_MARKDOWN_COMPONENTS }
33673405 urlTransform = { markdownUrlTransform }
33683406 >
33693407 { text }
0 commit comments