devnonla-ui 0.17.0 → 0.17.2
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/package.json +1 -1
- package/src/chat/AgentChatbox.tsx +23 -17
- package/src/chat/AgentPanel.tsx +23 -17
- package/src/chat/index.ts +1 -0
- package/src/chat/message-ui/ChatAgentMessage.tsx +5 -2
- package/src/chat/message-ui/ChatMarkdown.tsx +6 -3
- package/src/markdown-editor/MarkdownEditor.tsx +17 -4
- package/src/markdown-editor/MarkdownPreview.tsx +7 -5
- package/src/markdown-editor/MarkdownViewer.tsx +65 -6
- package/src/markdown-editor/index.ts +2 -2
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ReactNode, useEffect, useLayoutEffect } from "react";
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
|
+
import { type MarkdownComponents, MarkdownComponentsProvider } from "../markdown-editor/MarkdownViewer";
|
|
3
4
|
import { OverlayScroll, type OverlayScrollVisibility } from "../scroll/OverlayScroll";
|
|
4
5
|
import { AgentMessageList, activityStatus, useStickToBottom } from "./chatMessageList";
|
|
5
6
|
import type { AgentMessage, AgentToolHook } from "./common/types";
|
|
@@ -30,6 +31,8 @@ export type AgentChatboxProps = {
|
|
|
30
31
|
placeholder?: string;
|
|
31
32
|
toolbar?: ReactNode;
|
|
32
33
|
toolUis?: AgentToolUI[];
|
|
34
|
+
/** Fence language → component for assistant markdown (`step`, `subagent`, `mindmap`). */
|
|
35
|
+
components?: MarkdownComponents;
|
|
33
36
|
toolHooks?: AgentToolHook[];
|
|
34
37
|
accessory?: ReactNode;
|
|
35
38
|
emptyState?: ReactNode;
|
|
@@ -51,6 +54,7 @@ export function AgentChatbox({
|
|
|
51
54
|
placeholder = "Message…",
|
|
52
55
|
toolbar,
|
|
53
56
|
toolUis,
|
|
57
|
+
components,
|
|
54
58
|
toolHooks,
|
|
55
59
|
accessory,
|
|
56
60
|
emptyState,
|
|
@@ -88,25 +92,27 @@ export function AgentChatbox({
|
|
|
88
92
|
};
|
|
89
93
|
|
|
90
94
|
return (
|
|
91
|
-
<
|
|
92
|
-
<
|
|
93
|
-
<
|
|
94
|
-
{messages.length === 0 ?
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
<MarkdownComponentsProvider components={components}>
|
|
96
|
+
<div className={cn("nonla-agent-chatbox relative flex h-full min-h-0 flex-col overflow-hidden bg-transparent", className)}>
|
|
97
|
+
<OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
|
|
98
|
+
<div data-chat-scroll-content className={`max-w-200 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col" : "px-2.5 pt-4 pb-20"}`}>
|
|
99
|
+
{messages.length === 0 ? (
|
|
100
|
+
(emptyState ?? (
|
|
101
|
+
<ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
|
|
102
|
+
))
|
|
103
|
+
) : (
|
|
104
|
+
<AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
|
|
105
|
+
)}
|
|
106
|
+
</div>
|
|
107
|
+
</OverlayScroll>
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
109
|
+
<div className="relative shrink-0">
|
|
110
|
+
{accessory}
|
|
111
|
+
<div className="mx-auto w-full max-w-200">
|
|
112
|
+
<ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} toolbar={toolbar} enableTypeToFocus />
|
|
113
|
+
</div>
|
|
108
114
|
</div>
|
|
109
115
|
</div>
|
|
110
|
-
</
|
|
116
|
+
</MarkdownComponentsProvider>
|
|
111
117
|
);
|
|
112
118
|
}
|
package/src/chat/AgentPanel.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ReactNode, useEffect, useLayoutEffect, useState } from "react";
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
|
+
import { type MarkdownComponents, MarkdownComponentsProvider } from "../markdown-editor/MarkdownViewer";
|
|
3
4
|
import { OverlayScroll, type OverlayScrollVisibility } from "../scroll/OverlayScroll";
|
|
4
5
|
import { AgentMessageList, activityStatus, useStickToBottom } from "./chatMessageList";
|
|
5
6
|
import type { AgentMessage, AgentPanelEndpoint, AgentToolHook } from "./common/types";
|
|
@@ -27,6 +28,8 @@ export type AgentPanelProps = {
|
|
|
27
28
|
toolbar?: ReactNode;
|
|
28
29
|
/** Extra tool cards keyed by stream `toolName`. Checked before builtins. */
|
|
29
30
|
toolUis?: AgentToolUI[];
|
|
31
|
+
/** Fence language → component for assistant markdown (`step`, `subagent`, `mindmap`). */
|
|
32
|
+
components?: MarkdownComponents;
|
|
30
33
|
/** Tool lifecycle hooks, matched by `name` like `toolUis`. Omit `name` to hear every tool. */
|
|
31
34
|
toolHooks?: AgentToolHook[];
|
|
32
35
|
accessory?: ReactNode;
|
|
@@ -51,6 +54,7 @@ export function AgentPanel({
|
|
|
51
54
|
initialMessages,
|
|
52
55
|
toolbar,
|
|
53
56
|
toolUis,
|
|
57
|
+
components,
|
|
54
58
|
toolHooks,
|
|
55
59
|
accessory,
|
|
56
60
|
emptyState,
|
|
@@ -95,25 +99,27 @@ export function AgentPanel({
|
|
|
95
99
|
};
|
|
96
100
|
|
|
97
101
|
return (
|
|
98
|
-
<
|
|
99
|
-
<
|
|
100
|
-
<
|
|
101
|
-
{messages.length === 0 ?
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
<MarkdownComponentsProvider components={components}>
|
|
103
|
+
<div className={cn("nonla-agent-panel relative flex h-full min-h-0 flex-col overflow-hidden bg-transparent", className)}>
|
|
104
|
+
<OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
|
|
105
|
+
<div data-chat-scroll-content className={`max-w-200 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col" : "px-2.5 pt-4 pb-28"}`}>
|
|
106
|
+
{messages.length === 0 ? (
|
|
107
|
+
(emptyState ?? (
|
|
108
|
+
<ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
|
|
109
|
+
))
|
|
110
|
+
) : (
|
|
111
|
+
<AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
|
|
112
|
+
)}
|
|
113
|
+
</div>
|
|
114
|
+
</OverlayScroll>
|
|
110
115
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
116
|
+
<div className="relative shrink-0">
|
|
117
|
+
{accessory}
|
|
118
|
+
<div className="mx-auto w-full max-w-200">
|
|
119
|
+
<ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} onClear={handleNewChat} toolbar={toolbar} focusSignal={String(epoch)} enableTypeToFocus />
|
|
120
|
+
</div>
|
|
115
121
|
</div>
|
|
116
122
|
</div>
|
|
117
|
-
</
|
|
123
|
+
</MarkdownComponentsProvider>
|
|
118
124
|
);
|
|
119
125
|
}
|
package/src/chat/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
2
|
import { cn } from "../../lib/cn";
|
|
3
|
+
import type { MarkdownComponents } from "../../markdown-editor/MarkdownViewer";
|
|
3
4
|
import { chatRowBoxClass, chatRowClass } from "../chatRhythm";
|
|
4
5
|
import { ChatMarkdown } from "./ChatMarkdown";
|
|
5
6
|
import { ChatThinking } from "./ChatThinking";
|
|
@@ -8,6 +9,8 @@ export type ChatAgentMessageProps = {
|
|
|
8
9
|
/** Markdown body. Rendered with `MarkdownViewer`. */
|
|
9
10
|
content?: string;
|
|
10
11
|
streaming?: boolean;
|
|
12
|
+
/** Fence language → component for `content`. */
|
|
13
|
+
components?: MarkdownComponents;
|
|
11
14
|
/** Extra row content. Replaces `content` when set. */
|
|
12
15
|
children?: ReactNode;
|
|
13
16
|
thinking?: string;
|
|
@@ -16,8 +19,8 @@ export type ChatAgentMessageProps = {
|
|
|
16
19
|
className?: string;
|
|
17
20
|
};
|
|
18
21
|
|
|
19
|
-
export function ChatAgentMessage({ content, streaming = false, children, thinking, thinkingDuration, thinkingStreaming, className }: ChatAgentMessageProps) {
|
|
20
|
-
const body = children ?? (content ? <ChatMarkdown content={content} streaming={streaming} /> : null);
|
|
22
|
+
export function ChatAgentMessage({ content, streaming = false, components, children, thinking, thinkingDuration, thinkingStreaming, className }: ChatAgentMessageProps) {
|
|
23
|
+
const body = children ?? (content ? <ChatMarkdown content={content} streaming={streaming} components={components} /> : null);
|
|
21
24
|
|
|
22
25
|
return (
|
|
23
26
|
<div className={cn("nonla-chat-agent", chatRowClass, className)}>
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { CSSProperties } from "react";
|
|
2
|
-
import { MarkdownViewer } from "../../markdown-editor/MarkdownViewer";
|
|
2
|
+
import { type MarkdownComponents, MarkdownViewer, useMarkdownComponents } from "../../markdown-editor/MarkdownViewer";
|
|
3
3
|
|
|
4
4
|
export type ChatMarkdownProps = {
|
|
5
5
|
content: string;
|
|
6
6
|
streaming?: boolean;
|
|
7
|
+
/** Fence language → component, such as `step`, `subagent`, or `mindmap`. */
|
|
8
|
+
components?: MarkdownComponents;
|
|
7
9
|
};
|
|
8
10
|
|
|
9
11
|
/** Reply headings are `text-lg` at the strong weight. Body stays `text-md`. Strong is 500. Tables stay `text-base`. */
|
|
@@ -40,6 +42,7 @@ const chatEdgeClass = [
|
|
|
40
42
|
].join(" ");
|
|
41
43
|
|
|
42
44
|
/** Agent markdown. Headings are `text-lg` at the strong weight. */
|
|
43
|
-
export function ChatMarkdown({ content, streaming = false }: ChatMarkdownProps) {
|
|
44
|
-
|
|
45
|
+
export function ChatMarkdown({ content, streaming = false, components }: ChatMarkdownProps) {
|
|
46
|
+
const inherited = useMarkdownComponents();
|
|
47
|
+
return <MarkdownViewer value={content} streaming={streaming} className={chatEdgeClass} style={chatHeadingScale} components={components ?? inherited} />;
|
|
45
48
|
}
|
|
@@ -2,11 +2,12 @@ import { type CSSProperties, type MouseEvent, type ReactNode, useCallback, useMe
|
|
|
2
2
|
import { Icon } from "../icon/Icon";
|
|
3
3
|
import { cn } from "../lib/cn";
|
|
4
4
|
import { Segmented } from "../segmented/Segmented";
|
|
5
|
-
import { appendTextBlock, cleanupDoc, joinMarkdownDoc, type MarkdownDoc, parseMarkdownDoc, readInlineEdit, splitListItem } from "./blocks";
|
|
5
|
+
import { appendTextBlock, cleanupDoc, joinMarkdownDoc, type MarkdownBlock, type MarkdownDoc, parseMarkdownDoc, readInlineEdit, splitListItem } from "./blocks";
|
|
6
6
|
import { diffMarkdown } from "./diff";
|
|
7
7
|
import { MarkdownDiff, type MarkdownDiffLayout } from "./MarkdownDiff";
|
|
8
8
|
import { MarkdownPreview } from "./MarkdownPreview";
|
|
9
9
|
import { MarkdownSource } from "./MarkdownSource";
|
|
10
|
+
import { isCustomFence, type MarkdownComponents } from "./MarkdownViewer";
|
|
10
11
|
|
|
11
12
|
export type MarkdownEditorMode = "preview" | "edit" | "diff";
|
|
12
13
|
export type { MarkdownDiffLayout };
|
|
@@ -32,8 +33,18 @@ export type MarkdownEditorProps = {
|
|
|
32
33
|
modifiedLabel?: ReactNode;
|
|
33
34
|
className?: string;
|
|
34
35
|
style?: CSSProperties;
|
|
36
|
+
/**
|
|
37
|
+
* Components for fenced blocks, keyed by language (`step`, `subagent`, `mindmap`).
|
|
38
|
+
* Same map as `MarkdownViewer`. Those fences stay view only, like mermaid.
|
|
39
|
+
*/
|
|
40
|
+
components?: MarkdownComponents;
|
|
35
41
|
};
|
|
36
42
|
|
|
43
|
+
function viewOnlyBlock(block: MarkdownBlock | undefined, components?: MarkdownComponents): boolean {
|
|
44
|
+
if (!block || block.kind === "mermaid") return true;
|
|
45
|
+
return block.kind === "code" && isCustomFence(block.body, components);
|
|
46
|
+
}
|
|
47
|
+
|
|
37
48
|
type EditSession = {
|
|
38
49
|
index: number;
|
|
39
50
|
doc: MarkdownDoc;
|
|
@@ -60,7 +71,7 @@ function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v
|
|
|
60
71
|
* Markdown editor in the style of Cursor's document editor.
|
|
61
72
|
*
|
|
62
73
|
* Preview renders the document. Click a heading, list item, or paragraph to edit that text in place.
|
|
63
|
-
* Mermaid fences render
|
|
74
|
+
* Mermaid fences and registered fence components render in place and stay view only.
|
|
64
75
|
* Edit is the full-file text buffer. Diff compares `original` and `value` like Monaco.
|
|
65
76
|
* `readOnly` locks editing and hides the mode switcher.
|
|
66
77
|
* A finished document with no editor chrome uses `MarkdownViewer`.
|
|
@@ -83,6 +94,7 @@ export function MarkdownEditor({
|
|
|
83
94
|
modifiedLabel = "Modified",
|
|
84
95
|
className,
|
|
85
96
|
style,
|
|
97
|
+
components,
|
|
86
98
|
}: MarkdownEditorProps) {
|
|
87
99
|
const [value, setValue] = useControllable(valueProp, defaultValue, onChange);
|
|
88
100
|
const [mode, setMode] = useControllable(modeProp, defaultMode, onModeChange);
|
|
@@ -129,7 +141,7 @@ export function MarkdownEditor({
|
|
|
129
141
|
if (readOnly || mode !== "preview") return false;
|
|
130
142
|
const doc = currentDoc();
|
|
131
143
|
const block = doc.blocks[index];
|
|
132
|
-
if (
|
|
144
|
+
if (viewOnlyBlock(block, components)) return false;
|
|
133
145
|
const next: EditSession = { index, doc };
|
|
134
146
|
sessionRef.current = next;
|
|
135
147
|
setSession(next);
|
|
@@ -203,7 +215,7 @@ export function MarkdownEditor({
|
|
|
203
215
|
if (!current || current.index === index) return;
|
|
204
216
|
if (event.target instanceof Element && event.target.closest("a, button, textarea, input")) return;
|
|
205
217
|
const block = current.doc.blocks[index];
|
|
206
|
-
if (
|
|
218
|
+
if (viewOnlyBlock(block, components)) return;
|
|
207
219
|
event.preventDefault();
|
|
208
220
|
if (startEdit(index)) switching.current = true;
|
|
209
221
|
};
|
|
@@ -292,6 +304,7 @@ export function MarkdownEditor({
|
|
|
292
304
|
onEnterList={enterList}
|
|
293
305
|
caret={live?.caret}
|
|
294
306
|
onAppend={append}
|
|
307
|
+
components={components}
|
|
295
308
|
/>
|
|
296
309
|
) : null}
|
|
297
310
|
{mode === "diff" && model ? <MarkdownDiff model={model} layout={layout} originalLabel={originalLabel} modifiedLabel={modifiedLabel} /> : null}
|
|
@@ -2,7 +2,7 @@ import { type MouseEvent, useLayoutEffect, useRef } from "react";
|
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
3
|
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
4
4
|
import { type MarkdownDoc, readInlineEdit, writeInlineEdit } from "./blocks";
|
|
5
|
-
import { BlockBody, headingClass, ListItem, markdownScaleStyle, spaceBefore } from "./MarkdownViewer";
|
|
5
|
+
import { BlockBody, headingClass, isCustomFence, ListItem, type MarkdownComponents, markdownScaleStyle, spaceBefore } from "./MarkdownViewer";
|
|
6
6
|
|
|
7
7
|
function BlockEditor({ value, mono, className, caret = "end", onChange, onBlur, onEnter }: { value: string; mono?: boolean; className?: string; caret?: "start" | "end"; onChange: (value: string) => void; onBlur: () => void; onEnter?: (before: string, after: string) => void }) {
|
|
8
8
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
@@ -83,15 +83,17 @@ export type MarkdownPreviewProps = {
|
|
|
83
83
|
/** Where the caret goes when an edit starts. */
|
|
84
84
|
caret?: "start" | "end";
|
|
85
85
|
onAppend: () => void;
|
|
86
|
+
/** Fence language → component. Same map as `MarkdownViewer`. */
|
|
87
|
+
components?: MarkdownComponents;
|
|
86
88
|
};
|
|
87
89
|
|
|
88
|
-
export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Write markdown…", onStartEdit, onEditMouseDown, onChangeBody, onBlurBlock, onEnterList, caret = "end", onAppend }: MarkdownPreviewProps) {
|
|
90
|
+
export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Write markdown…", onStartEdit, onEditMouseDown, onChangeBody, onBlurBlock, onEnterList, caret = "end", onAppend, components }: MarkdownPreviewProps) {
|
|
89
91
|
return (
|
|
90
92
|
<OverlayScroll className="h-full bg-card">
|
|
91
93
|
<div className="mx-auto flex w-full max-w-3xl flex-col px-8 py-6" style={markdownScaleStyle}>
|
|
92
94
|
{doc.blocks.map((block, index) => {
|
|
93
95
|
const editing = editingIndex === index;
|
|
94
|
-
const locked = block.kind === "mermaid" || readOnly;
|
|
96
|
+
const locked = block.kind === "mermaid" || (block.kind === "code" && isCustomFence(block.body, components)) || readOnly;
|
|
95
97
|
const view = block.kind === "text" ? readInlineEdit(block.body) : null;
|
|
96
98
|
const editor =
|
|
97
99
|
view?.kind === "heading" ? (
|
|
@@ -112,7 +114,7 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
112
114
|
data-md-block={block.kind}
|
|
113
115
|
className={cn("outline-none", gap)}
|
|
114
116
|
>
|
|
115
|
-
<BlockBody block={block} />
|
|
117
|
+
<BlockBody block={block} components={components} />
|
|
116
118
|
</div>
|
|
117
119
|
);
|
|
118
120
|
}
|
|
@@ -137,7 +139,7 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
137
139
|
onStartEdit(index);
|
|
138
140
|
}}
|
|
139
141
|
>
|
|
140
|
-
{editing ? editor : <BlockBody block={block} />}
|
|
142
|
+
{editing ? editor : <BlockBody block={block} components={components} />}
|
|
141
143
|
</div>
|
|
142
144
|
);
|
|
143
145
|
})}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CSSProperties, type ReactNode, useMemo } from "react";
|
|
1
|
+
import { type ComponentType, type CSSProperties, createContext, type ReactNode, useContext, useMemo } from "react";
|
|
2
2
|
import ReactMarkdown, { type Components } from "react-markdown";
|
|
3
3
|
import remarkGfm from "remark-gfm";
|
|
4
4
|
import { Checkbox } from "../checkbox/Checkbox";
|
|
@@ -176,14 +176,62 @@ function fenceClosed(body: string): boolean {
|
|
|
176
176
|
return /^(?:`{3,}|~{3,})$/.test(last);
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
/** Props for a component registered on a fence language. */
|
|
180
|
+
export type MarkdownFenceProps = {
|
|
181
|
+
/** Fence body, without the opening and closing lines. */
|
|
182
|
+
code: string;
|
|
183
|
+
/** Fence language, lowercased. */
|
|
184
|
+
lang: string;
|
|
185
|
+
/** True while the parent viewer is still receiving the document. */
|
|
186
|
+
streaming: boolean;
|
|
187
|
+
/** True once the closing fence line has arrived. */
|
|
188
|
+
closed: boolean;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export type MarkdownFenceComponent = ComponentType<MarkdownFenceProps>;
|
|
192
|
+
|
|
193
|
+
/** Fence language → component. Keys are languages such as `step`, `subagent`, and `mindmap`. */
|
|
194
|
+
export type MarkdownComponents = Record<string, MarkdownFenceComponent>;
|
|
195
|
+
|
|
196
|
+
const MarkdownComponentsContext = createContext<MarkdownComponents | undefined>(undefined);
|
|
197
|
+
|
|
198
|
+
/** Shares a fence component map with every `ChatMarkdown` under this tree. */
|
|
199
|
+
export function MarkdownComponentsProvider({ components, children }: { components?: MarkdownComponents; children: ReactNode }) {
|
|
200
|
+
return <MarkdownComponentsContext.Provider value={components}>{children}</MarkdownComponentsContext.Provider>;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function useMarkdownComponents(): MarkdownComponents | undefined {
|
|
204
|
+
return useContext(MarkdownComponentsContext);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** True when this fence language has a component in the map. */
|
|
208
|
+
export function isCustomFence(body: string, components?: MarkdownComponents): boolean {
|
|
209
|
+
if (!components) return false;
|
|
210
|
+
const lang = readFence(body)?.lang.toLowerCase();
|
|
211
|
+
if (!lang) return false;
|
|
212
|
+
return Object.keys(components).some((key) => key.toLowerCase() === lang);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function fenceComponent(components: MarkdownComponents | undefined, lang: string): MarkdownFenceComponent | undefined {
|
|
216
|
+
if (!components || !lang) return undefined;
|
|
217
|
+
const direct = components[lang];
|
|
218
|
+
if (direct) return direct;
|
|
219
|
+
const lower = lang.toLowerCase();
|
|
220
|
+
for (const key of Object.keys(components)) {
|
|
221
|
+
if (key.toLowerCase() === lower) return components[key];
|
|
222
|
+
}
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
179
226
|
type LiveReactOptions = {
|
|
180
227
|
trustedModules?: Record<string, unknown>;
|
|
181
228
|
sandboxSrc?: string;
|
|
182
229
|
showHeader?: boolean;
|
|
183
230
|
showCode?: boolean;
|
|
231
|
+
components?: MarkdownComponents;
|
|
184
232
|
};
|
|
185
233
|
|
|
186
|
-
export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHeader, showCode }: { block: MarkdownBlock; streaming?: boolean } & LiveReactOptions): ReactNode {
|
|
234
|
+
export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHeader, showCode, components }: { block: MarkdownBlock; streaming?: boolean } & LiveReactOptions): ReactNode {
|
|
187
235
|
if (block.kind === "mermaid") {
|
|
188
236
|
const fence = readFence(block.body);
|
|
189
237
|
const code = fence?.code ?? block.body;
|
|
@@ -193,11 +241,17 @@ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHe
|
|
|
193
241
|
if (block.kind === "code") {
|
|
194
242
|
const fence = readFence(block.body);
|
|
195
243
|
if (!fence) return <CodeBlock code={block.body} className="my-0" />;
|
|
244
|
+
const closed = fenceClosed(block.body);
|
|
196
245
|
// An unclosed fence is still being written, so it never runs.
|
|
197
|
-
if (fence.lang === "live-react" &&
|
|
246
|
+
if (fence.lang === "live-react" && closed) {
|
|
198
247
|
if (sandboxSrc) return <ReactCodeSandbox key={fence.code} code={fence.code} src={sandboxSrc} showHeader={showHeader} showCode={showCode} />;
|
|
199
248
|
if (trustedModules) return <ReactCode code={fence.code} trustedModules={trustedModules} showHeader={showHeader} showCode={showCode} />;
|
|
200
249
|
}
|
|
250
|
+
const Custom = fence.lang === "live-react" ? undefined : fenceComponent(components, fence.lang);
|
|
251
|
+
if (Custom) {
|
|
252
|
+
if (streaming && !closed) return <CodeBlock code={fence.code} language={fence.lang} className="my-0" />;
|
|
253
|
+
return <Custom code={fence.code} lang={fence.lang} streaming={!!streaming} closed={closed} />;
|
|
254
|
+
}
|
|
201
255
|
return <CodeBlock code={fence.code} language={fence.lang || undefined} className="my-0" />;
|
|
202
256
|
}
|
|
203
257
|
const view = readInlineEdit(block.body);
|
|
@@ -213,7 +267,7 @@ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHe
|
|
|
213
267
|
|
|
214
268
|
export type MarkdownViewerProps = {
|
|
215
269
|
value?: string;
|
|
216
|
-
/** An open mermaid fence stays a code block until the stream closes it. */
|
|
270
|
+
/** An open mermaid or custom fence stays a code block until the stream closes it. */
|
|
217
271
|
streaming?: boolean;
|
|
218
272
|
placeholder?: string;
|
|
219
273
|
className?: string;
|
|
@@ -234,10 +288,15 @@ export type MarkdownViewerProps = {
|
|
|
234
288
|
showCode?: boolean;
|
|
235
289
|
/** Overrides scale variables from `markdownScaleStyle`, such as heading size. */
|
|
236
290
|
style?: CSSProperties;
|
|
291
|
+
/**
|
|
292
|
+
* Components for fenced blocks, keyed by language (`step`, `subagent`, `mindmap`).
|
|
293
|
+
* `mermaid` and `live-react` stay built in.
|
|
294
|
+
*/
|
|
295
|
+
components?: MarkdownComponents;
|
|
237
296
|
};
|
|
238
297
|
|
|
239
298
|
/** Read-only markdown. Same blocks as the editor preview, without editing. */
|
|
240
|
-
export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trustedModules, showHeader = true, showCode = true, placeholder, className, style }: MarkdownViewerProps) {
|
|
299
|
+
export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trustedModules, showHeader = true, showCode = true, placeholder, className, style, components }: MarkdownViewerProps) {
|
|
241
300
|
const doc = useMemo(() => parseMarkdownDoc(value), [value]);
|
|
242
301
|
if (doc.blocks.length === 0) {
|
|
243
302
|
return placeholder ? <p className={cn("m-0 text-sm text-quaternary-foreground", className)}>{placeholder}</p> : null;
|
|
@@ -251,7 +310,7 @@ export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trus
|
|
|
251
310
|
data-md-block={block.kind}
|
|
252
311
|
className={cn("outline-none", spaceBefore(block, doc.blocks[index - 1]))}
|
|
253
312
|
>
|
|
254
|
-
<BlockBody block={block} streaming={streaming} trustedModules={trustedModules} sandboxSrc={sandboxSrc} showHeader={showHeader} showCode={showCode} />
|
|
313
|
+
<BlockBody block={block} streaming={streaming} trustedModules={trustedModules} sandboxSrc={sandboxSrc} showHeader={showHeader} showCode={showCode} components={components} />
|
|
255
314
|
</div>
|
|
256
315
|
))}
|
|
257
316
|
</div>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type { MarkdownDiffLayout, MarkdownEditorMode, MarkdownEditorProps } from "./MarkdownEditor";
|
|
2
2
|
export { MarkdownEditor } from "./MarkdownEditor";
|
|
3
|
-
export type { MarkdownViewerProps } from "./MarkdownViewer";
|
|
4
|
-
export { MarkdownViewer } from "./MarkdownViewer";
|
|
3
|
+
export type { MarkdownComponents, MarkdownFenceComponent, MarkdownFenceProps, MarkdownViewerProps } from "./MarkdownViewer";
|
|
4
|
+
export { MarkdownComponentsProvider, MarkdownViewer } from "./MarkdownViewer";
|
|
5
5
|
export type { MermaidBlockProps } from "./MermaidBlock";
|
|
6
6
|
export { MermaidBlock } from "./MermaidBlock";
|
|
7
7
|
export type { ReactCodeProps } from "./ReactCode";
|