Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

3.7 KB
Raw Blame
import PropTypes from "prop-types";
import { useMemo } from "react";
import { Link as RouterLink } from "react-router-dom";
import { Link, Paragraph, Text } from "@/components/ui/shims/antd-typography";

import { isSafeExternalUrl } from "../../../helpers/urlSafety";
import { useSessionStore } from "../../../store/session-store";

const CustomMarkdown = ({
  text = "",
  renderNewLines = true,
  isSecondary = false,
  styleClassName,
}) => {
  const { sessionDetails } = useSessionStore();
  const orgName = sessionDetails?.orgName;
  const textType = isSecondary ? "secondary" : undefined;

  /*
    Patterns with bounded quantifiers to prevent performance issues:
    - Triple code: ```...```
    - Inline code: `...`
    - Bold: **...**
    - Link: [text](url)
    - New line: \n
  */
  const patterns = [
    { type: "tripleCode", regex: /```\n([\s\S]*?)\n```/g },
    { type: "inlineCode", regex: /`([^`]{1,100})`/g },
    { type: "bold", regex: /\*\*([^*]{1,100})\*\*/g },
    { type: "link", regex: /\[([^\]]{1,100})\]\(([^)]{1,200})\)/g },
    { type: "newline", regex: /\n/g },
  ];

  const renderToken = (type, content, url) => {
    switch (type) {
      case "tripleCode":
        return (
          <Paragraph style={{ margin: 0 }}>
            <pre
              style={{
                margin: 0,
                whiteSpace: "pre-wrap",
                overflowWrap: "anywhere",
              }}
            >
              {content}
            </pre>
          </Paragraph>
        );
      case "inlineCode":
        return (
          <Text code type={textType}>
            {content}
          </Text>
        );
      case "bold":
        return (
          <Text strong type={textType}>
            {content}
          </Text>
        );
      case "link": {
        // Exclude protocol-relative `//evil.com` from internal-route branch.
        const isInternal = url?.startsWith("/") && !url.startsWith("//");
        if (isInternal) {
          const resolvedUrl = orgName ? `/${orgName}${url}` : url;
          return <RouterLink to={resolvedUrl}>{content}</RouterLink>;
        }
        if (!isSafeExternalUrl(url)) {
          return content;
        }
        return (
          <Link href={url} target="_blank" rel="noopener noreferrer">
            {content}
          </Link>
        );
      }
      case "newline":
        return renderNewLines ? <br /> : "\n";
      default:
        return content;
    }
  };

  const content = useMemo(() => {
    let elements = [text];

    // Process each pattern sequentially
    patterns.forEach(({ type, regex }) => {
      const newElements = [];
      elements.forEach((element) => {
        if (typeof element !== "string") {
          newElements.push(element);
          return;
        }

        let lastIndex = 0;
        let match;
        while ((match = regex.exec(element)) !== null) {
          const matchIndex = match.index;
          if (matchIndex > lastIndex) {
            newElements.push(element.substring(lastIndex, matchIndex));
          }
          const url = match[2]; // Relevant only for link
          newElements.push(renderToken(type, match[1], url));
          lastIndex = matchIndex + match[0].length;
        }

        if (lastIndex < element.length) {
          newElements.push(element.substring(lastIndex));
        }
      });
      elements = newElements;
    });

    return elements;
  }, [text, renderNewLines, textType, orgName]);

  return (
    <Text type={textType} className={styleClassName}>
      {content}
    </Text>
  );
};

CustomMarkdown.propTypes = {
  text: PropTypes.string,
  renderNewLines: PropTypes.bool,
  isSecondary: PropTypes.bool,
  styleClassName: PropTypes.string,
};

export default CustomMarkdown;