Skip to content

Commit

Permalink
make katex optional
Browse files Browse the repository at this point in the history
  • Loading branch information
cogentapps committed Jul 2, 2023
1 parent 1f739e9 commit 56582e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
21 changes: 15 additions & 6 deletions app/src/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const ImagePreview = styled.div`
export interface MarkdownProps {
content: string;
className?: string;
katex? : boolean;
}

export function Markdown(props: MarkdownProps) {
Expand All @@ -68,11 +69,19 @@ export function Markdown(props: MarkdownProps) {
return classes;
}, [props.className])

const elem = useMemo(() => (
<div className={classes.join(' ')}>
const elem = useMemo(() => {
const remarkPlugins: any[] = [remarkGfm];
const rehypePlugins: any[] = [];

if (props.katex) {
remarkPlugins.push(remarkMath);
rehypePlugins.push(rehypeKatex);
}

return <div className={classes.join(' ')}>
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeKatex]}
remarkPlugins={remarkPlugins}
rehypePlugins={rehypePlugins}
components={{
ol({ start, children }) {
return <ol start={start ?? 1} style={{ counterReset: `list-item ${(start || 1)}` }}>
Expand Down Expand Up @@ -129,8 +138,8 @@ export function Markdown(props: MarkdownProps) {
)
}
}}>{props.content}</ReactMarkdown>
</div>
), [props.content, classes, intl]);
</div>;
}, [props.content, props.katex, classes, intl]);

return elem;
}
7 changes: 6 additions & 1 deletion app/src/components/message.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from '@emotion/styled';
import { Button, CopyButton, Loader, Textarea } from '@mantine/core';

import { useOption } from '../core/options/use-option';
import { Message } from "../core/chat/types";
import { share } from '../core/utils';
import { TTSButton } from './tts-button';
Expand Down Expand Up @@ -210,6 +211,8 @@ export default function MessageComponent(props: { message: Message, last: boolea
const [content, setContent] = useState('');
const intl = useIntl();

const [katex] = useOption<boolean>('markdown', 'katex');

const tab = useAppSelector(selectSettingsTab);

const getRoleName = useCallback((role: string, share = false) => {
Expand Down Expand Up @@ -288,7 +291,9 @@ export default function MessageComponent(props: { message: Message, last: boolea
</Button>
)}
</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>
<Textarea value={content}
onChange={e => setContent(e.currentTarget.value)}
Expand Down
3 changes: 2 additions & 1 deletion app/src/global-options/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { OptionGroup } from "../core/options/option-group";
import { openAIOptions } from "./openai";
import { parameterOptions } from "./parameters";
import { ttsServiceOptions } from "./tts-service";
import { autoScrollOptions, inputOptions } from "./ui";
import { autoScrollOptions, inputOptions, markdownOptions } from "./ui";
import { whisperOptions } from "./whisper";

export const globalOptions: OptionGroup[] = [
openAIOptions,
autoScrollOptions,
parameterOptions,
inputOptions,
markdownOptions,
whisperOptions,
ttsServiceOptions,
];
Expand Down
16 changes: 16 additions & 0 deletions app/src/global-options/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
},
},
],
}

0 comments on commit 56582e6

Please sign in to comment.