pierrejeambrun commented on code in PR #66809: URL: https://github.com/apache/airflow/pull/66809#discussion_r3468714591
########## airflow-core/src/airflow/ui/src/components/ReactMarkdownBlocks.tsx: ########## @@ -0,0 +1,221 @@ +/*! + * 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 { Box, Flex, Spinner, Text } from "@chakra-ui/react"; +import { useEffect, useId, useState, type ReactNode } from "react"; +import { useTranslation } from "react-i18next"; + +import { LazyClipboard } from "src/components/ui"; +import { useColorMode } from "src/context/colorMode"; +import { renderMermaidDiagram } from "src/utils/renderMermaid"; +import { SyntaxHighlighter, type SyntaxTheme } from "src/utils/syntaxHighlighter"; + +const MarkdownBlockFrame = ({ + action, + children, + label, +}: { + readonly action: ReactNode; + readonly children: ReactNode; + readonly label: string; +}) => ( + <Box maxWidth="100%" minWidth={0} my={3} width="100%"> + <Flex + alignItems="center" + bg="bg.muted" + borderColor="border.emphasized" + borderTopRadius="md" + borderWidth="1px" + justifyContent="space-between" + minH={8} + px={3} + py={1} + > + <Text color="fg.muted" fontFamily="mono" fontSize="xs" lineHeight="short"> + {label} + </Text> + {action} + </Flex> + <Box + borderBottomRadius="md" + borderColor="border.emphasized" + borderTopWidth={0} + borderWidth="1px" + maxWidth="100%" + minWidth={0} + overflow="hidden" + > + {children} + </Box> + </Box> +); + +export const MarkdownCodeBlock = ({ + language, + style, + value, +}: { + readonly language?: string; + readonly style: SyntaxTheme; + readonly value: string; +}) => { + const { t: translate } = useTranslation("components"); + const codeBlockStyle = style['pre[class*="language-"]']; + + return ( + <MarkdownBlockFrame + action={ + <LazyClipboard + aria-label={translate("clipboard.copy")} + data-testid="markdown-copy-button" + getValue={() => value} + title={translate("clipboard.copy")} + /> + } + label={language ?? "text"} + > + <Box + css={{ ...codeBlockStyle, borderRadius: 0, margin: 0 }} + data-testid="markdown-code-scroll-area" + maxWidth="100%" + minWidth={0} + overflowX="auto" + overflowY="hidden" + width="100%" + > + <Box data-testid="markdown-code-content" display="inline-block" minWidth="100%"> + <SyntaxHighlighter + codeTagProps={{ + style: { + background: "transparent", + overflowWrap: "normal", + whiteSpace: "pre", + wordBreak: "normal", + }, + }} + customStyle={{ + background: "transparent", + borderRadius: 0, + margin: 0, + padding: 0, + width: "max-content", + }} + language={language} + lineNumberStyle={{ minWidth: "2.5em", opacity: 0.6, paddingRight: "1em" }} + PreTag="div" + showLineNumbers + style={style} + > + {value} + </SyntaxHighlighter> + </Box> + </Box> + </MarkdownBlockFrame> + ); +}; + +export const MarkdownMermaid = ({ + chart, + fallbackStyle, +}: { + readonly chart: string; + readonly fallbackStyle: SyntaxTheme; +}) => { + const { t: translate } = useTranslation("components"); + const { colorMode } = useColorMode(); + const diagramId = useId().replaceAll(":", ""); + const [error, setError] = useState(false); + const [svg, setSvg] = useState<string>(); + const theme = colorMode === "dark" ? "dark" : "default"; + + useEffect(() => { + let cancelled = false; + + const renderMermaid = async () => { + try { + const renderedSvg = await renderMermaidDiagram({ + chart, + diagramId: `markdown-mermaid-${diagramId}`, + theme, + }); + + if (!cancelled) { + setSvg(renderedSvg); + setError(false); + } + } catch { + if (!cancelled) { + setError(true); + setSvg(undefined); + } + } + }; + + void renderMermaid(); + + return () => { + cancelled = true; + }; + }, [chart, diagramId, theme]); + + if (error) { + return <MarkdownCodeBlock language="mermaid" style={fallbackStyle} value={chart} />; + } + + return ( + <MarkdownBlockFrame + action={ + <LazyClipboard + aria-label={translate("clipboard.copy")} + data-testid="markdown-mermaid-copy-button" + getValue={() => chart} + title={translate("clipboard.copy")} + /> + } + label="mermaid" + > + <Box maxWidth="100%" minHeight="8rem" minWidth={0} overflow="hidden" p={3} width="100%"> + {svg === undefined ? ( + <Box + alignItems="center" + color="fg.muted" + data-testid="markdown-mermaid-loading" + display="inline-flex" + minHeight="2rem" + > + <Spinner size="sm" /> + </Box> + ) : ( + <Box + css={{ + "& svg": { + display: "block", + height: "auto", + marginInline: "auto", + maxWidth: "100%", + width: "100%", + }, + }} + dangerouslySetInnerHTML={{ __html: svg }} Review Comment: ```suggestion /* Trusting mermaid's strict-mode sanitizer */ dangerouslySetInnerHTML={{ __html: svg }} ``` ########## airflow-core/src/airflow/example_dags/tutorial.py: ########## @@ -87,16 +87,35 @@ # [START documentation] t1.doc_md = textwrap.dedent( """\ - #### Task Documentation - You can document your task using the attributes `doc_md` (markdown), - `doc` (plain text), `doc_rst`, `doc_json`, `doc_yaml` which gets - rendered in the UI's Task Instance Details page. -  - **Image Credit:** Randall Munroe, [XKCD](https://xkcd.com/license.html) + #### Print the current date + + This task runs `date` by using the `bash_command` argument on `BashOperator`. + In the Task Instance Details page, Airflow renders this documentation from + the task's `doc_md` field. After this task succeeds, Airflow can run both + downstream tasks: `sleep` and `templated`. + + ```bash + date + ``` + + Display math is rendered with KaTeX. This tutorial starts one task and then + branches into two downstream tasks: + + $$ Review Comment: `$$` fence is ambiguous and could wrongly detect a `math` expression. Is is possible to switch to a \`\`\`math \`\`\` fence instead? to be consistant with code fense? -- 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]
