CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-docusaurus--theme-mermaid

Mermaid diagram rendering components for Docusaurus websites with theme integration and error handling

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

client-usage.mddocs/

Client Usage

Client-side hooks and utilities for rendering Mermaid diagrams with theme integration and configuration management.

Capabilities

Mermaid Configuration Hook

Returns the current Mermaid configuration with theme applied based on color mode.

/**
 * Returns the current Mermaid configuration with theme applied based on color mode
 * @returns MermaidConfig object with theme and options merged
 */
function useMermaidConfig(): MermaidConfig;

interface MermaidConfig {
  startOnLoad: boolean;
  theme: string;
  [key: string]: any;
}

Usage Example:

import { useMermaidConfig } from "@docusaurus/theme-mermaid/client";

function MyDiagramComponent() {
  const config = useMermaidConfig();
  // config.theme will be 'default' or 'dark' based on current color mode
  // config.startOnLoad will be false by default
  // Additional options from themeConfig.mermaid.options are merged
}

Theme Configuration Hook

Returns the Mermaid-specific theme configuration from Docusaurus theme config.

/**
 * Returns the Mermaid-specific theme configuration from Docusaurus theme config
 * @returns Mermaid theme configuration object
 */
function useMermaidThemeConfig(): ThemeConfig['mermaid'];

interface MermaidThemeConfigResult {
  theme: {
    light: string;
    dark: string;
  };
  options: Record<string, any>;
}

Mermaid Render Hook

Renders Mermaid text to SVG and handles errors with optional custom configuration.

/**
 * Renders Mermaid text to SVG and handles errors with optional custom configuration
 * @param params - Configuration object with text and optional config
 * @returns RenderResult object or null if rendering is in progress
 */
function useMermaidRenderResult(params: {
  text: string;
  config?: MermaidConfig;
}): RenderResult | null;

interface RenderResult {
  svg: string;
  bindFunctions?: (element: Element) => void;
}

Usage Example:

import { useMermaidRenderResult } from "@docusaurus/theme-mermaid/client";

function CustomMermaidRenderer({ diagramText }: { diagramText: string }) {
  const renderResult = useMermaidRenderResult({ text: diagramText });
  
  if (!renderResult) {
    return <div>Loading diagram...</div>;
  }
  
  return (
    <div 
      dangerouslySetInnerHTML={{ __html: renderResult.svg }}
      onLoad={() => renderResult.bindFunctions?.(/* element */)}
    />
  );
}

Container Class Name

Stable CSS class name for targeting Mermaid containers with custom styles.

/**
 * Stable CSS class name for targeting Mermaid containers with custom styles
 */
const MermaidContainerClassName: string; // "docusaurus-mermaid-container"

Usage Example:

import { MermaidContainerClassName } from "@docusaurus/theme-mermaid/client";

// In your CSS
const styles = `
  .${MermaidContainerClassName} {
    border: 1px solid var(--ifm-color-emphasis-300);
    border-radius: 8px;
    padding: 1rem;
  }
`;

Error Handling

The useMermaidRenderResult hook handles errors by triggering the parent React error boundary:

  • Mermaid parsing errors: Invalid diagram syntax throws errors caught by error boundaries
  • DOM cleanup: Automatically removes failed SVG elements from DOM
  • Async rendering: Returns null while rendering is in progress
  • Error propagation: Uses React's error boundary pattern for error handling

Error Boundary Usage:

import React from "react";
import ErrorBoundary from "@docusaurus/ErrorBoundary";
import { useMermaidRenderResult } from "@docusaurus/theme-mermaid/client";

function SafeMermaidRenderer({ text }: { text: string }) {
  return (
    <ErrorBoundary fallback={<div>Failed to render diagram</div>}>
      <MermaidContent text={text} />
    </ErrorBoundary>
  );
}

function MermaidContent({ text }: { text: string }) {
  const result = useMermaidRenderResult({ text });
  return result ? <div dangerouslySetInnerHTML={{ __html: result.svg }} /> : null;
}

Install with Tessl CLI

npx tessl i tessl/npm-docusaurus--theme-mermaid

docs

client-usage.md

components.md

index.md

plugin-setup.md

tile.json