Skip to content

Commit

Permalink
Merge pull request #1363 from vibhatha/fix-chatbox-markdown-rendering
Browse files Browse the repository at this point in the history
Chatbox markdown format rendering
  • Loading branch information
aarondr77 authored Nov 20, 2024
2 parents 6606fa6 + a4e4392 commit 496413c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mito-ai/src/Extensions/AiChat/ChatMessage/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import OpenAI from 'openai';
import { classNames } from '../../../utils/classNames';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import CodeBlock from './CodeBlock';
import MarkdownBlock from './MarkdownBlock';
import { INotebookTracker } from '@jupyterlab/notebook';
import { PYTHON_CODE_BLOCK_START_WITHOUT_NEW_LINE, splitStringWithCodeBlocks } from '../../../utils/strings';
import ErrorIcon from '../../../icons/ErrorIcon';
Expand Down Expand Up @@ -96,7 +97,7 @@ const ChatMessage: React.FC<IChatMessageProps> = ({
if (messagePart.length > 14) {
return (
<CodeBlock
key={index + messagePart}
key={index + messagePart}
code={messagePart}
role={message.role}
rendermime={rendermime}
Expand All @@ -113,9 +114,12 @@ const ChatMessage: React.FC<IChatMessageProps> = ({
} else {
return (
<div style={{ position: 'relative' }}>
<p onDoubleClick={() => setIsEditing(true)}>
<p key={index + messagePart} onDoubleClick={() => setIsEditing(true)}>
{mitoAIConnectionError && <span style={{ marginRight: '4px' }}><ErrorIcon /></span>}
{messagePart}
<MarkdownBlock
markdown={messagePart}
rendermime={rendermime}
/>
</p>
{message.role === 'user' && (
<div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '4px' }}>
Expand Down
35 changes: 35 additions & 0 deletions mito-ai/src/Extensions/AiChat/ChatMessage/MarkdownBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useEffect, useState } from 'react';
import { IRenderMimeRegistry, MimeModel } from '@jupyterlab/rendermime';


interface IMarkdownCodeProps {
markdown: string;
rendermime: IRenderMimeRegistry;
}

const MarkdownBlock: React.FC<IMarkdownCodeProps> = ({ markdown, rendermime }) => {
const [renderedContent, setRenderedContent] = useState<JSX.Element | null>(null);


useEffect(() => {
const renderMarkdown = async () => {
const model = new MimeModel({
data: { ['text/markdown']: markdown },
});

const renderer = rendermime.createRenderer('text/markdown');
await renderer.renderModel(model);

const node = renderer.node;
setRenderedContent(<div ref={(el) => el && el.appendChild(node)} />);
};

renderMarkdown();
}, [markdown, rendermime]);

return (
<div>{renderedContent || <div></div>} </div>
);
};

export default MarkdownBlock;

0 comments on commit 496413c

Please sign in to comment.