@agentscope-ai/chat 1.1.39 → 1.1.41-beta.1764749089549
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/starter_webui.zip +0 -0
- package/components/AgentScopeRuntimeWebUI/core/AgentScopeRuntime/Response/Builder.tsx +8 -4
- package/components/ImageGenerator/index.tsx +1 -1
- package/components/Markdown/Markdown.tsx +105 -0
- package/components/Markdown/{Markdown/defaultComponents → core/components}/CodeBlock.tsx +1 -1
- package/components/Markdown/core/components/Null.tsx +1 -0
- package/components/Markdown/core/components/Raw.tsx +18 -0
- package/components/Markdown/{Markdown → core}/hooks/useCitationsData.tsx +3 -7
- package/components/Markdown/core/hooks/useCursorContent.tsx +15 -0
- package/components/Markdown/{Markdown/hooks/useTyping.ts → core/hooks/useTyping.tsx} +4 -1
- package/components/Markdown/core/index.tsx +18 -0
- package/components/Markdown/{plugins → core/plugins}/citations/CitationComponent.tsx +5 -3
- package/components/Markdown/{plugins → core/plugins}/citations/index.tsx +3 -4
- package/components/Markdown/{plugins → core/plugins}/cursor/Underline.tsx +1 -1
- package/components/Markdown/{plugins → core/plugins}/cursor/index.tsx +8 -8
- package/components/Markdown/demo/cursor.tsx +2 -3
- package/components/Markdown/index.en-US.md +1 -2
- package/components/Markdown/index.tsx +6 -0
- package/components/Markdown/index.zh-CN.md +1 -2
- package/components/Markdown/{Markdown/style.ts → styles.ts} +12 -21
- package/components/Markdown/types.tsx +94 -0
- package/components/index.ts +1 -3
- package/lib/AgentScopeRuntimeWebUI/core/AgentScopeRuntime/Response/Builder.js +5 -3
- package/lib/ImageGenerator/index.js +1 -1
- package/lib/Markdown/Markdown.d.ts +4 -0
- package/lib/Markdown/Markdown.js +110 -0
- package/lib/Markdown/core/components/Null.d.ts +2 -0
- package/lib/Markdown/core/components/Null.js +3 -0
- package/lib/Markdown/core/components/Raw.d.ts +8 -0
- package/lib/Markdown/core/components/Raw.js +13 -0
- package/lib/Markdown/core/hooks/useCitationsData.d.ts +6 -0
- package/lib/Markdown/{Markdown → core}/hooks/useCitationsData.js +1 -1
- package/lib/Markdown/core/hooks/useCursorContent.d.ts +4 -0
- package/lib/Markdown/core/hooks/useCursorContent.js +14 -0
- package/lib/Markdown/core/hooks/useTyping.d.ts +5 -0
- package/lib/Markdown/{Markdown → core}/hooks/useTyping.js +3 -2
- package/lib/Markdown/core/index.d.ts +2 -0
- package/lib/Markdown/{Markdown/hooks/useAnimation.js → core/index.js} +23 -23
- package/lib/Markdown/{plugins → core/plugins}/citations/CitationComponent.js +5 -5
- package/lib/Markdown/{plugins → core/plugins}/citations/index.d.ts +1 -2
- package/lib/Markdown/{plugins → core/plugins}/citations/index.js +1 -1
- package/lib/Markdown/{plugins → core/plugins}/cursor/Dot.js +1 -1
- package/lib/Markdown/{plugins → core/plugins}/cursor/Underline.js +1 -1
- package/lib/Markdown/{plugins → core/plugins}/cursor/index.js +4 -4
- package/lib/Markdown/index.d.ts +2 -1
- package/lib/Markdown/styles.js +32 -0
- package/lib/Markdown/types.d.ts +78 -0
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -2
- package/package.json +7 -31
- package/components/AGUI/components/HelpModal/icons.tsx +0 -68
- package/components/AGUI/components/HelpModal/index.tsx +0 -1
- package/components/AGUI/components/HelpModal/modal.tsx +0 -101
- package/components/AGUI/components/chat/Button.tsx +0 -18
- package/components/AGUI/components/chat/Chat.tsx +0 -780
- package/components/AGUI/components/chat/ChatContext.tsx +0 -248
- package/components/AGUI/components/chat/CodeBlock.tsx +0 -406
- package/components/AGUI/components/chat/Header.tsx +0 -22
- package/components/AGUI/components/chat/Icons.tsx +0 -237
- package/components/AGUI/components/chat/ImageUploadQueue.tsx +0 -77
- package/components/AGUI/components/chat/Input.tsx +0 -24
- package/components/AGUI/components/chat/Markdown.tsx +0 -134
- package/components/AGUI/components/chat/Messages.tsx +0 -259
- package/components/AGUI/components/chat/Modal.tsx +0 -133
- package/components/AGUI/components/chat/Popup.tsx +0 -57
- package/components/AGUI/components/chat/PoweredByTag.tsx +0 -29
- package/components/AGUI/components/chat/Sidebar.tsx +0 -74
- package/components/AGUI/components/chat/Suggestion.tsx +0 -132
- package/components/AGUI/components/chat/Suggestions.tsx +0 -20
- package/components/AGUI/components/chat/Textarea.tsx +0 -61
- package/components/AGUI/components/chat/Window.tsx +0 -152
- package/components/AGUI/components/chat/index.tsx +0 -11
- package/components/AGUI/components/chat/messages/AssistantMessage.tsx +0 -69
- package/components/AGUI/components/chat/messages/RenderActionExecutionMessage.tsx +0 -129
- package/components/AGUI/components/chat/messages/RenderAgentStateMessage.tsx +0 -116
- package/components/AGUI/components/chat/messages/RenderImageMessage.tsx +0 -64
- package/components/AGUI/components/chat/messages/RenderResultMessage.tsx +0 -26
- package/components/AGUI/components/chat/messages/RenderTextMessage.tsx +0 -51
- package/components/AGUI/components/chat/messages/UserMessage.tsx +0 -10
- package/components/AGUI/components/chat/props.ts +0 -186
- package/components/AGUI/components/index.ts +0 -1
- package/components/AGUI/context/index.ts +0 -1
- package/components/AGUI/hooks/index.ts +0 -1
- package/components/AGUI/hooks/use-copilot-chat-suggestions.tsx +0 -122
- package/components/AGUI/hooks/use-copy-to-clipboard.tsx +0 -29
- package/components/AGUI/hooks/use-dark-mode.ts +0 -10
- package/components/AGUI/hooks/use-push-to-talk.tsx +0 -166
- package/components/AGUI/index.tsx +0 -4
- package/components/AGUI/lib/utils.test.ts +0 -7
- package/components/AGUI/lib/utils.ts +0 -27
- package/components/AGUI/styles.css +0 -0
- package/components/AGUI/types/css.ts +0 -0
- package/components/AGUI/types/index.ts +0 -1
- package/components/AGUI/types/suggestions.ts +0 -6
- package/components/Markdown/Markdown/AnimationNode.tsx +0 -89
- package/components/Markdown/Markdown/Markdown.tsx +0 -61
- package/components/Markdown/Markdown/core/Parser.ts +0 -52
- package/components/Markdown/Markdown/core/Renderer.ts +0 -121
- package/components/Markdown/Markdown/core/index.ts +0 -4
- package/components/Markdown/Markdown/hooks/index.ts +0 -4
- package/components/Markdown/Markdown/hooks/useAnimation.tsx +0 -27
- package/components/Markdown/Markdown/hooks/useStreaming.ts +0 -503
- package/components/Markdown/Markdown/index.tsx +0 -198
- package/components/Markdown/Markdown/interface.ts +0 -217
- package/components/Markdown/demo/typing.tsx +0 -82
- package/components/Markdown/index.ts +0 -1
- package/components/Markdown/plugins/latex/index.ts +0 -109
- package/components/Markdown/plugins/type.ts +0 -71
- package/lib/AGUI/components/HelpModal/icons.d.ts +0 -5
- package/lib/AGUI/components/HelpModal/icons.js +0 -84
- package/lib/AGUI/components/HelpModal/index.d.ts +0 -1
- package/lib/AGUI/components/HelpModal/index.js +0 -1
- package/lib/AGUI/components/HelpModal/modal.d.ts +0 -1
- package/lib/AGUI/components/HelpModal/modal.js +0 -104
- package/lib/AGUI/components/chat/Button.d.ts +0 -2
- package/lib/AGUI/components/chat/Button.js +0 -27
- package/lib/AGUI/components/chat/Chat.d.ts +0 -242
- package/lib/AGUI/components/chat/Chat.js +0 -634
- package/lib/AGUI/components/chat/ChatContext.d.ts +0 -142
- package/lib/AGUI/components/chat/ChatContext.js +0 -75
- package/lib/AGUI/components/chat/CodeBlock.d.ts +0 -12
- package/lib/AGUI/components/chat/CodeBlock.js +0 -398
- package/lib/AGUI/components/chat/Header.d.ts +0 -2
- package/lib/AGUI/components/chat/Header.js +0 -28
- package/lib/AGUI/components/chat/Icons.d.ts +0 -16
- package/lib/AGUI/components/chat/Icons.js +0 -247
- package/lib/AGUI/components/chat/ImageUploadQueue.d.ts +0 -11
- package/lib/AGUI/components/chat/ImageUploadQueue.js +0 -65
- package/lib/AGUI/components/chat/Input.d.ts +0 -2
- package/lib/AGUI/components/chat/Input.js +0 -30
- package/lib/AGUI/components/chat/Markdown.d.ts +0 -7
- package/lib/AGUI/components/chat/Markdown.js +0 -187
- package/lib/AGUI/components/chat/Messages.d.ts +0 -7
- package/lib/AGUI/components/chat/Messages.js +0 -242
- package/lib/AGUI/components/chat/Modal.d.ts +0 -43
- package/lib/AGUI/components/chat/Modal.js +0 -113
- package/lib/AGUI/components/chat/Popup.d.ts +0 -49
- package/lib/AGUI/components/chat/Popup.js +0 -64
- package/lib/AGUI/components/chat/PoweredByTag.d.ts +0 -3
- package/lib/AGUI/components/chat/PoweredByTag.js +0 -27
- package/lib/AGUI/components/chat/Sidebar.d.ts +0 -2
- package/lib/AGUI/components/chat/Sidebar.js +0 -85
- package/lib/AGUI/components/chat/Suggestion.d.ts +0 -16
- package/lib/AGUI/components/chat/Suggestion.js +0 -146
- package/lib/AGUI/components/chat/Suggestions.d.ts +0 -2
- package/lib/AGUI/components/chat/Suggestions.js +0 -21
- package/lib/AGUI/components/chat/Textarea.d.ts +0 -11
- package/lib/AGUI/components/chat/Textarea.js +0 -60
- package/lib/AGUI/components/chat/Window.d.ts +0 -2
- package/lib/AGUI/components/chat/Window.js +0 -115
- package/lib/AGUI/components/chat/index.d.ts +0 -11
- package/lib/AGUI/components/chat/index.js +0 -11
- package/lib/AGUI/components/chat/messages/AssistantMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/AssistantMessage.js +0 -56
- package/lib/AGUI/components/chat/messages/RenderActionExecutionMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/RenderActionExecutionMessage.js +0 -114
- package/lib/AGUI/components/chat/messages/RenderAgentStateMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/RenderAgentStateMessage.js +0 -97
- package/lib/AGUI/components/chat/messages/RenderImageMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/RenderImageMessage.js +0 -61
- package/lib/AGUI/components/chat/messages/RenderResultMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/RenderResultMessage.js +0 -27
- package/lib/AGUI/components/chat/messages/RenderTextMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/RenderTextMessage.js +0 -47
- package/lib/AGUI/components/chat/messages/UserMessage.d.ts +0 -2
- package/lib/AGUI/components/chat/messages/UserMessage.js +0 -9
- package/lib/AGUI/components/chat/props.d.ts +0 -157
- package/lib/AGUI/components/index.d.ts +0 -1
- package/lib/AGUI/components/index.js +0 -1
- package/lib/AGUI/context/index.d.ts +0 -1
- package/lib/AGUI/context/index.js +0 -1
- package/lib/AGUI/hooks/index.d.ts +0 -1
- package/lib/AGUI/hooks/index.js +0 -1
- package/lib/AGUI/hooks/use-copilot-chat-suggestions.d.ts +0 -87
- package/lib/AGUI/hooks/use-copilot-chat-suggestions.js +0 -95
- package/lib/AGUI/hooks/use-copy-to-clipboard.d.ts +0 -7
- package/lib/AGUI/hooks/use-copy-to-clipboard.js +0 -34
- package/lib/AGUI/hooks/use-dark-mode.d.ts +0 -1
- package/lib/AGUI/hooks/use-dark-mode.js +0 -4
- package/lib/AGUI/hooks/use-push-to-talk.d.ts +0 -16
- package/lib/AGUI/hooks/use-push-to-talk.js +0 -260
- package/lib/AGUI/index.d.ts +0 -4
- package/lib/AGUI/index.js +0 -4
- package/lib/AGUI/lib/utils.d.ts +0 -2
- package/lib/AGUI/lib/utils.js +0 -52
- package/lib/AGUI/styles.css +0 -0
- package/lib/AGUI/types/css.d.ts +0 -0
- package/lib/AGUI/types/css.js +0 -0
- package/lib/AGUI/types/index.d.ts +0 -1
- package/lib/AGUI/types/index.js +0 -1
- package/lib/AGUI/types/suggestions.d.ts +0 -6
- package/lib/AGUI/types/suggestions.js +0 -1
- package/lib/Markdown/Markdown/AnimationNode.d.ts +0 -20
- package/lib/Markdown/Markdown/AnimationNode.js +0 -84
- package/lib/Markdown/Markdown/Markdown.d.ts +0 -4
- package/lib/Markdown/Markdown/Markdown.js +0 -58
- package/lib/Markdown/Markdown/core/Parser.d.ts +0 -16
- package/lib/Markdown/Markdown/core/Parser.js +0 -65
- package/lib/Markdown/Markdown/core/Renderer.d.ts +0 -24
- package/lib/Markdown/Markdown/core/Renderer.js +0 -137
- package/lib/Markdown/Markdown/core/index.d.ts +0 -3
- package/lib/Markdown/Markdown/core/index.js +0 -3
- package/lib/Markdown/Markdown/hooks/index.d.ts +0 -3
- package/lib/Markdown/Markdown/hooks/index.js +0 -3
- package/lib/Markdown/Markdown/hooks/useAnimation.d.ts +0 -4
- package/lib/Markdown/Markdown/hooks/useCitationsData.d.ts +0 -7
- package/lib/Markdown/Markdown/hooks/useStreaming.d.ts +0 -3
- package/lib/Markdown/Markdown/hooks/useStreaming.js +0 -521
- package/lib/Markdown/Markdown/hooks/useTyping.d.ts +0 -4
- package/lib/Markdown/Markdown/index.d.ts +0 -84
- package/lib/Markdown/Markdown/index.js +0 -161
- package/lib/Markdown/Markdown/interface.d.ts +0 -203
- package/lib/Markdown/Markdown/interface.js +0 -1
- package/lib/Markdown/Markdown/style.js +0 -32
- package/lib/Markdown/plugins/latex/index.d.ts +0 -9
- package/lib/Markdown/plugins/latex/index.js +0 -97
- package/lib/Markdown/plugins/type.d.ts +0 -60
- package/lib/Markdown/plugins/type.js +0 -1
- /package/components/Markdown/{Markdown/defaultComponents/DisabledImage.tsx → core/components/DisableImage.tsx} +0 -0
- /package/components/Markdown/{Markdown/defaultComponents → core/components}/Media.tsx +0 -0
- /package/components/Markdown/{plugins → core/plugins}/cursor/Dot.tsx +0 -0
- /package/lib/Markdown/{Markdown/defaultComponents → core/components}/CodeBlock.d.ts +0 -0
- /package/lib/Markdown/{Markdown/defaultComponents → core/components}/CodeBlock.js +0 -0
- /package/lib/Markdown/{Markdown/defaultComponents/DisabledImage.d.ts → core/components/DisableImage.d.ts} +0 -0
- /package/lib/Markdown/{Markdown/defaultComponents/DisabledImage.js → core/components/DisableImage.js} +0 -0
- /package/lib/Markdown/{Markdown/defaultComponents → core/components}/Media.d.ts +0 -0
- /package/lib/Markdown/{Markdown/defaultComponents → core/components}/Media.js +0 -0
- /package/lib/Markdown/{plugins → core/plugins}/citations/CitationComponent.d.ts +0 -0
- /package/lib/Markdown/{plugins → core/plugins}/cursor/Dot.d.ts +0 -0
- /package/lib/Markdown/{plugins → core/plugins}/cursor/Underline.d.ts +0 -0
- /package/lib/Markdown/{plugins → core/plugins}/cursor/index.d.ts +0 -0
- /package/lib/Markdown/{Markdown/style.d.ts → styles.d.ts} +0 -0
- /package/lib/{AGUI/components/chat/props.js → Markdown/types.js} +0 -0
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import type { DOMNode } from 'html-react-parser';
|
|
2
|
-
import React, { useEffect, useRef, useState } from 'react';
|
|
3
|
-
import { HTMLTag } from './hooks/useAnimation';
|
|
4
|
-
import { AnimationConfig } from './interface';
|
|
5
|
-
import { useGlobalContext, useProviderContext } from '@agentscope-ai/chat';
|
|
6
|
-
|
|
7
|
-
export interface AnimationNodeProps extends React.HTMLAttributes<HTMLElement> {
|
|
8
|
-
children?: React.ReactNode | React.ReactNode[];
|
|
9
|
-
nodeTag: HTMLTag;
|
|
10
|
-
animationConfig?: AnimationConfig;
|
|
11
|
-
streamStatus?: 'loading' | 'done';
|
|
12
|
-
domNode?: DOMNode;
|
|
13
|
-
[key: string]: unknown;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface AnimationTextProps {
|
|
17
|
-
text: string;
|
|
18
|
-
animationConfig?: AnimationConfig;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const AnimationText = React.memo<AnimationTextProps>((props) => {
|
|
22
|
-
const { text, animationConfig } = props;
|
|
23
|
-
const { fadeDuration = 200, easing = 'ease-in-out' } = animationConfig || {};
|
|
24
|
-
const [chunks, setChunks] = useState<string[]>([]);
|
|
25
|
-
const prevTextRef = useRef('');
|
|
26
|
-
const { getPrefixCls, } = useProviderContext();
|
|
27
|
-
const prefixCls = getPrefixCls('markdown');
|
|
28
|
-
|
|
29
|
-
useEffect(() => {
|
|
30
|
-
if (text === prevTextRef.current) return;
|
|
31
|
-
|
|
32
|
-
if (!(prevTextRef.current && text.indexOf(prevTextRef.current) === 0)) {
|
|
33
|
-
setChunks([text]);
|
|
34
|
-
prevTextRef.current = text;
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const newText = text.slice(prevTextRef.current.length);
|
|
39
|
-
if (!newText) return;
|
|
40
|
-
|
|
41
|
-
setChunks((prev) => [...prev, newText]);
|
|
42
|
-
prevTextRef.current = text;
|
|
43
|
-
}, [text]);
|
|
44
|
-
|
|
45
|
-
return (
|
|
46
|
-
<>
|
|
47
|
-
{chunks.map((text, index) => {
|
|
48
|
-
return (
|
|
49
|
-
<span
|
|
50
|
-
style={{
|
|
51
|
-
animation: `${prefixCls}-fadeIn ${fadeDuration}ms ${easing} forwards`,
|
|
52
|
-
}}
|
|
53
|
-
key={`${index}-${text}`}
|
|
54
|
-
>
|
|
55
|
-
{text}
|
|
56
|
-
</span>
|
|
57
|
-
);
|
|
58
|
-
})}
|
|
59
|
-
</>
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const AnimationNode: React.FC<AnimationNodeProps> = (props) => {
|
|
64
|
-
const { nodeTag, children, animationConfig, domNode, streamStatus, ...restProps } = props;
|
|
65
|
-
|
|
66
|
-
const renderChildren = (): React.ReactNode | React.ReactNode[] => {
|
|
67
|
-
if (!children) return null;
|
|
68
|
-
|
|
69
|
-
if (Array.isArray(children)) {
|
|
70
|
-
return children.map((child, index) =>
|
|
71
|
-
typeof child === 'string' ? (
|
|
72
|
-
<AnimationText key={index} animationConfig={animationConfig} text={child} />
|
|
73
|
-
) : (
|
|
74
|
-
child
|
|
75
|
-
),
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
return typeof children === 'string' ? (
|
|
79
|
-
<AnimationText text={children} animationConfig={animationConfig} />
|
|
80
|
-
) : (
|
|
81
|
-
children
|
|
82
|
-
);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
return React.createElement(nodeTag, restProps, renderChildren());
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export default AnimationNode;
|
|
89
|
-
export { AnimationText };
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import classnames from 'classnames';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { Parser, Renderer } from './core';
|
|
4
|
-
import { useAnimation, useStreaming } from './hooks';
|
|
5
|
-
import { MarkdownProps } from './interface';
|
|
6
|
-
import Style from './style';
|
|
7
|
-
|
|
8
|
-
const Markdown: React.FC<MarkdownProps> = (props) => {
|
|
9
|
-
|
|
10
|
-
const {
|
|
11
|
-
cursor,
|
|
12
|
-
baseFontSize,
|
|
13
|
-
baseLineHeight,
|
|
14
|
-
streaming,
|
|
15
|
-
config,
|
|
16
|
-
components,
|
|
17
|
-
paragraphTag = 'p',
|
|
18
|
-
content,
|
|
19
|
-
children,
|
|
20
|
-
rootClassName,
|
|
21
|
-
className,
|
|
22
|
-
style,
|
|
23
|
-
openLinksInNewTab,
|
|
24
|
-
dompurifyConfig,
|
|
25
|
-
} = props;
|
|
26
|
-
const mergedCls = classnames(rootClassName, className);
|
|
27
|
-
const mergedStyle: React.CSSProperties = {
|
|
28
|
-
fontSize: baseFontSize,
|
|
29
|
-
lineHeight: baseLineHeight,
|
|
30
|
-
...style,
|
|
31
|
-
};
|
|
32
|
-
const displayContent = useStreaming(content || children || '', streaming, cursor);
|
|
33
|
-
const animationComponents = useAnimation(streaming);
|
|
34
|
-
|
|
35
|
-
if (!displayContent) return null;
|
|
36
|
-
|
|
37
|
-
const parser = new Parser({
|
|
38
|
-
markedConfig: config,
|
|
39
|
-
paragraphTag,
|
|
40
|
-
openLinksInNewTab,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const renderComponents = { ...animationComponents, ...(components || {}) };
|
|
44
|
-
const renderer = new Renderer({
|
|
45
|
-
components: renderComponents,
|
|
46
|
-
dompurifyConfig,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const htmlString = parser.parse(displayContent);
|
|
50
|
-
return (
|
|
51
|
-
<>
|
|
52
|
-
<Style />
|
|
53
|
-
<div className={mergedCls} style={mergedStyle}>
|
|
54
|
-
{renderer.render(htmlString)}
|
|
55
|
-
</div>
|
|
56
|
-
</>
|
|
57
|
-
);
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
export default Markdown;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { Marked, Renderer, Tokens } from 'marked';
|
|
2
|
-
import { MarkdownProps } from '../interface';
|
|
3
|
-
|
|
4
|
-
type ParserOptions = {
|
|
5
|
-
markedConfig?: MarkdownProps['config'];
|
|
6
|
-
paragraphTag?: string;
|
|
7
|
-
openLinksInNewTab?: boolean;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
class Parser {
|
|
11
|
-
options: ParserOptions;
|
|
12
|
-
markdownInstance: Marked;
|
|
13
|
-
|
|
14
|
-
constructor(options: ParserOptions = {}) {
|
|
15
|
-
const { markedConfig = {}, openLinksInNewTab = true } = options;
|
|
16
|
-
this.options = options;
|
|
17
|
-
this.markdownInstance = new Marked(markedConfig);
|
|
18
|
-
this.configureRenderer(openLinksInNewTab);
|
|
19
|
-
this.configureParagraph();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private configureRenderer(openLinksInNewTab?: boolean) {
|
|
23
|
-
if (!openLinksInNewTab) return;
|
|
24
|
-
|
|
25
|
-
const renderer = {
|
|
26
|
-
link(this: Renderer, { href, title, tokens }: Tokens.Link) {
|
|
27
|
-
const text = this.parser.parseInline(tokens);
|
|
28
|
-
const titleAttr = title ? ` title="${title}"` : '';
|
|
29
|
-
return `<a href="${href}"${titleAttr} target="_blank" rel="noopener noreferrer">${text}</a>`;
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
this.markdownInstance.use({ renderer });
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public configureParagraph() {
|
|
36
|
-
const { paragraphTag } = this.options;
|
|
37
|
-
if (!paragraphTag) return;
|
|
38
|
-
|
|
39
|
-
const renderer = {
|
|
40
|
-
paragraph(this: Renderer, { tokens }: Tokens.Paragraph) {
|
|
41
|
-
return `<${paragraphTag}>${this.parser.parseInline(tokens)}</${paragraphTag}>\n`;
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
this.markdownInstance.use({ renderer });
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public parse(content: string) {
|
|
48
|
-
return this.markdownInstance.parse(content) as string;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export default Parser;
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import type { Config as DOMPurifyConfig } from 'dompurify';
|
|
2
|
-
import DOMPurify from 'dompurify';
|
|
3
|
-
import parseHtml, { DOMNode, domToReact, Element } from 'html-react-parser';
|
|
4
|
-
import React, { ReactNode } from 'react';
|
|
5
|
-
import type { ComponentProps, MarkdownProps } from '../interface';
|
|
6
|
-
|
|
7
|
-
interface RendererOptions {
|
|
8
|
-
components?: MarkdownProps['components'];
|
|
9
|
-
dompurifyConfig?: DOMPurifyConfig;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
class Renderer {
|
|
13
|
-
private readonly options: RendererOptions;
|
|
14
|
-
|
|
15
|
-
constructor(options: RendererOptions) {
|
|
16
|
-
this.options = options;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Detect unclosed tags using regular expressions
|
|
21
|
-
*/
|
|
22
|
-
private detectUnclosedTags(htmlString: string): Set<string> {
|
|
23
|
-
const unclosedTags = new Set<string>();
|
|
24
|
-
const stack: string[] = [];
|
|
25
|
-
const tagRegex = /<\/?([a-zA-Z][a-zA-Z0-9-]*)(?:\s[^>]*)?>/g;
|
|
26
|
-
|
|
27
|
-
let match = tagRegex.exec(htmlString);
|
|
28
|
-
while (match !== null) {
|
|
29
|
-
const [fullMatch, tagName] = match;
|
|
30
|
-
const isClosing = fullMatch.startsWith('</');
|
|
31
|
-
const isSelfClosing = fullMatch.endsWith('/>');
|
|
32
|
-
|
|
33
|
-
if (this.options.components?.[tagName.toLowerCase()]) {
|
|
34
|
-
if (isClosing) {
|
|
35
|
-
// Found closing tag, pop from stack
|
|
36
|
-
const lastIndex = stack.lastIndexOf(tagName.toLowerCase());
|
|
37
|
-
if (lastIndex !== -1) {
|
|
38
|
-
stack.splice(lastIndex, 1);
|
|
39
|
-
}
|
|
40
|
-
} else if (!isSelfClosing) {
|
|
41
|
-
// Found opening tag, push to stack
|
|
42
|
-
stack.push(tagName.toLowerCase());
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
match = tagRegex.exec(htmlString);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Remaining tags in stack are unclosed
|
|
49
|
-
stack.forEach((tag) => {
|
|
50
|
-
unclosedTags.add(tag);
|
|
51
|
-
});
|
|
52
|
-
return unclosedTags;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Configure DOMPurify to preserve components and target attributes, filter everything else
|
|
57
|
-
*/
|
|
58
|
-
private configureDOMPurify(): DOMPurifyConfig {
|
|
59
|
-
const customComponents = Object.keys(this.options.components || {});
|
|
60
|
-
const userConfig = this.options.dompurifyConfig || {};
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
...userConfig,
|
|
64
|
-
ADD_TAGS: [...new Set([...customComponents, ...(userConfig.ALLOWED_TAGS || [])])],
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private replaceElement(unclosedTags?: Set<string>) {
|
|
69
|
-
return (domNode: DOMNode) => {
|
|
70
|
-
if (!('name' in domNode)) return;
|
|
71
|
-
|
|
72
|
-
const { name, attribs, children } = domNode as Element;
|
|
73
|
-
const renderElement = this.options.components?.[name];
|
|
74
|
-
|
|
75
|
-
if (renderElement) {
|
|
76
|
-
const streamStatus = unclosedTags?.has(name) ? 'loading' : 'done';
|
|
77
|
-
const props: ComponentProps = {
|
|
78
|
-
domNode,
|
|
79
|
-
streamStatus,
|
|
80
|
-
...attribs,
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// Handle class and className merging
|
|
84
|
-
const classes = [props.className, props.classname, props.class]
|
|
85
|
-
.filter(Boolean)
|
|
86
|
-
.join(' ')
|
|
87
|
-
.trim();
|
|
88
|
-
props.className = classes || '';
|
|
89
|
-
|
|
90
|
-
if (children) {
|
|
91
|
-
props.children = this.processChildren(children as DOMNode[], unclosedTags);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return React.createElement(renderElement, props);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private processChildren(children: DOMNode[], unclosedTags?: Set<string>): ReactNode {
|
|
100
|
-
return domToReact(children as DOMNode[], {
|
|
101
|
-
replace: this.replaceElement(unclosedTags),
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public processHtml(htmlString: string): React.ReactNode {
|
|
106
|
-
const unclosedTags = this.detectUnclosedTags(htmlString);
|
|
107
|
-
// Use DOMPurify to clean HTML while preserving custom components and target attributes
|
|
108
|
-
// const purifyConfig = this.configureDOMPurify();
|
|
109
|
-
// const cleanHtml = DOMPurify.sanitize(htmlString, purifyConfig);
|
|
110
|
-
|
|
111
|
-
return parseHtml(htmlString, {
|
|
112
|
-
replace: this.replaceElement(unclosedTags),
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public render(html: string): ReactNode | null {
|
|
117
|
-
return this.processHtml(html);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export default Renderer;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import React, { useMemo } from 'react';
|
|
2
|
-
import AnimationNode from '../AnimationNode';
|
|
3
|
-
import { MarkdownProps } from '../interface';
|
|
4
|
-
|
|
5
|
-
export type HTMLTag = 'div' | 'p' | 'li' | 'h1' | 'h2' | 'h3' | 'h4' | 'strong';
|
|
6
|
-
type AnimationComponents = Record<HTMLTag, React.FC<React.ComponentProps<HTMLTag>>>;
|
|
7
|
-
|
|
8
|
-
const ANIMATION_TAGS: HTMLTag[] = ['div', 'p', 'li', 'h1', 'h2', 'h3', 'h4', 'strong'];
|
|
9
|
-
|
|
10
|
-
const useAnimation = (streaming: MarkdownProps['streaming']) => {
|
|
11
|
-
const { enableAnimation = false, animationConfig } = streaming || {};
|
|
12
|
-
|
|
13
|
-
const animatedComponents = useMemo(() => {
|
|
14
|
-
if (!enableAnimation) return {};
|
|
15
|
-
|
|
16
|
-
return ANIMATION_TAGS.reduce<AnimationComponents>((acc, tag) => {
|
|
17
|
-
const AnimatedComponent: React.FC<React.ComponentProps<typeof tag>> = React.memo((props) => (
|
|
18
|
-
<AnimationNode nodeTag={tag} animationConfig={animationConfig} {...props} />
|
|
19
|
-
));
|
|
20
|
-
return { ...acc, [tag]: AnimatedComponent };
|
|
21
|
-
}, {} as AnimationComponents);
|
|
22
|
-
}, [enableAnimation, JSON.stringify(animationConfig)]);
|
|
23
|
-
|
|
24
|
-
return animatedComponents;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export default useAnimation;
|