@@ -67,12 +67,14 @@ import React, {
6767import type { Components , Options as ReactMarkdownOptions } from "react-markdown" ;
6868import ReactMarkdown from "react-markdown" ;
6969import { defaultUrlTransform } from "react-markdown" ;
70+ import rehypeKatex from "rehype-katex" ;
7071import rehypeRaw from "rehype-raw" ;
7172import rehypeSanitize , { defaultSchema } from "rehype-sanitize" ;
7273import remarkBreaks from "remark-breaks" ;
7374import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations" ;
7475import { AssistantCitationChip } from "./chat/AssistantCitationChip" ;
7576import remarkGfm from "remark-gfm" ;
77+ import remarkMath from "remark-math" ;
7678import { remarkGithubAlerts } from "../markdown-github-alerts" ;
7779import {
7880 artifactTemplateFromHastProperties ,
@@ -126,6 +128,7 @@ import {
126128 serializeTableElementToMarkdown ,
127129} from "../markdown-clipboard" ;
128130import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation" ;
131+ import { normalizeLatexDelimiters } from "../markdown-latex" ;
129132import {
130133 extractMarkdownLinkHrefs ,
131134 isWindowsDrivePathHref ,
@@ -396,6 +399,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
396399
397400const CHAT_MARKDOWN_REMARK_PLUGINS = [
398401 remarkGfm ,
402+ remarkMath ,
399403 remarkGithubAlerts ,
400404 remarkNormalizeListItemIndentation ,
401405 remarkCodexDirectives ,
@@ -405,6 +409,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS = [
405409
406410const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
407411 remarkGfm ,
412+ remarkMath ,
408413 remarkGithubAlerts ,
409414 remarkNormalizeListItemIndentation ,
410415 remarkCodexDirectives ,
@@ -413,10 +418,15 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
413418 remarkNormalizeLinksAndTagInlineCode ,
414419] satisfies NonNullable < ReactMarkdownOptions [ "remarkPlugins" ] > ;
415420
416- const CHAT_MARKDOWN_REHYPE_PLUGINS = [
421+ const CHAT_MARKDOWN_REHYPE_PLUGINS = [ rehypeKatex ] satisfies NonNullable <
422+ ReactMarkdownOptions [ "rehypePlugins" ]
423+ > ;
424+
425+ const CHAT_MARKDOWN_RAW_HTML_REHYPE_PLUGINS = [
417426 rehypeRaw ,
418427 rehypePreserveImageSourceMeta ,
419428 [ rehypeSanitize , CHAT_MARKDOWN_SANITIZE_SCHEMA ] ,
429+ rehypeKatex ,
420430] satisfies NonNullable < ReactMarkdownOptions [ "rehypePlugins" ] > ;
421431
422432/** GitHub's own five alert kinds, in its colors: the glyph names the urgency, the title says it. */
@@ -2908,6 +2918,7 @@ function ChatMarkdown({
29082918 ] ,
29092919 [ extraRemarkPlugins , lineBreaks ] ,
29102920 ) ;
2921+ const normalizedText = useMemo ( ( ) => normalizeLatexDelimiters ( text ) , [ text ] ) ;
29112922
29122923 // react-markdown converts unparsed HTML nodes to text when skipHtml is false.
29132924 // Keep that behavior explicit because literal mode depends on escaping the
@@ -2923,12 +2934,14 @@ function ChatMarkdown({
29232934 < ChatMarkdownRendererContext value = { componentState } >
29242935 < ReactMarkdown
29252936 remarkPlugins = { remarkPlugins }
2926- rehypePlugins = { parseRawHtml ? CHAT_MARKDOWN_REHYPE_PLUGINS : undefined }
2937+ rehypePlugins = {
2938+ parseRawHtml ? CHAT_MARKDOWN_RAW_HTML_REHYPE_PLUGINS : CHAT_MARKDOWN_REHYPE_PLUGINS
2939+ }
29272940 skipHtml = { false }
29282941 components = { CHAT_MARKDOWN_COMPONENTS }
29292942 urlTransform = { markdownUrlTransform }
29302943 >
2931- { text }
2944+ { normalizedText }
29322945 </ ReactMarkdown >
29332946 </ ChatMarkdownRendererContext >
29342947 { localMediaPreview ? (
0 commit comments