From 17b8a8e8236685d7cdf1c0c8b14a76e484ece9a7 Mon Sep 17 00:00:00 2001 From: Arswarog Date: Sat, 8 Aug 2026 06:04:59 +0300 Subject: [PATCH] =?UTF-8?q?feat(theme):=20Nord-=D1=82=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20Mermaid=20=D0=B4=D0=B8=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=BC=20=D0=B2=20=D1=82=D1=91=D0=BC=D0=BD=D0=BE?= =?UTF-8?q?=D0=BC=20=D1=80=D0=B5=D0=B6=D0=B8=D0=BC=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat(theme): Nord mermaid только в тёмном режиме через swizzle fix(theme): useMemo для config mermaid — убрать бесконечный ре-рендер --- docusaurus.config.ts | 6 +++ src/theme/Mermaid/index.tsx | 82 +++++++++++++++++++++++++++++ src/theme/Mermaid/styles.module.css | 7 +++ 3 files changed, 95 insertions(+) create mode 100644 src/theme/Mermaid/index.tsx create mode 100644 src/theme/Mermaid/styles.module.css diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 4e46379..b6f12f6 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -127,6 +127,12 @@ const config: Config = { theme: prismThemes.github, darkTheme: prismThemes.dracula, }, + mermaid: { + theme: { + light: 'default', + dark: 'base', + }, + }, } satisfies Preset.ThemeConfig, }; diff --git a/src/theme/Mermaid/index.tsx b/src/theme/Mermaid/index.tsx new file mode 100644 index 0000000..b291403 --- /dev/null +++ b/src/theme/Mermaid/index.tsx @@ -0,0 +1,82 @@ +import ErrorBoundary from '@docusaurus/ErrorBoundary'; +import { ErrorBoundaryErrorMessageFallback } from '@docusaurus/theme-common'; +import { useColorMode } from '@docusaurus/theme-common'; +import { + MermaidContainerClassName, + useMermaidConfig, + useMermaidRenderResult, +} from '@docusaurus/theme-mermaid/client'; +import type { Props } from '@theme/Mermaid'; +import type { RenderResult } from 'mermaid'; +import React, { useRef, useEffect, useMemo, type ReactNode } from 'react'; + +import styles from './styles.module.css'; + +const NORD_THEME_VARIABLES = { + background: '#2e3440', + mainBkg: '#3b4252', + primaryColor: '#5e81ac', + primaryTextColor: '#eceff4', + primaryBorderColor: '#88c0d0', + secondaryColor: '#434c5e', + secondaryTextColor: '#d8dee9', + secondaryBorderColor: '#4c566a', + tertiaryColor: '#3b4252', + tertiaryTextColor: '#d8dee9', + tertiaryBorderColor: '#4c566a', + lineColor: '#88c0d0', + edgeLabelBackground: '#434c5e', + textColor: '#eceff4', + titleColor: '#88c0d0', + nodeBorder: '#4c566a', + clusterBkg: '#434c5e', + clusterBorder: '#4c566a', +}; + +function MermaidRenderResult({ renderResult }: { renderResult: RenderResult }): ReactNode { + const ref = useRef(null); + + useEffect(() => { + const div = ref.current!; + + renderResult.bindFunctions?.(div); + }, [renderResult]); + + return ( +
+ ); +} + +function MermaidRenderer({ value }: Props): ReactNode { + const { colorMode } = useColorMode(); + const baseConfig = useMermaidConfig(); + + const config = useMemo( + () => + colorMode === 'dark' + ? { ...baseConfig, themeVariables: NORD_THEME_VARIABLES } + : baseConfig, + [colorMode, baseConfig], + ); + + const renderResult = useMermaidRenderResult({ text: value, config }); + + if (renderResult === null) { + return null; + } + + return ; +} + +export default function Mermaid(props: Props): ReactNode { + return ( + }> + + + ); +} diff --git a/src/theme/Mermaid/styles.module.css b/src/theme/Mermaid/styles.module.css new file mode 100644 index 0000000..097dff9 --- /dev/null +++ b/src/theme/Mermaid/styles.module.css @@ -0,0 +1,7 @@ +.container { + max-width: 100%; +} + +.container > svg { + max-width: 100%; +}