Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(ui): markdown - properly render list items with styled text elements (#2633)

marliessophie3a1f34a54ca2
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 6aea2dae83..e22dfcdd99 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -1,5 +1,14 @@
import { cn } from "@/src/utils/tailwind";
-import { type FC, memo, type ReactNode, useState } from "react";
+import {
+ type FC,
+ type ReactNode,
+ type ReactElement,
+ memo,
+ useState,
+ isValidElement,
+ Children,
+ createElement,
+} from "react";
import ReactMarkdown, { type Options } from "react-markdown";
import Link from "next/link";
import DOMPurify from "dompurify";
@@ -21,6 +30,22 @@ const isChecklist = (children: ReactNode) =>
Array.isArray(children) &&
children.some((child: any) => child?.props?.className === "task-list-item");
+const isTextElement = (child: ReactNode): child is ReactElement =>
+ isValidElement(child) &&
+ typeof child.type !== "string" &&
+ ["p", "h1", "h2", "h3", "h4", "h5", "h6"].includes(child.type.name);
+
+const transformListItemChildren = (children: ReactNode) =>
+ Children.map(children, (child) =>
+ isTextElement(child) ? (
+ <div className="mb-1 inline-flex">
+ {createElement(child.type, { ...child.props })}
+ </div>
+ ) : (
+ child
+ ),
+ );
+
export function MarkdownView({
markdown,
title,
@@ -97,7 +122,14 @@ export function MarkdownView({
);
},
li({ children }) {
- return <li className="mb-1">{children}</li>;
+ return (
+ <li className="mb-1 list-item">
+ {transformListItemChildren(children)}
+ </li>
+ );
+ },
+ pre({ children }) {
+ return <pre className="rounded p-2">{children}</pre>;
},
h1({ children }) {
return <h1 className="text-2xl font-bold">{children}</h1>;