diff --git a/client/src/components/Prompts/Groups/VariableForm.tsx b/client/src/components/Prompts/Groups/VariableForm.tsx index d77d164f72b..793e278f8a4 100644 --- a/client/src/components/Prompts/Groups/VariableForm.tsx +++ b/client/src/components/Prompts/Groups/VariableForm.tsx @@ -1,5 +1,11 @@ import { useMemo } from 'react'; import { useForm, useFieldArray, Controller, useWatch } from 'react-hook-form'; +import ReactMarkdown from 'react-markdown'; +import remarkGfm from 'remark-gfm'; +import rehypeKatex from 'rehype-katex'; +import remarkMath from 'remark-math'; +import supersub from 'remark-supersub'; +import rehypeHighlight from 'rehype-highlight'; import type { TPromptGroup } from 'librechat-data-provider'; import { extractVariableInfo, wrapVariable, replaceSpecialVars } from '~/utils'; import { useAuthContext, useLocalize, useSubmitMessage } from '~/hooks'; @@ -50,31 +56,16 @@ export default function VariableForm({ return null; } - const generateHighlightedText = () => { + const generateHighlightedMarkdown = () => { let tempText = mainText; - const parts: JSX.Element[] = []; - allVariables.forEach((variable, index) => { const placeholder = `{{${variable}}}`; - const partsBeforePlaceholder = tempText.split(placeholder); const fieldIndex = variableIndexMap.get(variable) as string | number; const fieldValue = fieldValues[fieldIndex].value as string; - parts.push( - {partsBeforePlaceholder[0]}, - - {fieldValue !== '' ? fieldValue : placeholder} - , - ); - - tempText = partsBeforePlaceholder.slice(1).join(placeholder); + const highlightText = fieldValue !== '' ? fieldValue : placeholder; + tempText = tempText.replaceAll(placeholder, `**${highlightText}**`); }); - - parts.push({tempText}); - - return parts; + return tempText; }; const onSubmit = (data: FormValues) => { @@ -94,7 +85,13 @@ export default function VariableForm({
-

{generateHighlightedText()}

+ + {generateHighlightedMarkdown()} +
{fields.map((field, index) => ( diff --git a/client/src/components/Prompts/PromptEditor.tsx b/client/src/components/Prompts/PromptEditor.tsx index 869eb5be14a..5db05234112 100644 --- a/client/src/components/Prompts/PromptEditor.tsx +++ b/client/src/components/Prompts/PromptEditor.tsx @@ -2,6 +2,11 @@ import { useMemo, memo } from 'react'; import { useRecoilValue } from 'recoil'; import { EditIcon } from 'lucide-react'; import { Controller, useFormContext, useFormState } from 'react-hook-form'; +import remarkGfm from 'remark-gfm'; +import rehypeKatex from 'rehype-katex' +import remarkMath from 'remark-math'; +import supersub from 'remark-supersub'; +import ReactMarkdown from 'react-markdown'; import AlwaysMakeProd from '~/components/Prompts/Groups/AlwaysMakeProd'; import { SaveIcon, CrossIcon } from '~/components/svg'; import { TextareaAutosize } from '~/components/ui'; @@ -9,6 +14,9 @@ import { PromptsEditorMode } from '~/common'; import { useLocalize } from '~/hooks'; import { cn } from '~/utils'; import store from '~/store'; +import rehypeHighlight from 'rehype-highlight'; +import type { PluggableList } from 'unified'; +import { langSubset } from '~/utils'; const { promptsEditorMode } = store; @@ -32,6 +40,18 @@ const PromptEditor: React.FC = ({ name, isEditing, setIsEditing }) => { return isEditing ? SaveIcon : EditIcon; }, [isEditing, prompt]); + const rehypePlugins: PluggableList = [ + [rehypeKatex, { output: 'mathml' }], + [ + rehypeHighlight, + { + detect: true, + ignoreMissing: true, + subset: langSubset, + }, + ], + ]; + return (

@@ -85,9 +105,13 @@ const PromptEditor: React.FC = ({ name, isEditing, setIsEditing }) => { }} /> ) : ( -
+              
                 {field.value}
-              
+ ) } />