bbovenzi commented on code in PR #66809:
URL: https://github.com/apache/airflow/pull/66809#discussion_r3306136694


##########
airflow-core/src/airflow/ui/src/context/mermaid/MermaidProvider.tsx:
##########
@@ -0,0 +1,74 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { PropsWithChildren } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+
+import { useColorMode } from "src/context/colorMode";
+
+import { MermaidContext, type MermaidRenderParams, type MermaidTheme } from 
"./Context";
+
+type MermaidApi = {
+  initialize: (config: { securityLevel: "strict"; startOnLoad: false; theme: 
MermaidTheme }) => void;
+  render: (diagramId: string, chart: string) => Promise<{ svg: string }>;
+};
+
+let mermaidModulePromise: Promise<MermaidApi> | undefined;
+
+const getMermaid = async (): Promise<MermaidApi> => {
+  mermaidModulePromise ??= import("mermaid").then(({ default: mermaid }) => 
mermaid as MermaidApi);
+
+  return mermaidModulePromise;
+};
+
+export const MermaidProvider = ({ children }: PropsWithChildren) => {
+  const { colorMode } = useColorMode();
+  const theme: MermaidTheme = colorMode === "dark" ? "dark" : "default";
+  const [initializedTheme, setInitializedTheme] = useState<MermaidTheme | 
undefined>(undefined);
+  const initializedThemeRef = useRef<MermaidTheme | 
undefined>(initializedTheme);
+
+  useEffect(() => {
+    initializedThemeRef.current = initializedTheme;
+  }, [initializedTheme]);
+
+  const initializeMermaid = useCallback(async (): Promise<MermaidApi> => {

Review Comment:
   We don't really need to call `useCallback` anymore with the latest react 
compiler



##########
airflow-core/src/airflow/ui/src/components/ReactMarkdown.tsx:
##########
@@ -106,81 +134,106 @@ const UlComponent = ({ children }: PropsWithChildren) => 
(
   </List.Root>
 );
 
-// Factory function for the code component that needs style
-const createCodeComponent =
-  (style: typeof oneDark | typeof oneLight) =>
-  ({
-    children,
-    className,
-    inline,
-  }: {
-    readonly children: ReactNode;
-    readonly className?: string;
-    readonly inline?: boolean;
-  }) => {
-    if (inline) {
-      return (
-        <Code display="inline" p={2}>
-          {children}
-        </Code>
-      );
+type MarkdownCodeElementProps = {
+  readonly children?: ReactNode;
+  readonly className?: string;
+};
+
+const hasDisplayMath = (children: Options["children"]): boolean =>
+  typeof children === "string" && children.includes("$$");
+
+const extractText = (child: ReactNode): string => (typeof child === "string" ? 
child : "");
+
+const extractTextContent = (children: MarkdownCodeElementProps["children"]): 
string => {
+  if (Array.isArray(children)) {
+    return children.map(extractText).join("");
+  }
+
+  return extractText(children);
+};
+
+const InlineCodeComponent = ({ children }: PropsWithChildren) => <Code 
display="inline">{children}</Code>;
+
+// Factory function for the pre component that needs style
+const createPreComponent =
+  (style: SyntaxTheme) =>
+  ({ children }: { readonly children?: ReactNode }) => {
+    const [codeElement] = Children.toArray(children);
+
+    if (!isValidElement<MarkdownCodeElementProps>(codeElement)) {
+      return <Box my={3}>{children}</Box>;
     }
 
     // Extract language from className (format: "language-python")
-    const match = /language-(?<lang>\w+)/u.exec(className ?? "");
+    const { children: codeChildren, className } = codeElement.props;
+    const match = /language-(?<lang>[-\w]+)/u.exec(className ?? "");
     const language = match?.groups?.lang;
+    const childString = extractTextContent(codeChildren).replace(/\n$/u, "");

Review Comment:
   ```suggestion
   const codeText = Array.isArray(codeChildren)
     ? codeChildren.join("")
     : (codeChildren ?? "");
   
   const childString = String(codeText).replace(/\n$/u, "");
   ```
   
   Then we don't need extractText & extractTextContent



##########
airflow-core/src/airflow/ui/src/context/mermaid/MermaidProvider.tsx:
##########
@@ -0,0 +1,74 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { PropsWithChildren } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+
+import { useColorMode } from "src/context/colorMode";
+
+import { MermaidContext, type MermaidRenderParams, type MermaidTheme } from 
"./Context";
+
+type MermaidApi = {
+  initialize: (config: { securityLevel: "strict"; startOnLoad: false; theme: 
MermaidTheme }) => void;
+  render: (diagramId: string, chart: string) => Promise<{ svg: string }>;
+};
+
+let mermaidModulePromise: Promise<MermaidApi> | undefined;
+
+const getMermaid = async (): Promise<MermaidApi> => {
+  mermaidModulePromise ??= import("mermaid").then(({ default: mermaid }) => 
mermaid as MermaidApi);
+
+  return mermaidModulePromise;
+};
+
+export const MermaidProvider = ({ children }: PropsWithChildren) => {
+  const { colorMode } = useColorMode();
+  const theme: MermaidTheme = colorMode === "dark" ? "dark" : "default";
+  const [initializedTheme, setInitializedTheme] = useState<MermaidTheme | 
undefined>(undefined);
+  const initializedThemeRef = useRef<MermaidTheme | 
undefined>(initializedTheme);
+
+  useEffect(() => {
+    initializedThemeRef.current = initializedTheme;
+  }, [initializedTheme]);

Review Comment:
   We don't need so much state management. Let's just initialize mermaid during 
`renderDiagram`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to