Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

3.3 KB
Raw Blame
---
type Props = {
  title?: string;
};

const { title = '' } = Astro.props as Props;
---

<script>
  import mermaid from 'mermaid';

  /**
   * Resolves a docs color token to an sRGB hex color. Mermaid derives shades from its theme colors and cannot parse
   * the OKLCH values the tokens hold, so let a canvas convert them.
   */
  const resolveColor = (token: string) => {
    const context = document.createElement('canvas').getContext('2d', { willReadFrequently: true })!;
    context.fillStyle = getComputedStyle(document.documentElement).getPropertyValue(token).trim();
    context.fillRect(0, 0, 1, 1);
    const [r, g, b] = context.getImageData(0, 0, 1, 1).data;
    return `#${[r, g, b].map((channel) => channel.toString(16).padStart(2, '0')).join('')}`;
  };

  const getThemeVariables = () => {
    const surface = resolveColor('--invoke-neutral-900');
    const control = resolveColor('--invoke-neutral-800');
    const border = resolveColor('--invoke-neutral-700');
    const text = resolveColor('--invoke-neutral-50');
    return {
      darkMode: true,
      fontFamily: getComputedStyle(document.body).fontFamily,
      background: resolveColor('--invoke-neutral-950'),
      primaryColor: control,
      primaryTextColor: text,
      primaryBorderColor: resolveColor('--invoke-accent'),
      secondaryColor: surface,
      tertiaryColor: surface,
      lineColor: resolveColor('--invoke-neutral-300'),
      textColor: text,
      clusterBkg: surface,
      clusterBorder: border,
      titleColor: resolveColor('--invoke-neutral-200'),
      edgeLabelBackground: surface,
    };
  };

  function extractAndCleanMermaidCode() {
    const mermaidElements = document.querySelectorAll('figure.expandable-diagram');
    mermaidElements.forEach((element) => {
      // Find the code element within the complex structure
      const codeElement = element.querySelector('pre[data-language="mermaid"] code');
      if (!codeElement) return;

      // Extract the text content from each line div
      const codeLines = codeElement.querySelectorAll('.ec-line .code');
      let cleanedCode = Array.from(codeLines)
        .map((line) => line.textContent.trim())
        .join('\n');

      // Remove any leading/trailing whitespace
      cleanedCode = cleanedCode.trim();

      // Create a new pre element with just the cleaned code
      const newPreElement = document.createElement('pre');
      newPreElement.className = 'mermaid not-prose';
      newPreElement.textContent = cleanedCode;

      // Find the diagram content container
      const diagramContentContainer = element.querySelector('.diagram-content');

      // Replace existing diagram content child with the new pre element
      diagramContentContainer?.replaceChild(newPreElement, diagramContentContainer.firstChild);
    });
  }

  // Wait for the DOM to be fully loaded
  document.addEventListener('DOMContentLoaded', async () => {
    extractAndCleanMermaidCode();
    mermaid.initialize({ startOnLoad: false, theme: 'base', themeVariables: getThemeVariables() });
    await mermaid.run({ querySelector: 'figure.expandable-diagram pre.mermaid' });
  });
</script>

<figure class="expandable-diagram">
  <figcaption>{title}</figcaption>

  <div class="diagram-content">{Astro.locals.t('mermaid.loading')}</div>

  <details>
    <summary>{Astro.locals.t('mermaid.sourceCode')}</summary>
    <slot />
  </details>
</figure>