Skip to content

feat: parse resource metadata values as markdown #10521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 71 additions & 5 deletions site/src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import { FC, memo } from "react";
import ReactMarkdown from "react-markdown";
import { type Interpolation, type Theme } from "@emotion/react";
import isEqual from "lodash/isEqual";
import { type FC, memo } from "react";
import ReactMarkdown, { type Options } from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import gfm from "remark-gfm";
import { colors } from "theme/colors";
import { darcula } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { type Interpolation, type Theme } from "@emotion/react";

interface MarkdownProps {
/**
Expand All @@ -20,10 +21,15 @@ interface MarkdownProps {
children: string;

className?: string;

/**
* Can override the behavior of the generated elements
*/
components?: Options["components"];
}

export const Markdown: FC<MarkdownProps> = (props) => {
const { children, className } = props;
const { children, className, components = {} } = props;

return (
<ReactMarkdown
Expand Down Expand Up @@ -106,14 +112,74 @@ export const Markdown: FC<MarkdownProps> = (props) => {
th: ({ children }) => {
return <TableCell>{children}</TableCell>;
},

...components,
}}
>
{children}
</ReactMarkdown>
);
};

interface MarkdownInlineProps {
/**
* The Markdown text to parse and render
*/
children: string;

className?: string;

/**
* Can override the behavior of the generated elements
*/
components?: Options["components"];
}

/**
* Supports a strict subset of Markdown that bahaves well as inline/confined content.
*/
export const InlineMarkdown: FC<MarkdownInlineProps> = (props) => {
const { children, className, components = {} } = props;

return (
<ReactMarkdown
className={className}
allowedElements={["p", "em", "strong", "a", "pre", "code"]}
unwrapDisallowed
components={{
p: ({ children }) => <>{children}</>,

a: ({ href, target, children }) => (
<Link href={href} target={target}>
{children}
</Link>
),

code: ({ node, className, children, style, ...props }) => (
<code
css={(theme) => ({
padding: "1px 4px",
background: theme.palette.divider,
borderRadius: 4,
color: theme.palette.text.primary,
fontSize: 14,
})}
{...props}
>
{children}
</code>
),

...components,
}}
>
{children}
</ReactMarkdown>
);
};

export const MemoizedMarkdown = memo(Markdown);
export const MemoizedMarkdown = memo(Markdown, isEqual);
export const MemoizedInlineMarkdown = memo(InlineMarkdown, isEqual);

const markdownStyles: Interpolation<Theme> = (theme: Theme) => ({
fontSize: 16,
Expand Down
16 changes: 13 additions & 3 deletions site/src/components/Resources/ResourceCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { type FC, useState } from "react";
import { type FC, type PropsWithChildren, useState } from "react";
import IconButton from "@mui/material/IconButton";
import Tooltip from "@mui/material/Tooltip";
import { type CSSObject, type Interpolation, type Theme } from "@emotion/react";
import { Children } from "react";
import type { WorkspaceAgent, WorkspaceResource } from "api/typesGenerated";
import { DropdownArrow } from "../DropdownArrow/DropdownArrow";
import { CopyableValue } from "../CopyableValue/CopyableValue";
import { MemoizedInlineMarkdown } from "../Markdown/Markdown";
import { Stack } from "../Stack/Stack";
import { ResourceAvatar } from "./ResourceAvatar";
import { SensitiveValue } from "./SensitiveValue";
Expand Down Expand Up @@ -72,6 +74,14 @@ export interface ResourceCardProps {
agentRow: (agent: WorkspaceAgent) => JSX.Element;
}

const p = ({ children }: PropsWithChildren) => {
const childrens = Children.toArray(children);
if (childrens.every((child) => typeof child === "string")) {
return <CopyableValue value={childrens.join("")}>{children}</CopyableValue>;
}
return <>{children}</>;
};

export const ResourceCard: FC<ResourceCardProps> = ({ resource, agentRow }) => {
const [shouldDisplayAllMetadata, setShouldDisplayAllMetadata] =
useState(false);
Expand Down Expand Up @@ -136,9 +146,9 @@ export const ResourceCard: FC<ResourceCardProps> = ({ resource, agentRow }) => {
{meta.sensitive ? (
<SensitiveValue value={meta.value} />
) : (
<CopyableValue value={meta.value}>
<MemoizedInlineMarkdown components={{ p }}>
{meta.value}
</CopyableValue>
</MemoizedInlineMarkdown>
)}
</div>
</div>
Expand Down
38 changes: 38 additions & 0 deletions site/src/components/Resources/Resources.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ const reallyLong = {
sensitive: false,
};

export const Markdown: Story = {
args: {
resources: [
{
...nullDevice,
type: "workspace",
id: "1",
name: "Workspace",
metadata: [
{ key: "text", value: "hello", sensitive: false },
{ key: "link", value: "[hello](#)", sensitive: false },
{ key: "b/i", value: "_hello_, **friend**!", sensitive: false },
{ key: "coder", value: "`beep boop`", sensitive: false },
],
},

// bits of Markdown that are intentionally not supported here
{
...nullDevice,
type: "unsupported",
id: "2",
name: "Unsupported",
metadata: [
{
key: "multiple paragraphs",
value: `home,

home on the range`,
sensitive: false,
},
{ key: "heading", value: "# HI", sensitive: false },
{ key: "image", value: "![go](/icon/go.svg)", sensitive: false },
],
},
],
},
};

export const BunchOfDevicesWithMetadata: Story = {
args: {
resources: [
Expand Down