make katex optional

main
cogentapps 2023-07-02 08:00:39 +00:00
parent 1f739e94c4
commit 56582e6f5e
4 changed files with 39 additions and 8 deletions

View File

@ -53,6 +53,7 @@ const ImagePreview = styled.div`
export interface MarkdownProps { export interface MarkdownProps {
content: string; content: string;
className?: string; className?: string;
katex? : boolean;
} }
export function Markdown(props: MarkdownProps) { export function Markdown(props: MarkdownProps) {
@ -68,11 +69,19 @@ export function Markdown(props: MarkdownProps) {
return classes; return classes;
}, [props.className]) }, [props.className])
const elem = useMemo(() => ( const elem = useMemo(() => {
<div className={classes.join(' ')}> const remarkPlugins: any[] = [remarkGfm];
const rehypePlugins: any[] = [];
if (props.katex) {
remarkPlugins.push(remarkMath);
rehypePlugins.push(rehypeKatex);
}
return <div className={classes.join(' ')}>
<ReactMarkdown <ReactMarkdown
remarkPlugins={[remarkGfm, remarkMath]} remarkPlugins={remarkPlugins}
rehypePlugins={[rehypeKatex]} rehypePlugins={rehypePlugins}
components={{ components={{
ol({ start, children }) { ol({ start, children }) {
return <ol start={start ?? 1} style={{ counterReset: `list-item ${(start || 1)}` }}> return <ol start={start ?? 1} style={{ counterReset: `list-item ${(start || 1)}` }}>
@ -129,8 +138,8 @@ export function Markdown(props: MarkdownProps) {
) )
} }
}}>{props.content}</ReactMarkdown> }}>{props.content}</ReactMarkdown>
</div> </div>;
), [props.content, classes, intl]); }, [props.content, props.katex, classes, intl]);
return elem; return elem;
} }

View File

@ -1,6 +1,7 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { Button, CopyButton, Loader, Textarea } from '@mantine/core'; import { Button, CopyButton, Loader, Textarea } from '@mantine/core';
import { useOption } from '../core/options/use-option';
import { Message } from "../core/chat/types"; import { Message } from "../core/chat/types";
import { share } from '../core/utils'; import { share } from '../core/utils';
import { TTSButton } from './tts-button'; import { TTSButton } from './tts-button';
@ -210,6 +211,8 @@ export default function MessageComponent(props: { message: Message, last: boolea
const [content, setContent] = useState(''); const [content, setContent] = useState('');
const intl = useIntl(); const intl = useIntl();
const [katex] = useOption<boolean>('markdown', 'katex');
const tab = useAppSelector(selectSettingsTab); const tab = useAppSelector(selectSettingsTab);
const getRoleName = useCallback((role: string, share = false) => { const getRoleName = useCallback((role: string, share = false) => {
@ -288,7 +291,9 @@ export default function MessageComponent(props: { message: Message, last: boolea
</Button> </Button>
)} )}
</div> </div>
{!editing && <Markdown content={props.message.content} className={"content content-" + props.message.id} />} {!editing && <Markdown content={props.message.content}
katex={katex}
className={"content content-" + props.message.id} />}
{editing && (<Editor> {editing && (<Editor>
<Textarea value={content} <Textarea value={content}
onChange={e => setContent(e.currentTarget.value)} onChange={e => setContent(e.currentTarget.value)}

View File

@ -4,7 +4,7 @@ import { OptionGroup } from "../core/options/option-group";
import { openAIOptions } from "./openai"; import { openAIOptions } from "./openai";
import { parameterOptions } from "./parameters"; import { parameterOptions } from "./parameters";
import { ttsServiceOptions } from "./tts-service"; import { ttsServiceOptions } from "./tts-service";
import { autoScrollOptions, inputOptions } from "./ui"; import { autoScrollOptions, inputOptions, markdownOptions } from "./ui";
import { whisperOptions } from "./whisper"; import { whisperOptions } from "./whisper";
export const globalOptions: OptionGroup[] = [ export const globalOptions: OptionGroup[] = [
@ -12,6 +12,7 @@ export const globalOptions: OptionGroup[] = [
autoScrollOptions, autoScrollOptions,
parameterOptions, parameterOptions,
inputOptions, inputOptions,
markdownOptions,
whisperOptions, whisperOptions,
ttsServiceOptions, ttsServiceOptions,
]; ];

View File

@ -47,4 +47,20 @@ export const inputOptions: OptionGroup = {
}, },
}, },
], ],
}
export const markdownOptions: OptionGroup = {
id: 'markdown',
name: "Markdown",
options: [
{
id: 'katex',
defaultValue: false,
displayOnSettingsScreen: "ui",
renderProps: {
type: "checkbox",
label: "Enable Katex math rendering (experimental)",
},
},
],
} }