Skip to content

Commit b1b897e

Browse files
Hoist ChatMarkdown plugin arrays
- Reuse typed remark and rehype plugin arrays for markdown rendering
1 parent 87721c5 commit b1b897e

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import React, {
3232
useState,
3333
type ReactNode,
3434
} from "react";
35-
import type { Components } from "react-markdown";
35+
import type { Components, Options as ReactMarkdownOptions } from "react-markdown";
3636
import ReactMarkdown from "react-markdown";
3737
import { defaultUrlTransform } from "react-markdown";
3838
import rehypeRaw from "rehype-raw";
@@ -165,6 +165,24 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
165165
},
166166
} satisfies Parameters<typeof rehypeSanitize>[0];
167167

168+
const CHAT_MARKDOWN_REMARK_PLUGINS = [
169+
remarkGfm,
170+
remarkNormalizeListItemIndentation,
171+
remarkPreserveCodeMeta,
172+
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
173+
174+
const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
175+
remarkGfm,
176+
remarkNormalizeListItemIndentation,
177+
remarkBreaks,
178+
remarkPreserveCodeMeta,
179+
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
180+
181+
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
182+
rehypeRaw,
183+
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
184+
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
185+
168186
function extractFenceLanguage(className: string | undefined): string {
169187
const match = className?.match(CODE_FENCE_LANGUAGE_REGEX);
170188
const raw = match?.[1] ?? "text";
@@ -1546,11 +1564,9 @@ function ChatMarkdown({
15461564
>
15471565
<ReactMarkdown
15481566
remarkPlugins={
1549-
lineBreaks
1550-
? [remarkGfm, remarkNormalizeListItemIndentation, remarkBreaks, remarkPreserveCodeMeta]
1551-
: [remarkGfm, remarkNormalizeListItemIndentation, remarkPreserveCodeMeta]
1567+
lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS
15521568
}
1553-
rehypePlugins={[rehypeRaw, [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA]]}
1569+
rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
15541570
components={markdownComponents}
15551571
urlTransform={markdownUrlTransform}
15561572
>

0 commit comments

Comments
 (0)