Skip to content

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

feat(web): syntax-highlight shell, TOML and JSON setup snippets (#19306)

Tobias Wochingerbb85692102a0
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ebed9fdb67..07f2c3a17d 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -917,6 +917,9 @@ importers:
react18-json-view:
specifier: ^0.2.8-canary.6
version: 0.2.8-canary.6([email protected])
+ refractor:
+ specifier: ^5.0.0
+ version: 5.0.0
remark-gfm:
specifier: ^4.0.1
version: 4.0.1([email protected])
@@ -11079,6 +11082,9 @@ packages:
resolution: {integrity: sha512-00o4I+DVrefhv+nX0ulyi3biSHCPDe+yLv5o/p6d/UVlirijB8E16FtfwSAi4g3tcqrQ4lRAqQSoFEZJehYEcw==}
engines: {node: '>= 0.4'}
+ resolution: {integrity: sha512-QXOrHQF5jOpjjLfiNk5GFnWhRXvxjUVnlFxkeDmewR5sXkr3iM46Zo+CnRR8B+MDVqkULW4EcLVcRBNOPXHosw==}
+
resolution: {integrity: sha512-iETxpjK6YoRWJG5o6hXLwvjYAoW+FEZn9os0PD/b6AP6xQwsa/Y7lCVgIixBbUPMfhu+i2LtdeAqVTgGlQarfA==}
hasBin: true
@@ -23322,6 +23328,13 @@ snapshots:
get-proto: 1.0.1
which-builtin-type: 1.2.1
+ dependencies:
+ '@types/hast': 3.0.4
+ '@types/prismjs': 1.26.5
+ hastscript: 9.0.1
+ parse-entities: 4.0.2
+
diff --git a/web/package.json b/web/package.json
index 086f3a99d2..c3b35d2bd8 100644
--- a/web/package.json
+++ b/web/package.json
@@ -163,6 +163,7 @@
"react-resizable-panels": "^4.8.0",
"react-responsive": "^10.0.1",
"react18-json-view": "^0.2.8-canary.6",
+ "refractor": "^5.0.0",
"remark-gfm": "^4.0.1",
"rxjs": "7.8.1",
"sonner": "^2.0.7",
diff --git a/web/src/components/design-system/CodeSection/CodeSection.stories.tsx b/web/src/components/design-system/CodeSection/CodeSection.stories.tsx
index 666ad04fdd..fc22a3a3f4 100644
--- a/web/src/components/design-system/CodeSection/CodeSection.stories.tsx
+++ b/web/src/components/design-system/CodeSection/CodeSection.stories.tsx
@@ -23,6 +23,10 @@ const longCode = Array.from(
`line ${index + 1}: const value${index + 1} = compute(${index + 1});`,
).join("\n");
+const shellCode = `# List the models the key can reach
+curl -s "$LANGFUSE_GATEWAY_HOST/openai/v1/models" \\
+ -H "Authorization: Bearer $LANGFUSE_GATEWAY_KEY"`;
+
const longLine =
"Authorization: Basic cGstbGYtMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwOnNrLWxmLTAwMDAwMDAwLTAwMDAtMDAwMC0wMDAwLTAwMDAwMDAwMDAwMA==";
@@ -110,6 +114,15 @@ export const LongLineWithoutWrap = meta.story({
},
});
+export const WithLanguage = meta.story({
+ args: {
+ variant: "outline",
+ language: "sh",
+ shouldWrapLines: false,
+ content: shellCode,
+ },
+});
+
export const VariantMatrix = meta.story({
render: () => (
<div className="grid gap-6">
diff --git a/web/src/components/design-system/CodeSection/CodeSection.tsx b/web/src/components/design-system/CodeSection/CodeSection.tsx
index dd6105deac..cae300d1d2 100644
--- a/web/src/components/design-system/CodeSection/CodeSection.tsx
+++ b/web/src/components/design-system/CodeSection/CodeSection.tsx
@@ -11,6 +11,7 @@ import {
import { type ReactNode, useState } from "react";
import { IconButton } from "../IconButton/IconButton";
+import { SyntaxHighlight } from "../SyntaxHighlight/SyntaxHighlight";
import { Tooltip } from "../Tooltip/Tooltip";
import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
@@ -76,6 +77,8 @@ type CodeSectionProps = {
content: string;
/** Custom body, e.g. highlighted variables; copy still uses `content`. */
children?: ReactNode;
+ /** Prism language name, e.g. `sh` or `json`, to syntax-highlight `content`. Ignored when `children` is set. */
+ language?: string;
/** Omitted = no header row; actions move into the body's top-right corner. */
title?: string;
variant?: "filled" | "outline" | "plain";
@@ -92,6 +95,7 @@ type CodeSectionProps = {
export function CodeSection({
content,
children,
+ language,
title,
variant = "filled",
actions = [],
@@ -193,7 +197,14 @@ export function CodeSection({
dir="auto"
style={{ unicodeBidi: "plaintext" }}
>
- {children ?? content}
+ {children ??
+ (language === undefined ? (
+ content
+ ) : (
+ <SyntaxHighlight code={content} language={language}>
+ {({ content: highlighted }) => highlighted}
+ </SyntaxHighlight>
+ ))}
</code>
{title ? null : actionGroup}
</div>
diff --git a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
index e028f17537..c3a4a5c50c 100644
--- a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
+++ b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
@@ -31,11 +31,11 @@ export const LightThemeTokenRendering = Default.extend({
play: async ({ args, canvasElement, canvas }) => {
const preview = canvasElement.querySelector("pre")!;
await expect(preview).toHaveTextContent(String(args.value));
- const keyword = preview.querySelector(".token.keyword")!;
+ const keyword = preview.querySelector('[data-token~="keyword"]')!;
await expect(keyword).toHaveTextContent("const");
- await expect(preview.querySelector(".token.string")).toHaveTextContent(
- '"Hello, Langfuse!"',
- );
+ await expect(
+ preview.querySelector('[data-token~="string"]'),
+ ).toHaveTextContent('"Hello, Langfuse!"');
const theme = args.theme === "dark" ? themes.vsDark : themes.github;
const keywordStyle = theme.styles
.filter((rule) => {
@@ -261,6 +261,8 @@ export const HtmlSafety = Default.extend({
const preview = canvasElement.querySelector("pre")!;
await expect(preview.textContent).toBe(args.value);
await expect(preview.querySelector("script")).toBeNull();
- await expect(preview.querySelector(".token.tag")).toBeInTheDocument();
+ await expect(
+ preview.querySelector('[data-token~="tag"]'),
+ ).toBeInTheDocument();
},
});
diff --git a/web/src/components/design-system/Codeblock/Codeblock.tsx b/web/src/components/design-system/Codeblock/Codeblock.tsx
index 61bca122c2..4ce4f87b4d 100644
--- a/web/src/components/design-system/Codeblock/Codeblock.tsx
+++ b/web/src/components/design-system/Codeblock/Codeblock.tsx
@@ -72,7 +72,9 @@ export type CodeblockLanguage =
| "json"
| "webmanifest"
| "bash"
- | "shell";
+ | "sh"
+ | "shell"
+ | "toml";
interface CodeblockProps {
/** Use a labelled grammar when the source language has no matching grammar. */
diff --git a/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx b/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx
index f635b2980e..dd150274b3 100644
--- a/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx
+++ b/web/src/components/design-system/SyntaxHighlight/SyntaxHighlight.tsx
@@ -1,7 +1,18 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { type ComponentProps, type ReactNode } from "react";
-import { Highlight, themes, type PrismTheme } from "prism-react-renderer";
+import {
+ Highlight,
+ Prism,
+ themes,
+ type PrismTheme,
+} from "prism-react-renderer";
import { useTheme } from "next-themes";
+import bash from "refractor/bash";
+import toml from "refractor/toml";
+
+// prism-react-renderer bundles no shell or TOML grammar. `bash` also registers `sh` and `shell`.
+bash(Prism);
+toml(Prism);
/** JSON token colors shared with the table JSON viewer; Prism aliases `null` to `keyword`. */
const JSON_TOKEN_STYLES: PrismTheme["styles"] = [
@@ -18,6 +29,37 @@ const JSON_TOKEN_STYLES: PrismTheme["styles"] = [
const LIGHT_THEME = withJsonTokens(themes.github);
const DARK_THEME = withJsonTokens(themes.vsDark);
+const SHELL_LANGUAGES = new Set(["bash", "sh", "shell"]);
+
+/**
+ * Shell gets a whole theme because plain text is themed too (VS Dark renders it light blue).
+ * App colours, so it follows light/dark mode on its own: values in the JSON viewer's string
+ * green, commands in the search bar's keyword blue, punctuation muted.
+ */
+const SHELL_THEME: PrismTheme = {
+ plain: { color: "hsl(var(--foreground))" },
+ styles: [
+ {
+ types: [
+ "comment",
+ "shebang",
+ "punctuation",
+ "operator",
+ "file-descriptor",
+ ],
+ style: { color: "hsl(var(--muted-foreground))" },
+ },
+ {
+ types: ["string", "char", "number", "boolean"],
+ style: { color: "var(--json-value-string)" },
+ },
+ {
+ types: ["function", "builtin", "keyword"],
+ style: { color: "hsl(var(--qlang-keyword))" },
+ },
+ ],
+};
+
/** Shares token rendering while callers own the code container and controls. */
export function SyntaxHighlight({
code,
@@ -40,7 +82,7 @@ export function SyntaxHighlight({
<Highlight
code={code}
language={language}
- theme={appliedTheme === "dark" ? DARK_THEME : LIGHT_THEME}
+ theme={pickTheme(language, appliedTheme)}
>
{({ className, style, tokens, getLineProps, getTokenProps }) =>
children({
@@ -48,9 +90,19 @@ export function SyntaxHighlight({
style,
content: tokens.map((line, index) => (
<div key={index} {...getLineProps({ line })}>
- {line.map((token, key) => (
- <span key={key} {...getTokenProps({ token })} />
- ))}
+ {line.map((token, key) => {
+ // Prism's per-type class names include Tailwind utilities such as `table`; the theme styles tokens inline instead.
+ const { style, children } = getTokenProps({ token });
+ return (
+ <span
+ key={key}
+ style={style}
+ data-token={token.types.join(" ")}
+ >
+ {children}
+ </span>
+ );
+ })}
</div>
)),
})
@@ -59,6 +111,11 @@ export function SyntaxHighlight({
);
}
+function pickTheme(language: string, mode: string | undefined): PrismTheme {
+ if (SHELL_LANGUAGES.has(language)) return SHELL_THEME;
+ return mode === "dark" ? DARK_THEME : LIGHT_THEME;
+}
+
function withJsonTokens(theme: PrismTheme): PrismTheme {
return { ...theme, styles: [...theme.styles, ...JSON_TOKEN_STYLES] };
}
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent/components/AgentSetupSteps.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent/components/AgentSetupSteps.tsx
index 481bfabe64..b3e1ed0d5d 100644
--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent/components/AgentSetupSteps.tsx
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GeneratedKeyContent/components/AgentSetupSteps.tsx
@@ -45,6 +45,7 @@ export function AgentSetupSteps({ instructions }: AgentSetupStepsProps) {
<CodeSection
variant="outline"
content={step.code}
+ language={step.language}
shouldWrapLines={false}
/>
)}
diff --git a/web/src/features/skills/components/CliSkillImportInstructions.stories.tsx b/web/src/features/skills/components/CliSkillImportInstructions.stories.tsx
index 0fc078fd0b..b035831d8b 100644
--- a/web/src/features/skills/components/CliSkillImportInstructions.stories.tsx
+++ b/web/src/features/skills/components/CliSkillImportInstructions.stories.tsx
@@ -2,6 +2,14 @@ import { expect, spyOn, userEvent, within } from "storybook/test";
import preview from "../../../../.storybook/preview";
import { CliSkillImportInstructions } from "./CliSkillImportInstructions";
+/** Highlighted commands are split across token spans, so match the whole code block. */
+const codeBlockWithText =
+ (text: string | RegExp) => (_content: string, element: Element | null) =>
+ element?.tagName === "PRE" &&
+ (typeof text === "string"
+ ? element.textContent === text
+ : text.test(element.textContent ?? ""));
+
const meta = preview.meta({
component: CliSkillImportInstructions,
args: {
@@ -23,7 +31,9 @@ export const ImportScopes = meta.story({
try {
const recursiveCommand =
"langfuse skills push '.agents/skills' --recursive";
- await expect(canvas.getByText(recursiveCommand)).toBeVisible();
+ await expect(
+ canvas.getByText(codeBlockWithText(recursiveCommand)),
+ ).toBeVisible();
const configure = canvas.getByRole("button", {
name: "Configure import command",
});
@@ -36,7 +46,9 @@ export const ImportScopes = meta.story({
"team's skills/review-code",
);
const singleCommand = `langfuse skills push 'team'"'"'s skills/review-code'`;
- await expect(canvas.getByText(singleCommand)).toBeVisible();
+ await expect(
+ canvas.getByText(codeBlockWithText(singleCommand)),
+ ).toBeVisible();
await userEvent.click(
within(canvas.getByRole("group", { name: "Setup prompt" })).getByRole(
"button",
@@ -50,12 +62,16 @@ export const ImportScopes = meta.story({
expect.stringContaining(`Langfuse host: ${JSON.stringify(args.host)}`),
);
await userEvent.click(canvas.getByRole("tab", { name: "All skills" }));
- await expect(canvas.getByText(recursiveCommand)).toBeVisible();
+ await expect(
+ canvas.getByText(codeBlockWithText(recursiveCommand)),
+ ).toBeVisible();
await userEvent.click(canvas.getByRole("tab", { name: "Single skill" }));
await expect(canvas.getByLabelText("Skill folder")).toHaveValue(
"team's skills/review-code",
);
- await expect(canvas.getByText(singleCommand)).toBeVisible();
+ await expect(
+ canvas.getByText(codeBlockWithText(singleCommand)),
+ ).toBeVisible();
} finally {
writeText.mockRestore();
}
@@ -78,7 +94,7 @@ export const EmptyPath = meta.story({
canvas.queryByRole("group", { name: "Setup prompt" }),
).not.toBeInTheDocument();
await expect(
- canvas.queryByText(/langfuse skills push/),
+ canvas.queryByText(codeBlockWithText(/langfuse skills push/)),
).not.toBeInTheDocument();
},
});
diff --git a/web/src/features/skills/components/SkillInstallationOptions.stories.tsx b/web/src/features/skills/components/SkillInstallationOptions.stories.tsx
index eeb2e69cd7..c46b17e534 100644
--- a/web/src/features/skills/components/SkillInstallationOptions.stories.tsx
+++ b/web/src/features/skills/components/SkillInstallationOptions.stories.tsx
@@ -2,6 +2,14 @@ import { expect, userEvent, within } from "storybook/test";
import preview from "../../../../.storybook/preview";
import { SkillInstallationOptions } from "./SkillInstallationOptions";
+/** Highlighted commands are split across token spans, so match the whole code block. */
+const codeBlockWithText =
+ (text: string | RegExp) => (_content: string, element: Element | null) =>
+ element?.tagName === "PRE" &&
+ (typeof text === "string"
+ ? element.textContent === text
+ : text.test(element.textContent ?? ""));
+
const meta = preview.meta({ component: SkillInstallationOptions });
export const FromSkill = meta.story({
@@ -70,7 +78,9 @@ export const InstallScopes = meta.story({
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(
- canvas.getByText("langfuse skills install --all --label='production'"),
+ canvas.getByText(
+ codeBlockWithText("langfuse skills install --all --label='production'"),
+ ),
).toBeVisible();
const configure = canvas.getByRole("button", {
name: "Configure install command",
@@ -81,20 +91,26 @@ export const InstallScopes = meta.story({
await userEvent.click(canvas.getByRole("tab", { name: "Name" }));
await expect(
- canvas.getByText("langfuse skills install 'review-code' --version 7"),
+ canvas.getByText(
+ codeBlockWithText("langfuse skills install 'review-code' --version 7"),
+ ),
).toBeVisible();
await userEvent.click(canvas.getByRole("tab", { name: "Tag" }));
await expect(
canvas.getByText(
- "langfuse skills install --tag='engineering' --label='production'",
+ codeBlockWithText(
+ "langfuse skills install --tag='engineering' --label='production'",
+ ),
),
).toBeVisible();
await userEvent.click(canvas.getByRole("tab", { name: "All" }));
await userEvent.click(configure);
await expect(
- canvas.getByText("langfuse skills install --all --label='production'"),
+ canvas.getByText(
+ codeBlockWithText("langfuse skills install --all --label='production'"),
+ ),
).toBeVisible();
},
});
diff --git a/web/src/utils/normalizeCodeblockLanguage.ts b/web/src/utils/normalizeCodeblockLanguage.ts
index 82ca738b95..7d97b165a2 100644
--- a/web/src/utils/normalizeCodeblockLanguage.ts
+++ b/web/src/utils/normalizeCodeblockLanguage.ts
@@ -1,6 +1,6 @@
import type { CodeblockLanguage } from "@/src/components/design-system/Codeblock/Codeblock";
-// Prism's bundled grammars and aliases, plus shell labels used by existing callers.
+// Grammars and aliases available to Prism in `SyntaxHighlight`.
const supportedLanguages = [
"plain",
"plaintext",
@@ -53,7 +53,9 @@ const supportedLanguages = [
"json",
"webmanifest",
"bash",
+ "sh",
"shell",
+ "toml",
] as const satisfies readonly CodeblockLanguage[];
export function normalizeCodeblockLanguage(