Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(control-ui): reuse Markdown preview renderer in identity documents

Josué3d06d030ba0c
diff --git a/control-ui/app/globals.css b/control-ui/app/globals.css
index fe63a6ace..1b029d415 100644
--- a/control-ui/app/globals.css
+++ b/control-ui/app/globals.css
@@ -14337,7 +14337,12 @@ button.cu-gfs-tree__button:focus-visible,
display: flex;
}
-.cu-identity-preview > .cu-gfs-markdown-preview__content {
+.cu-identity-preview__document {
+ width: 100%;
+ min-width: 0;
+}
+
+.cu-identity-preview__document > .cu-gfs-markdown-preview__content {
width: 100%;
max-width: none;
margin: 0;
diff --git a/control-ui/components/MarkdownContent/index.tsx b/control-ui/components/MarkdownContent/index.tsx
index d6606dda9..923696caa 100644
--- a/control-ui/components/MarkdownContent/index.tsx
+++ b/control-ui/components/MarkdownContent/index.tsx
@@ -1,9 +1,13 @@
'use client'
-import { Fragment, useMemo } from 'react'
-import type { ReactNode } from 'react'
-import { parseVanillaMarkdown } from '@lib/vanillaMarkdown'
-import type { MarkdownBlock, MarkdownInlineNode } from '@lib/vanillaMarkdown.types'
+import React from 'react'
+import dynamic from 'next/dynamic'
+import { cn } from '@lib/cn'
+import { safeMarkdownHref } from '@lib/vanillaMarkdown'
+
+type MarkdownPreviewProps = React.ComponentProps<
+ typeof import('@uiw/react-md-editor').default.Markdown
+>
type MarkdownContentProps = {
ariaLabel: string
@@ -12,50 +16,30 @@ type MarkdownContentProps = {
source: string
}
-function renderInlineNodes(nodes: MarkdownInlineNode[]): ReactNode[] {
- return nodes.map(node => {
- if (node.kind === 'text') return <Fragment key={node.id}>{node.value}</Fragment>
- if (node.kind === 'code') return <code key={node.id}>{node.value}</code>
- const children = renderInlineNodes(node.children)
- if (node.kind === 'strong') return <strong key={node.id}>{children}</strong>
- if (node.kind === 'emphasis') return <em key={node.id}>{children}</em>
- if (node.kind === 'strikethrough') return <s key={node.id}>{children}</s>
- return node.href ? (
- <a href={node.href} key={node.id} rel="noreferrer" target="_blank">
- {children}
- </a>
- ) : (
- <span key={node.id}>{children}</span>
- )
- })
-}
+const MarkdownPreview = dynamic<MarkdownPreviewProps>(
+ () => import('@uiw/react-md-editor').then(editor => editor.default.Markdown),
+ {
+ ssr: false,
+ loading: () => (
+ <div className="cu-identity-preview__loading" role="status">
+ Loading Markdown preview...
+ </div>
+ ),
+ }
+)
-function renderHeading(block: Extract<MarkdownBlock, { kind: 'heading' }>): ReactNode {
- const children = renderInlineNodes(block.children)
- if (block.level === 1) return <h1 key={block.id}>{children}</h1>
- if (block.level === 2) return <h2 key={block.id}>{children}</h2>
- if (block.level === 3) return <h3 key={block.id}>{children}</h3>
- if (block.level === 4) return <h4 key={block.id}>{children}</h4>
- if (block.level === 5) return <h5 key={block.id}>{children}</h5>
- return <h6 key={block.id}>{children}</h6>
+function renderMarkdownLink({ children, href }: { children?: React.ReactNode; href?: string }) {
+ const safeHref = safeMarkdownHref(href ?? '')
+ if (!safeHref) return <span>{children}</span>
+ return (
+ <a href={safeHref} rel="noreferrer" target="_blank">
+ {children}
+ </a>
+ )
}
-function renderBlock(block: MarkdownBlock): ReactNode {
- if (block.kind === 'heading') return renderHeading(block)
- if (block.kind === 'paragraph') return <p key={block.id}>{renderInlineNodes(block.children)}</p>
- if (block.kind === 'blockquote') {
- return <blockquote key={block.id}>{renderInlineNodes(block.children)}</blockquote>
- }
- if (block.kind === 'divider') return <hr key={block.id} />
- if (block.kind === 'code') {
- return (
- <pre key={block.id}>
- <code data-language={block.language ?? undefined}>{block.value}</code>
- </pre>
- )
- }
- const items = block.items.map(item => <li key={item.id}>{renderInlineNodes(item.children)}</li>)
- return block.ordered ? <ol key={block.id}>{items}</ol> : <ul key={block.id}>{items}</ul>
+function renderMarkdownImage({ alt }: { alt?: string | null }) {
+ return <span>[Image: {alt ?? ''}]</span>
}
export function MarkdownContent({
@@ -64,14 +48,35 @@ export function MarkdownContent({
emptyMessage,
source,
}: MarkdownContentProps): React.JSX.Element {
- const blocks = useMemo(() => parseVanillaMarkdown(source), [source])
+ if (!source.trim()) {
+ return (
+ <article aria-label={ariaLabel} className="cu-identity-preview__document">
+ <div className={cn('cu-gfs-markdown-preview__content', className)}>
+ <p className="cu-gfs-markdown-preview__empty">{emptyMessage}</p>
+ </div>
+ </article>
+ )
+ }
+
+ const components: MarkdownPreviewProps['components'] = {
+ a: renderMarkdownLink,
+ img: renderMarkdownImage,
+ }
+
return (
- <article aria-label={ariaLabel} className={className}>
- {blocks.length > 0 ? (
- blocks.map(renderBlock)
- ) : (
- <p className="cu-gfs-markdown-preview__empty">{emptyMessage}</p>
- )}
+ <article
+ aria-label={ariaLabel}
+ className="cu-identity-preview__document"
+ data-color-mode="dark"
+ >
+ <MarkdownPreview
+ className={cn('cu-gfs-markdown-preview__content', className)}
+ components={components}
+ disableCopy
+ skipHtml
+ source={source}
+ urlTransform={url => safeMarkdownHref(url) ?? ''}
+ />
</article>
)
}
diff --git a/control-ui/components/__tests__/HostIdentityTab.test.tsx b/control-ui/components/__tests__/HostIdentityTab.test.tsx
index 73f5b5bb0..cd6346694 100644
--- a/control-ui/components/__tests__/HostIdentityTab.test.tsx
+++ b/control-ui/components/__tests__/HostIdentityTab.test.tsx
@@ -69,6 +69,33 @@ describe('HostIdentityTab', () => {
expect(screen.getByRole('button', { name: 'Edit SOUL.md' })).toBeInTheDocument()
})
+ it('uses the Markdown renderer without activating unsafe links or remote images', async () => {
+ vi.mocked(api.getHostPersonalization).mockResolvedValue({
+ ...initialFiles,
+ identity:
+ '## Safe preview\n\n[Unsafe link](javascript:alert(1))\n\n![Remote image](https://example.invalid/image.png)',
+ })
+ renderTab()
+
+ await screen.findByRole('heading', { name: 'Safe preview' })
+ expect(screen.queryByRole('link', { name: 'Unsafe link' })).not.toBeInTheDocument()
+ expect(screen.getByText('Unsafe link')).toBeInTheDocument()
+ expect(screen.queryByRole('img')).not.toBeInTheDocument()
+ expect(screen.getByText('[Image: Remote image]')).toBeInTheDocument()
+ })
+
+ it('renders GitHub-flavored Markdown tables in the read-only document view', async () => {
+ vi.mocked(api.getHostPersonalization).mockResolvedValue({
+ ...initialFiles,
+ identity: '| Name | Value |\n| --- | --- |\n| Mode | Read-only |',
+ })
+ renderTab()
+
+ const table = await screen.findByRole('table')
+ expect(within(table).getByRole('columnheader', { name: 'Name' })).toBeInTheDocument()
+ expect(within(table).getByRole('cell', { name: 'Read-only' })).toBeInTheDocument()
+ })
+
it.each(['{Enter}', ' '])(
'opens a filled document with keyboard Edit activation %s',
async key => {
diff --git a/control-ui/lib/vanillaMarkdown.ts b/control-ui/lib/vanillaMarkdown.ts
index 0e3292a50..b24d7d3d0 100644
--- a/control-ui/lib/vanillaMarkdown.ts
+++ b/control-ui/lib/vanillaMarkdown.ts
@@ -8,7 +8,7 @@ const ORDERED_LIST_PATTERN = /^\s*\d+[.)]\s+(.+)$/
const DIVIDER_PATTERN = /^\s{0,3}((\*\s*){3,}|(-\s*){3,}|(_\s*){3,})$/
const FENCE_PATTERN = /^\s*(```|~~~)\s*([\w-]+)?\s*$/
-function safeMarkdownHref(rawHref: string): string | null {
+export function safeMarkdownHref(rawHref: string): string | null {
const href = rawHref.trim()
if (/^https?:\/\//i.test(href) || /^mailto:/i.test(href) || href.startsWith('#')) return href
return null
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index dec9bda56..6284ccabc 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.641",
+ "version": "0.1.642",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.641",
+ "version": "0.1.642",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/frontend-components": "file:../packages/frontend-components",
diff --git a/control-ui/package.json b/control-ui/package.json
index 5dfd6b429..719ebb6cb 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.641",
+ "version": "0.1.642",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",