Highlighter

The Highlighter component is used to display syntax-highlighted code blocks. It takes in the code content as a string and the language of the code as a string. The component allows users to copy the code content and can also display the language tag. The code block can have a background and the theme can be set to either 'dark' or 'light'.

import { Highlighter } from '@lobehub/ui';

Default

tsx
import { useTheme } from 'antd-style';
import mermaid from 'mermaid';
import { useCallback, useEffect, useState } from 'react';

export const useMermaid = (content: string) => {
  const [mermaidContent, setMermaidContent] = useState<string>();
  const theme = useTheme();
  useEffect(() => {
    mermaid.initialize({
      fontFamily: theme.fontFamilyCode,
      gantt: {
        useWidth: 1920,
      },
      securityLevel: 'loose',
      startOnLoad: true,
      theme: theme.isDarkMode ? 'dark' : 'default',
      themeVariables: {
        errorBkgColor: theme.colorError,
        errorTextColor: theme.colorText,
        fontSize: 14,
        lineColor: theme.colorText,
        primaryBorderColor: theme.colorPrimaryBorder,
        primaryColor: theme.colorPrimaryBg,
        primaryTextColor: theme.colorText,
        secondaryColor: theme.colorInfo,
        tertiaryColor: theme.colorSuccess,
      },
    });
    mermaid.contentLoaded();
  }, [mermaidContent, theme.isDarkMode]);

  const checkSyntax = async (textStr: string) => {
    try {
      if (await mermaid.parse(textStr)) {
        setMermaidContent(textStr);
      }
    } catch {}
  };

  useEffect(() => {
    checkSyntax(content);
  }, [content]);

  return useCallback(() => {
    return (
      <pre
        className={'mermaid'}
        style={{
          alignItems: 'center',
          display: 'flex',
          fontSize: 14,
          justifyContent: 'center',
          overflow: 'auto',
        }}
      >
        {mermaidContent}
      </pre>
    );
  }, [mermaidContent, theme.isDarkMode]);
};
tsx
import { useTheme } from 'antd-style';
import mermaid from 'mermaid';
import { useCallback, useEffect, useState } from 'react';

export const useMermaid = (content: string) => {
  const [mermaidContent, setMermaidContent] = useState<string>();
  const theme = useTheme();
  useEffect(() => {
    mermaid.initialize({
      fontFamily: theme.fontFamilyCode,
      gantt: {
        useWidth: 1920,
      },
      securityLevel: 'loose',
      startOnLoad: true,
      theme: theme.isDarkMode ? 'dark' : 'default',
      themeVariables: {
        errorBkgColor: theme.colorError,
        errorTextColor: theme.colorText,
        fontSize: 14,
        lineColor: theme.colorText,
        primaryBorderColor: theme.colorPrimaryBorder,
        primaryColor: theme.colorPrimaryBg,
        primaryTextColor: theme.colorText,
        secondaryColor: theme.colorInfo,
        tertiaryColor: theme.colorSuccess,
      },
    });
    mermaid.contentLoaded();
  }, [mermaidContent, theme.isDarkMode]);

  const checkSyntax = async (textStr: string) => {
    try {
      if (await mermaid.parse(textStr)) {
        setMermaidContent(textStr);
      }
    } catch {}
  };

  useEffect(() => {
    checkSyntax(content);
  }, [content]);

  return useCallback(() => {
    return (
      <pre
        className={'mermaid'}
        style={{
          alignItems: 'center',
          display: 'flex',
          fontSize: 14,
          justifyContent: 'center',
          overflow: 'auto',
        }}
      >
        {mermaidContent}
      </pre>
    );
  }, [mermaidContent, theme.isDarkMode]);
};

Actions Render

tsx
import { useTheme } from 'antd-style';
import mermaid from 'mermaid';
import { useCallback, useEffect, useState } from 'react';

export const useMermaid = (content: string) => {
  const [mermaidContent, setMermaidContent] = useState<string>();
  const theme = useTheme();
  useEffect(() => {
    mermaid.initialize({
      fontFamily: theme.fontFamilyCode,
      gantt: {
        useWidth: 1920,
      },
      securityLevel: 'loose',
      startOnLoad: true,
      theme: theme.isDarkMode ? 'dark' : 'default',
      themeVariables: {
        errorBkgColor: theme.colorError,
        errorTextColor: theme.colorText,
        fontSize: 14,
        lineColor: theme.colorText,
        primaryBorderColor: theme.colorPrimaryBorder,
        primaryColor: theme.colorPrimaryBg,
        primaryTextColor: theme.colorText,
        secondaryColor: theme.colorInfo,
        tertiaryColor: theme.colorSuccess,
      },
    });
    mermaid.contentLoaded();
  }, [mermaidContent, theme.isDarkMode]);

  const checkSyntax = async (textStr: string) => {
    try {
      if (await mermaid.parse(textStr)) {
        setMermaidContent(textStr);
      }
    } catch {}
  };

  useEffect(() => {
    checkSyntax(content);
  }, [content]);

  return useCallback(() => {
    return (
      <pre
        className={'mermaid'}
        style={{
          alignItems: 'center',
          display: 'flex',
          fontSize: 14,
          justifyContent: 'center',
          overflow: 'auto',
        }}
      >
        {mermaidContent}
      </pre>
    );
  }, [mermaidContent, theme.isDarkMode]);
};

APIs

NameDescriptionTypeDefault
actionsRender--(props:{ actionIconSize: any; content: string; language: string; originalNode: React.ReactNode; })=>ReactNode--
allowChangeLanguage--boolean--
bodyRender--(props:{ content: string; language: string; originalNode: React.ReactNode; })=>ReactNode--
childrenThe code content to be highlightedstring(required)
copyButtonSize--any--
copyableWhether to show the copy buttonbooleantrue
defalutExpandWhether to expand code blocks by defaultbooleantrue
enableTransformer--boolean--
fileName--string--
fullFeatured--boolean--
icon--ReactNode--
languageThe language of the code contentstring(required)
showLanguageWhether to show language tagbooleantrue
spotlightWhether add spotlight backgroundbooleanfalse
typeThe type of the code block"ghost"|"block"|"pure""block"
wrap--boolean--