From 4e9a454376542464b2f8773f68b19eef9ed75daa Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Wed, 8 Nov 2023 21:24:05 +0100 Subject: [PATCH] refactor(ai-help): extract Markdown component --- client/src/plus/ai-help/index.tsx | 85 ++-------------------------- client/src/plus/ai-help/markdown.tsx | 72 +++++++++++++++++++++++ 2 files changed, 76 insertions(+), 81 deletions(-) create mode 100644 client/src/plus/ai-help/markdown.tsx diff --git a/client/src/plus/ai-help/index.tsx b/client/src/plus/ai-help/index.tsx index 2cd2742f667c..9337db1cd4c6 100644 --- a/client/src/plus/ai-help/index.tsx +++ b/client/src/plus/ai-help/index.tsx @@ -1,14 +1,4 @@ -import Prism from "prismjs"; -import { - Children, - MutableRefObject, - ReactElement, - useEffect, - useRef, - useState, -} from "react"; -import ReactMarkdown from "react-markdown"; -import remarkGfm from "remark-gfm"; +import { MutableRefObject, useEffect, useRef, useState } from "react"; import { Message, MessageRole, Quota, useAiChat } from "./use-ai"; import { AiLoginBanner, AiUpsellBanner } from "./banners"; @@ -26,11 +16,11 @@ import { GleanThumbs } from "../../ui/atoms/thumbs"; import NoteCard from "../../ui/molecules/notecards"; import { Loading } from "../../ui/atoms/loading"; import { useLocation } from "react-router-dom"; -import { isExternalUrl } from "./utils"; import { useGleanClick } from "../../telemetry/glean-context"; import { AI_HELP } from "../../telemetry/constants"; import MDNModal from "../../ui/atoms/modal"; import { AI_FEEDBACK_GITHUB_REPO } from "../../env"; +import { Markdown } from "./markdown"; type Category = "apis" | "css" | "html" | "http" | "js" | "learn"; @@ -213,76 +203,9 @@ export function AIHelpInner() { message.content ) : ( <> - { - if (isExternalUrl(props.href ?? "")) { - props = { - ...props, - className: "external", - rel: "noopener noreferrer", - target: "_blank", - }; - } - // eslint-disable-next-line jsx-a11y/anchor-has-content - return ; - }, - pre: ({ node, className, children, ...props }) => { - const code = Children.toArray(children) - .map( - (child) => - /language-(\w+)/.exec( - (child as ReactElement)?.props?.className || - "" - )?.[1] - ) - .find(Boolean); - - if (!code) { - return ( -
-                                  {children}
-                                
- ); - } - return ( -
-

- {code} -

-
-                                  {children}
-                                
-
- ); - }, - code: ({ inline, className, children, ...props }) => { - const match = /language-(\w+)/.exec( - className || "" - ); - const lang = Prism.languages[match?.[1]]; - return !inline && lang ? ( - - ) : ( - - {children} - - ); - }, - }} - > + {message.content.replace(SORRY_BACKEND, SORRY_FRONTEND)} -
+ {message.status === "complete" && !message.content.includes(SORRY_BACKEND) && ( <> diff --git a/client/src/plus/ai-help/markdown.tsx b/client/src/plus/ai-help/markdown.tsx new file mode 100644 index 000000000000..7be5968a016c --- /dev/null +++ b/client/src/plus/ai-help/markdown.tsx @@ -0,0 +1,72 @@ +import Prism from "prismjs"; +import { Children, ReactElement } from "react"; +import ReactMarkdown from "react-markdown"; +import remarkGfm from "remark-gfm"; +import { isExternalUrl } from "./utils"; + +export function Markdown({ children }: { children: string }) { + return ( + { + if (isExternalUrl(props.href ?? "")) { + props = { + ...props, + className: "external", + rel: "noopener noreferrer", + target: "_blank", + }; + } + // eslint-disable-next-line jsx-a11y/anchor-has-content + return ; + }, + pre: ({ node, className, children, ...props }) => { + const code = Children.toArray(children) + .map( + (child) => + /language-(\w+)/.exec( + (child as ReactElement)?.props?.className || "" + )?.[1] + ) + .find(Boolean); + + if (!code) { + return ( +
+                {children}
+              
+ ); + } + return ( +
+

+ {code} +

+
{children}
+
+ ); + }, + code: ({ inline, className, children, ...props }) => { + const match = /language-(\w+)/.exec(className || ""); + const lang = Prism.languages[match?.[1]]; + return !inline && lang ? ( + + ) : ( + + {children} + + ); + }, + }} + > + {children} + + ); +}