Streamdown (#1128)

This commit is contained in:
Hayden Bleasel 2025-08-18 22:48:13 -07:00 committed by GitHub
parent 256305d19f
commit 6894189c17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 389 additions and 137 deletions

View file

@ -1,38 +0,0 @@
'use client';
interface CodeBlockProps {
node: any;
inline: boolean;
className: string;
children: any;
}
export function CodeBlock({
node,
inline,
className,
children,
...props
}: CodeBlockProps) {
if (!inline) {
return (
<div className="not-prose flex flex-col">
<pre
{...props}
className={`text-sm w-full overflow-x-auto dark:bg-zinc-900 p-4 border border-zinc-200 dark:border-zinc-700 rounded-xl dark:text-zinc-50 text-zinc-900`}
>
<code className="whitespace-pre-wrap break-words">{children}</code>
</pre>
</div>
);
} else {
return (
<code
className={`${className} text-sm bg-zinc-100 dark:bg-zinc-800 py-0.5 px-1 rounded-md`}
{...props}
>
{children}
</code>
);
}
}

View file

@ -11,7 +11,7 @@ import { EditorState } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import React, { useEffect, useRef } from 'react';
import { renderToString } from 'react-dom/server';
import ReactMarkdown from 'react-markdown';
import { Streamdown } from 'streamdown';
import { diffEditor, DiffType } from '@/lib/editor/diff';
@ -60,10 +60,10 @@ export const DiffView = ({ oldContent, newContent }: DiffEditorProps) => {
const parser = DOMParser.fromSchema(diffSchema);
const oldHtmlContent = renderToString(
<ReactMarkdown>{oldContent}</ReactMarkdown>,
<Streamdown>{oldContent}</Streamdown>,
);
const newHtmlContent = renderToString(
<ReactMarkdown>{newContent}</ReactMarkdown>,
<Streamdown>{newContent}</Streamdown>,
);
const oldContainer = document.createElement('div');

View file

@ -1,41 +1,10 @@
import Link from 'next/link';
import React, { memo } from 'react';
import ReactMarkdown, { type Components } from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { CodeBlock } from './code-block';
import { Streamdown, type StreamdownProps } from 'streamdown';
type Components = StreamdownProps['components'];
const components: Partial<Components> = {
// @ts-expect-error
code: CodeBlock,
pre: ({ children }) => <>{children}</>,
ol: ({ node, children, ...props }) => {
return (
<ol className="list-decimal list-outside ml-4" {...props}>
{children}
</ol>
);
},
li: ({ node, children, ...props }) => {
return (
<li className="py-1" {...props}>
{children}
</li>
);
},
ul: ({ node, children, ...props }) => {
return (
<ul className="list-decimal list-outside ml-4" {...props}>
{children}
</ul>
);
},
strong: ({ node, children, ...props }) => {
return (
<span className="font-semibold" {...props}>
{children}
</span>
);
},
a: ({ node, children, ...props }) => {
return (
// @ts-expect-error
@ -49,59 +18,11 @@ const components: Partial<Components> = {
</Link>
);
},
h1: ({ node, children, ...props }) => {
return (
<h1 className="text-3xl font-semibold mt-6 mb-2" {...props}>
{children}
</h1>
);
},
h2: ({ node, children, ...props }) => {
return (
<h2 className="text-2xl font-semibold mt-6 mb-2" {...props}>
{children}
</h2>
);
},
h3: ({ node, children, ...props }) => {
return (
<h3 className="text-xl font-semibold mt-6 mb-2" {...props}>
{children}
</h3>
);
},
h4: ({ node, children, ...props }) => {
return (
<h4 className="text-lg font-semibold mt-6 mb-2" {...props}>
{children}
</h4>
);
},
h5: ({ node, children, ...props }) => {
return (
<h5 className="text-base font-semibold mt-6 mb-2" {...props}>
{children}
</h5>
);
},
h6: ({ node, children, ...props }) => {
return (
<h6 className="text-sm font-semibold mt-6 mb-2" {...props}>
{children}
</h6>
);
},
};
const remarkPlugins = [remarkGfm];
const NonMemoizedMarkdown = ({ children }: { children: string }) => {
return (
<ReactMarkdown remarkPlugins={remarkPlugins} components={components}>
{children}
</ReactMarkdown>
);
};
const NonMemoizedMarkdown = ({ children }: { children: string }) => (
<Streamdown components={components}>{children}</Streamdown>
);
export const Markdown = memo(
NonMemoizedMarkdown,