@@ -87,8 +87,8 @@ import type {
8787 ExtraProps as ReactMarkdownExtraProps ,
8888 Options as ReactMarkdownOptions ,
8989} from "react-markdown" ;
90- import ReactMarkdown from "react-markdown" ;
9190import { createIncrementalMarkdownPlugin } from "../markdown-incremental" ;
91+ import type { Pluggable , PluggableList } from "unified" ;
9292import { defaultUrlTransform } from "react-markdown" ;
9393import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations" ;
9494import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences" ;
@@ -189,6 +189,7 @@ import {
189189import { resolveLinkTarget } from "../browser/browserLinkTarget" ;
190190import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview" ;
191191import { MarkdownCodeBlock , SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock" ;
192+ import { createMarkdownRenderer } from "./chat/markdownRenderer" ;
192193import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks" ;
193194import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock" ;
194195import { MarkdownTable } from "./chat/MarkdownTable" ;
@@ -397,6 +398,14 @@ const CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS = [
397398 rehypeTextDirection ,
398399] satisfies NonNullable < ReactMarkdownOptions [ "rehypePlugins" ] > ;
399400
401+ function markdownUrlTransform ( href : string ) {
402+ if ( parseAssistantCitationHref ( href ) ) return href ;
403+ if ( parseComposerContextHref ( href ) ) return href ;
404+ if ( parseThreadLinkHref ( href ) ) return href ;
405+ if ( isWindowsDrivePathHref ( href ) ) return href ;
406+ return rewriteMarkdownFileUriHref ( href ) ?? defaultUrlTransform ( href ) ;
407+ }
408+
400409const NO_MARKDOWN_EDITORS : ReadonlyArray < EditorId > = [ ] ;
401410
402411type MarkdownServerConfigFields = ReturnType < typeof markdownServerConfigFields > ;
@@ -644,13 +653,6 @@ function useChatMarkdownState({
644653 } ) ;
645654 } ;
646655 const diffThemeName = resolveDiffThemeName ( resolvedTheme ) ;
647- const markdownUrlTransform = ( href : string ) => {
648- if ( parseAssistantCitationHref ( href ) ) return href ;
649- if ( parseComposerContextHref ( href ) ) return href ;
650- if ( parseThreadLinkHref ( href ) ) return href ;
651- if ( isWindowsDrivePathHref ( href ) ) return href ;
652- return rewriteMarkdownFileUriHref ( href ) ?? defaultUrlTransform ( href ) ;
653- } ;
654656 const handleCopy = ( event : ReactClipboardEvent < HTMLElement > ) => {
655657 const selection = window . getSelection ( ) ;
656658 if ( ! selection || selection . isCollapsed || ! event . clipboardData ) return ;
@@ -925,7 +927,6 @@ function useChatMarkdownState({
925927 componentState,
926928 handleCopy,
927929 markdownRef,
928- markdownUrlTransform,
929930 localMediaPreview,
930931 closeLocalMediaPreview,
931932 } ;
@@ -1633,29 +1634,62 @@ const CHAT_MARKDOWN_COMPONENTS = {
16331634 } ,
16341635} satisfies Components ;
16351636
1637+ const REMARK_MATH : Pluggable = [ remarkMath , { singleDollarTextMath : true } ] ;
1638+ // Shared lists, so every message with the same options shares a processor and its cache.
1639+ const CHAT_MARKDOWN_REMARK_PLUGIN_SETS = {
1640+ plain : CHAT_MARKDOWN_REMARK_PLUGINS ,
1641+ breaks : CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS ,
1642+ math : [ REMARK_MATH , ...CHAT_MARKDOWN_REMARK_PLUGINS ] ,
1643+ mathBreaks : [ REMARK_MATH , ...CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS ] ,
1644+ } satisfies Record < string , PluggableList > ;
1645+
1646+ const markdownRenderers = new WeakMap <
1647+ PluggableList ,
1648+ WeakMap < PluggableList , ReturnType < typeof createMarkdownRenderer > >
1649+ > ( ) ;
1650+
1651+ function markdownRenderer ( remarkPlugins : PluggableList , rehypePlugins : PluggableList ) {
1652+ let byRehype = markdownRenderers . get ( remarkPlugins ) ;
1653+ if ( ! byRehype ) markdownRenderers . set ( remarkPlugins , ( byRehype = new WeakMap ( ) ) ) ;
1654+ let renderer = byRehype . get ( rehypePlugins ) ;
1655+ if ( ! renderer ) {
1656+ renderer = createMarkdownRenderer ( {
1657+ remarkPlugins,
1658+ rehypePlugins,
1659+ components : CHAT_MARKDOWN_COMPONENTS ,
1660+ urlTransform : markdownUrlTransform ,
1661+ } ) ;
1662+ byRehype . set ( rehypePlugins , renderer ) ;
1663+ }
1664+ return renderer ;
1665+ }
1666+
16361667const MarkdownSource = memo ( function MarkdownSource ( {
16371668 text,
16381669 remarkPlugins,
16391670 rehypePlugins,
1640- urlTransform,
1671+ shared,
1672+ cache,
16411673} : {
16421674 text : string ;
1643- remarkPlugins : NonNullable < ReactMarkdownOptions [ "remarkPlugins" ] > ;
1644- rehypePlugins : NonNullable < ReactMarkdownOptions [ "rehypePlugins" ] > ;
1645- urlTransform : ReactMarkdownOptions [ "urlTransform" ] ;
1675+ remarkPlugins : PluggableList ;
1676+ rehypePlugins : PluggableList ;
1677+ /** The plugins are one of the shared sets. */
1678+ shared : boolean ;
1679+ /** The text will not change again; streamed text would only fill the cache with prefixes. */
1680+ cache : boolean ;
16461681} ) {
1682+ // Per-instance plugins, such as the incremental parser, set up their state on every render.
1683+ const render = shared
1684+ ? markdownRenderer ( remarkPlugins , rehypePlugins )
1685+ : createMarkdownRenderer ( {
1686+ remarkPlugins,
1687+ rehypePlugins,
1688+ components : CHAT_MARKDOWN_COMPONENTS ,
1689+ urlTransform : markdownUrlTransform ,
1690+ } ) ;
16471691 return (
1648- < ChatMarkdownTextContext value = { text } >
1649- < ReactMarkdown
1650- remarkPlugins = { remarkPlugins }
1651- rehypePlugins = { rehypePlugins }
1652- skipHtml = { false }
1653- components = { CHAT_MARKDOWN_COMPONENTS }
1654- urlTransform = { urlTransform }
1655- >
1656- { text }
1657- </ ReactMarkdown >
1658- </ ChatMarkdownTextContext >
1692+ < ChatMarkdownTextContext value = { text } > { render ( text , shared && cache ) } </ ChatMarkdownTextContext >
16591693 ) ;
16601694} ) ;
16611695
@@ -1668,14 +1702,8 @@ function ChatMarkdown({
16681702 extraRemarkPlugins = EMPTY_REMARK_PLUGINS ,
16691703 ...props
16701704} : ChatMarkdownProps ) {
1671- const {
1672- componentState,
1673- handleCopy,
1674- markdownRef,
1675- markdownUrlTransform,
1676- localMediaPreview,
1677- closeLocalMediaPreview,
1678- } = useChatMarkdownState ( { text, inline, ...props } ) ;
1705+ const { componentState, handleCopy, markdownRef, localMediaPreview, closeLocalMediaPreview } =
1706+ useChatMarkdownState ( { text, inline, ...props } ) ;
16791707 // Streaming re-renders only the last block; offsets for task edits need the whole text.
16801708 const streamingBlocks =
16811709 props . isStreaming === true &&
@@ -1693,13 +1721,26 @@ function ChatMarkdown({
16931721 const bindMarkdownRef = ( node : HTMLElement | null ) => {
16941722 markdownRef . current = node ;
16951723 } ;
1696- const remarkPlugins = [
1697- ...( lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS ) ,
1698- ...extraRemarkPlugins ,
1699- ...( incrementalParsing ? [ createIncrementalMarkdownPlugin ( ) ] : [ ] ) ,
1700- ] ;
1701- if ( componentState . mathEnabled )
1702- remarkPlugins . unshift ( [ remarkMath , { singleDollarTextMath : true } ] ) ;
1724+ const sharedRemarkPlugins =
1725+ CHAT_MARKDOWN_REMARK_PLUGIN_SETS [
1726+ componentState . mathEnabled
1727+ ? lineBreaks
1728+ ? "mathBreaks"
1729+ : "math"
1730+ : lineBreaks
1731+ ? "breaks"
1732+ : "plain"
1733+ ] ;
1734+ // Extra and incremental plugins are per instance, so their output is not shared.
1735+ const uncachedPlugins = extraRemarkPlugins . length > 0 || incrementalParsing ;
1736+ const remarkPlugins = uncachedPlugins
1737+ ? [
1738+ ...sharedRemarkPlugins ,
1739+ ...extraRemarkPlugins ,
1740+ ...( incrementalParsing ? [ createIncrementalMarkdownPlugin ( ) ] : [ ] ) ,
1741+ ]
1742+ : sharedRemarkPlugins ;
1743+ const blocks = streamingBlocks ?? [ text ] ;
17031744
17041745 return (
17051746 < Root
@@ -1714,7 +1755,7 @@ function ChatMarkdown({
17141755 onCopy = { handleCopy }
17151756 >
17161757 < ChatMarkdownRendererContext value = { componentState } >
1717- { ( streamingBlocks ?? [ text ] ) . map ( ( source , index ) => (
1758+ { blocks . map ( ( source , index ) => (
17181759 < MarkdownSource
17191760 // oxlint-disable-next-line react/no-array-index-key -- streamed blocks only change at the end
17201761 key = { index }
@@ -1725,7 +1766,8 @@ function ChatMarkdown({
17251766 ? CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS
17261767 : CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS
17271768 }
1728- urlTransform = { markdownUrlTransform }
1769+ shared = { ! uncachedPlugins }
1770+ cache = { ! props . isStreaming }
17291771 />
17301772 ) ) }
17311773 </ ChatMarkdownRendererContext >
0 commit comments