Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

3.1 KB
Raw Blame
"use client";

import { createCodePlugin } from "@streamdown/code";
import { geistShikiTheme } from "@vercel/geistdocs/shiki-theme";
import { useMemo } from "react";
import { type Components, defaultRemarkPlugins, Streamdown } from "streamdown";
import {
  createReadmeHeadingIdsPlugin,
  createResolveReadmeLinksPlugin,
  resolveReadmeHref,
} from "@/lib/templates/readme-links";

interface TemplateReadmeProps {
  readme: string;
  sourceRevisionHref: string;
}

export const TemplateReadme = ({ readme, sourceRevisionHref }: TemplateReadmeProps) => {
  const codePlugin = useMemo(
    () => createCodePlugin({ themes: [geistShikiTheme, geistShikiTheme] }),
    [],
  );
  const remarkPlugins = useMemo(
    () => [
      ...Object.values(defaultRemarkPlugins),
      createReadmeHeadingIdsPlugin(),
      createResolveReadmeLinksPlugin(sourceRevisionHref),
    ],
    [sourceRevisionHref],
  );
  const components = useMemo<Partial<Components>>(
    () => ({
      div: (props) => {
        const { align: _align, ...htmlProps } = props as typeof props & { align?: string };
        return <div {...htmlProps} className="text-left" />;
      },
      a: ({ href, ...props }) => {
        const resolvedHref = resolveReadmeHref(href, sourceRevisionHref);
        if (!resolvedHref) {
          return <span>{props.children}</span>;
        }
        const external = !resolvedHref.startsWith("#");
        return (
          <a
            {...props}
            href={resolvedHref}
            rel={external ? "noopener noreferrer" : undefined}
            target={external ? "_blank" : undefined}
          />
        );
      },
      h1: ({ children, ...props }) => (
        <h2 {...props} className="mt-10 mb-4 text-heading-32 text-gray-1000 first:mt-0">
          {children}
        </h2>
      ),
      h2: ({ children, ...props }) => (
        <h3 {...props} className="mt-8 mb-3 text-heading-24 text-gray-1000">
          {children}
        </h3>
      ),
      h3: ({ children, ...props }) => (
        <h4 {...props} className="mt-6 mb-3 text-heading-20 text-gray-1000">
          {children}
        </h4>
      ),
      h4: ({ children, ...props }) => (
        <h5 {...props} className="mt-6 mb-3 text-heading-16 text-gray-1000">
          {children}
        </h5>
      ),
    }),
    [sourceRevisionHref],
  );

  return (
    <Streamdown
      allowElement={(element) => {
        for (const child of element.children) {
          if (child.type !== "text") continue;
          if (/\[(?:Image )?blocked(?::[^\]]*)?\]/i.test(child.value)) return false;
        }
        return true;
      }}
      className="min-w-0 text-gray-900 [&_a]:font-medium [&_a]:text-gray-1000 [&_a]:underline [&_a]:underline-offset-4 [&_blockquote]:border-gray-alpha-400 [&_blockquote]:border-l [&_blockquote]:pl-4 [&_code]:text-gray-1000 [&_li]:my-1 [&_ol]:my-3 [&_ol]:list-decimal [&_ol]:pl-5 [&_p]:my-3 [&_table]:my-5 [&_ul]:my-3 [&_ul]:list-disc [&_ul]:pl-5"
      components={components}
      disallowedElements={["img"]}
      mode="static"
      plugins={{ code: codePlugin }}
      remarkPlugins={remarkPlugins}
      shikiTheme={[geistShikiTheme, geistShikiTheme]}
    >
      {readme}
    </Streamdown>
  );
};