jscheffl commented on code in PR #53657:
URL: https://github.com/apache/airflow/pull/53657#discussion_r2234078646


##########
airflow-core/src/airflow/ui/src/pages/TaskInstance/RenderedTemplates.tsx:
##########
@@ -16,14 +16,75 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-import { Box, HStack, Table, Code } from "@chakra-ui/react";
+import { Box, HStack, Table } from "@chakra-ui/react";
 import { useParams } from "react-router-dom";
+import { PrismLight as SyntaxHighlighter } from "react-syntax-highlighter";
+import bash from "react-syntax-highlighter/dist/esm/languages/prism/bash";
+import json from "react-syntax-highlighter/dist/esm/languages/prism/json";
+import sql from "react-syntax-highlighter/dist/esm/languages/prism/sql";
+import yaml from "react-syntax-highlighter/dist/esm/languages/prism/yaml";
+import { oneLight, oneDark } from 
"react-syntax-highlighter/dist/esm/styles/prism";
 
 import { useTaskInstanceServiceGetMappedTaskInstance } from "openapi/queries";
 import { ClipboardRoot, ClipboardIconButton } from "src/components/ui";
+import { useColorMode } from "src/context/colorMode";
+
+SyntaxHighlighter.registerLanguage("json", json);
+SyntaxHighlighter.registerLanguage("yaml", yaml);
+SyntaxHighlighter.registerLanguage("sql", sql);
+SyntaxHighlighter.registerLanguage("bash", bash);
+
+const detectLanguage = (value: string): string => {

Review Comment:
   Can you separate this function out into a separate TS module file?



##########
airflow-core/src/airflow/ui/src/pages/TaskInstance/RenderedTemplates.tsx:
##########
@@ -16,14 +16,75 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-import { Box, HStack, Table, Code } from "@chakra-ui/react";
+import { Box, HStack, Table } from "@chakra-ui/react";
 import { useParams } from "react-router-dom";
+import { PrismLight as SyntaxHighlighter } from "react-syntax-highlighter";
+import bash from "react-syntax-highlighter/dist/esm/languages/prism/bash";
+import json from "react-syntax-highlighter/dist/esm/languages/prism/json";
+import sql from "react-syntax-highlighter/dist/esm/languages/prism/sql";
+import yaml from "react-syntax-highlighter/dist/esm/languages/prism/yaml";
+import { oneLight, oneDark } from 
"react-syntax-highlighter/dist/esm/styles/prism";
 
 import { useTaskInstanceServiceGetMappedTaskInstance } from "openapi/queries";
 import { ClipboardRoot, ClipboardIconButton } from "src/components/ui";
+import { useColorMode } from "src/context/colorMode";
+
+SyntaxHighlighter.registerLanguage("json", json);
+SyntaxHighlighter.registerLanguage("yaml", yaml);
+SyntaxHighlighter.registerLanguage("sql", sql);
+SyntaxHighlighter.registerLanguage("bash", bash);
+
+const detectLanguage = (value: string): string => {
+  const trimmed = value.trim();
+
+  // Try to detect JSON
+  if (
+    (trimmed.startsWith("{") && trimmed.endsWith("}")) ||
+    (trimmed.startsWith("[") && trimmed.endsWith("]"))
+  ) {
+    try {
+      JSON.parse(trimmed);
+
+      return "json";
+    } catch {
+      // Not valid JSON, continue
+    }
+  }
+
+  // Try to detect YAML (basic heuristics)
+  if (trimmed.includes(":") && (trimmed.includes("\n") || trimmed.includes("- 
"))) {
+    return "yaml";
+  }
+
+  // Try to detect SQL (basic heuristics)
+  const sqlKeywords = 
/\b(?:select|insert|update|delete|create|alter|drop|from|where|join)\b/iu;
+
+  if (sqlKeywords.test(trimmed)) {

Review Comment:
   Would it might be better to match more than a single keyword, as `SELECT` 
will in almost all cases will be followed by a `FROM` and otherwise `DROP` will 
also usually be followed by a `TABLE`.



##########
airflow-core/src/airflow/ui/src/pages/TaskInstance/RenderedTemplates.tsx:
##########
@@ -16,14 +16,75 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-import { Box, HStack, Table, Code } from "@chakra-ui/react";
+import { Box, HStack, Table } from "@chakra-ui/react";
 import { useParams } from "react-router-dom";
+import { PrismLight as SyntaxHighlighter } from "react-syntax-highlighter";
+import bash from "react-syntax-highlighter/dist/esm/languages/prism/bash";
+import json from "react-syntax-highlighter/dist/esm/languages/prism/json";
+import sql from "react-syntax-highlighter/dist/esm/languages/prism/sql";
+import yaml from "react-syntax-highlighter/dist/esm/languages/prism/yaml";
+import { oneLight, oneDark } from 
"react-syntax-highlighter/dist/esm/styles/prism";
 
 import { useTaskInstanceServiceGetMappedTaskInstance } from "openapi/queries";
 import { ClipboardRoot, ClipboardIconButton } from "src/components/ui";
+import { useColorMode } from "src/context/colorMode";
+
+SyntaxHighlighter.registerLanguage("json", json);
+SyntaxHighlighter.registerLanguage("yaml", yaml);
+SyntaxHighlighter.registerLanguage("sql", sql);
+SyntaxHighlighter.registerLanguage("bash", bash);
+
+const detectLanguage = (value: string): string => {
+  const trimmed = value.trim();
+
+  // Try to detect JSON
+  if (
+    (trimmed.startsWith("{") && trimmed.endsWith("}")) ||
+    (trimmed.startsWith("[") && trimmed.endsWith("]"))
+  ) {
+    try {
+      JSON.parse(trimmed);
+
+      return "json";
+    } catch {
+      // Not valid JSON, continue
+    }
+  }
+
+  // Try to detect YAML (basic heuristics)
+  if (trimmed.includes(":") && (trimmed.includes("\n") || trimmed.includes("- 
"))) {

Review Comment:
   Detection safety:
   - If a colon is matched it should be followed by a newline (`":\n"`)
   - Should we not rather use a lib like https://www.npmjs.com/package/yaml to 
test parsing after some basic heuristics before making a decision (similar to 
JSON)?



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to