import React from 'react'; import { MuiMarkdown } from 'mui-markdown'; import { useTheme } from '@mui/material/styles'; import { Link } from '@mui/material'; import { BackstoryQuery } from 'components/BackstoryQuery'; import Box from '@mui/material/Box'; import JsonView from '@uiw/react-json-view'; import { vscodeTheme } from '@uiw/react-json-view/vscode'; import { Mermaid } from 'components/Mermaid'; import { Scrollable } from 'components/Scrollable'; import { jsonrepair } from 'jsonrepair'; import { GenerateImage } from 'components/GenerateImage'; import './StyledMarkdown.css'; import { BackstoryElementProps } from './BackstoryTab'; import { ChatSession } from 'types/types'; interface StyledMarkdownProps extends BackstoryElementProps { className?: string, content: string, streaming?: boolean, chatSession?: ChatSession, }; const StyledMarkdown: React.FC = (props: StyledMarkdownProps) => { const { className, content, chatSession, submitQuery, sx, streaming, setSnack } = props; const theme = useTheme(); const overrides: any = { pre: { component: (element: any) => { const { className } = element.children.props; const content = element.children?.props?.children || ""; if (className === "lang-mermaid" && !streaming) { return ; } if (className === "lang-markdown") { return ; } if (className === "lang-json" && !streaming) { try { let fixed = JSON.parse(jsonrepair(content)); return { if (typeof (children) === "string" && children.match("\n")) { return
{children}
} }} />
} catch (e) { return
{content}
}; } return
{element.children}
; }, }, a: { component: Link, props: { onClick: (event: React.MouseEvent) => { const href = event.currentTarget.getAttribute('href'); console.log("StyledMarkdown onClick:", href); if (href) { if (href.match(/^\//)) { event.preventDefault(); window.history.replaceState({}, '', `${href}`); } } }, sx: { wordBreak: "break-all", color: theme.palette.secondary.main, textDecoration: 'none', '&:hover': { color: theme.palette.custom.highlight, textDecoration: 'underline', } } } }, BackstoryQuery: { component: (props: { query: string }) => { const queryString = props.query.replace(/(\w+):/g, '"$1":'); try { const query = JSON.parse(queryString); return } catch (e) { console.log("StyledMarkdown error:", queryString, e); return props.query; } }, } }; if (chatSession) { overrides.GenerateImage = { component: (props: { prompt: string }) => { const prompt = props.prompt.replace(/(\w+):/g, '"$1":'); try { return } catch (e) { console.log("StyledMarkdown error:", prompt, e); return props.prompt; } } } } return ; }; export { StyledMarkdown };