### What problem does this PR solve? [remarkjs/react-markdown/issues/785](https://github.com/remarkjs/react-markdown/issues/785) Fix: Fixed an issue where math formulas could not be displayed correctly #4405 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { EmptyConversationId } from '@/constants/chat';
|
|
import { Message } from '@/interfaces/database/chat';
|
|
import { IMessage } from '@/pages/chat/interface';
|
|
import { omit } from 'lodash';
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
export const isConversationIdExist = (conversationId: string) => {
|
|
return conversationId !== EmptyConversationId && conversationId !== '';
|
|
};
|
|
|
|
export const buildMessageUuid = (message: Partial<Message | IMessage>) => {
|
|
if ('id' in message && message.id) {
|
|
return message.id;
|
|
}
|
|
return uuid();
|
|
};
|
|
|
|
export const buildMessageListWithUuid = (messages?: Message[]) => {
|
|
return (
|
|
messages?.map((x: Message | IMessage) => ({
|
|
...omit(x, 'reference'),
|
|
id: buildMessageUuid(x),
|
|
})) ?? []
|
|
);
|
|
};
|
|
|
|
export const getConversationId = () => {
|
|
return uuid().replace(/-/g, '');
|
|
};
|
|
|
|
// When rendering each message, add a prefix to the id to ensure uniqueness.
|
|
export const buildMessageUuidWithRole = (
|
|
message: Partial<Message | IMessage>,
|
|
) => {
|
|
return `${message.role}_${message.id}`;
|
|
};
|
|
|
|
// Preprocess LaTeX equations to be rendered by KaTeX
|
|
// ref: https://github.com/remarkjs/react-markdown/issues/785
|
|
|
|
export const preprocessLaTeX = (content: string) => {
|
|
const blockProcessedContent = content.replace(
|
|
/\\\[([\s\S]*?)\\\]/g,
|
|
(_, equation) => `$$${equation}$$`,
|
|
);
|
|
const inlineProcessedContent = blockProcessedContent.replace(
|
|
/\\\(([\s\S]*?)\\\)/g,
|
|
(_, equation) => `$${equation}$`,
|
|
);
|
|
return inlineProcessedContent;
|
|
};
|