Skip to content

Commit

Permalink
use rendermime directrly in the react component
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-i committed Jan 9, 2024
1 parent 9345935 commit 017788d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 100 deletions.
4 changes: 2 additions & 2 deletions packages/jupyter-ai/src/components/chat-messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'katex/dist/katex.min.css';
import { AiService } from '../handler';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { Jupyternaut } from '../icons';
import { MarkdownComponent } from './markdown-component';
import { RendermimeMarkdown } from './rendermime-markdown';
import { useCollaboratorsContext } from '../contexts/collaborators-context';

type ChatMessagesProps = {
Expand Down Expand Up @@ -143,7 +143,7 @@ export function ChatMessages(props: ChatMessagesProps): JSX.Element {
timestamp={timestamps[message.id]}
sx={{ marginBottom: 3 }}
/>
<MarkdownComponent
<RendermimeMarkdown
rmRegistry={props.rmRegistry}
markdownStr={message.body}
/>
Expand Down
39 changes: 0 additions & 39 deletions packages/jupyter-ai/src/components/lumino-component.tsx

This file was deleted.

59 changes: 0 additions & 59 deletions packages/jupyter-ai/src/components/markdown-component.tsx

This file was deleted.

35 changes: 35 additions & 0 deletions packages/jupyter-ai/src/components/rendermime-markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useRef, useEffect } from 'react';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';

type RendermimeMarkdownProps = {
markdownStr: string;
rmRegistry: IRenderMimeRegistry;
};

export function RendermimeMarkdown(
props: RendermimeMarkdownProps
): JSX.Element {
const { markdownStr, rmRegistry } = props;
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
const renderMarkdown = async () => {
if (!ref.current) {
return;
}
ref.current.innerHTML = '';

const mimeType = 'text/markdown';
const model = rmRegistry.createModel({
data: { [mimeType]: markdownStr }
});
const renderer = rmRegistry.createRenderer(mimeType);
await renderer.renderModel(model);
ref.current.appendChild(renderer.node);
};

renderMarkdown();
}, [markdownStr, rmRegistry]);

return <div ref={ref} />;
}

0 comments on commit 017788d

Please sign in to comment.