Adapt findLast api;perf: markdown zh format. (#3066)

* perf: context code

* fix: adapt findLast api

* perf: commercial plugin run error

* perf: markdown zh format
This commit is contained in:
Archer
2024-11-04 18:04:09 +08:00
committed by archer
parent 16280e5d94
commit e78fa26ca7
9 changed files with 51 additions and 20 deletions

View File

@@ -29,11 +29,13 @@ const QuestionGuide = dynamic(() => import('./chat/QuestionGuide'), { ssr: false
const Markdown = ({
source = '',
showAnimation = false,
isDisabled = false
isDisabled = false,
forbidZhFormat = false
}: {
source?: string;
showAnimation?: boolean;
isDisabled?: boolean;
forbidZhFormat?: boolean;
}) => {
const components = useMemo<any>(
() => ({
@@ -46,15 +48,35 @@ const Markdown = ({
);
const formatSource = useMemo(() => {
const formatSource = source
if (showAnimation || forbidZhFormat) return source;
// 保护 URL 格式https://, http://, /api/xxx
const urlPlaceholders: string[] = [];
const textWithProtectedUrls = source.replace(
/(https?:\/\/[^\s<]+[^<.,:;"')\]\s]|\/api\/[^\s]+)(?=\s|$)/g,
(match) => {
urlPlaceholders.push(match);
return `__URL_${urlPlaceholders.length - 1}__`;
}
);
// 处理中文与英文数字之间的分词
const textWithSpaces = textWithProtectedUrls
.replace(
/([\u4e00-\u9fa5\u3000-\u303f])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fa5\u3000-\u303f])/g,
'$1$3 $2$4'
) // Chinese and english chars separated by space
)
// 处理引用标记
.replace(/\n*(\[QUOTE SIGN\]\(.*\))/g, '$1');
return formatSource;
}, [source]);
// 还原 URL
const finalText = textWithSpaces.replace(
/__URL_(\d+)__/g,
(_, index) => urlPlaceholders[parseInt(index)]
);
return finalText;
}, [showAnimation, source]);
const urlTransform = useCallback((val: string) => {
return val;

View File

@@ -137,9 +137,8 @@ const ChatItem = (props: Props) => {
};
const { t } = useTranslation();
const { isChatting, chatType } = useContextSelector(ChatBoxContext, (v) => {
return { isChatting: v.isChatting, chatType: v.chatType };
});
const { isChatting, chatType } = useContextSelector(ChatBoxContext, (v) => v);
const isChatLog = chatType === 'log';
const { copyData } = useCopyData();
@@ -218,14 +217,14 @@ const ChatItem = (props: Props) => {
<Flex w={'100%'} alignItems={'flex-end'} gap={2} justifyContent={styleMap.justifyContent}>
{isChatting && type === ChatRoleEnum.AI && isLastChild ? null : (
<Flex order={styleMap.order} ml={styleMap.ml} align={'center'} gap={'0.62rem'}>
{chat.time && (isPc || chatType === 'log') && (
{chat.time && (isPc || isChatLog) && (
<Box
order={type === ChatRoleEnum.AI ? 2 : 0}
className={'time-label'}
fontSize={styleMap.fontSize}
color={styleMap.color}
fontWeight={styleMap.fontWeight}
display={chatType === 'log' ? 'block' : 'none'}
display={isChatLog ? 'block' : 'none'}
>
{t(formatTimeToChatItemTime(chat.time) as any, {
time: dayjs(chat.time).format('HH:mm')

View File

@@ -22,7 +22,7 @@ const WelcomeBox = ({ welcomeText }: { welcomeText: string }) => {
bg={'white'}
boxShadow={'0 0 8px rgba(0,0,0,0.15)'}
>
<Markdown source={`~~~guide \n${welcomeText}`} />
<Markdown source={`~~~guide \n${welcomeText}`} forbidZhFormat />
</Card>
</Box>
</Box>

View File

@@ -64,7 +64,7 @@ const Header = () => {
useDebounceEffect(
() => {
const savedSnapshot =
future.findLast((snapshot) => snapshot.isSaved) ||
[...future].reverse().find((snapshot) => snapshot.isSaved) ||
past.find((snapshot) => snapshot.isSaved);
const val = compareSnapshot(

View File

@@ -70,7 +70,7 @@ const Header = () => {
useDebounceEffect(
() => {
const savedSnapshot =
future.findLast((snapshot) => snapshot.isSaved) ||
[...future].reverse().find((snapshot) => snapshot.isSaved) ||
past.find((snapshot) => snapshot.isSaved);
const val = compareSnapshot(