diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx index 7f8ea7642c2a..f734ca0b15da 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx @@ -16,6 +16,7 @@ import { containsLineNumbers, ThemeClassNames, usePrismTheme, + getPrismCssVariables, } from '@docusaurus/theme-common'; import CopyButton from '@theme/CodeBlock/CopyButton'; import type {Props} from '@theme/CodeBlock'; @@ -50,6 +51,8 @@ export default function CodeBlock({ const codeBlockTitle = parseCodeBlockTitle(metastring) || title; const prismTheme = usePrismTheme(); + const prismCssVariables = getPrismCssVariables(prismTheme); + //
tags in markdown map to CodeBlocks and they may contain JSX children.
// When the children is not a simple string, we just return a styled block
// without actually highlighting.
@@ -72,7 +75,8 @@ export default function CodeBlock({
styles.codeBlockContainer,
blockClassName,
ThemeClassNames.common.codeBlock,
- )}>
+ )}
+ style={prismCssVariables}>
{children}
)}
@@ -108,7 +112,8 @@ export default function CodeBlock({
language && !blockClassName.includes(`language-${language}`),
},
ThemeClassNames.common.codeBlock,
- )}>
+ )}
+ style={prismCssVariables}>
{codeBlockTitle && (
["'])(?.*?)\1/; const highlightLinesRangeRegex = /\{(? [\d,-]+)\}/; @@ -173,3 +175,20 @@ export function parseLines( code = lines.join('\n'); return {highlightLines, code}; } + +export function getPrismCssVariables(prismTheme: PrismTheme): CSSProperties { + const mapping: {[name: keyof PrismTheme['plain']]: string} = { + color: '--prism-color', + backgroundColor: '--prism-background-color', + }; + + const properties: CSSProperties = {}; + Object.entries(prismTheme.plain).forEach(([key, value]) => { + const varName = mapping[key]; + if (varName && typeof value === 'string') { + // @ts-expect-error: why css variables not in inline style type? + properties[varName] = value; + } + }); + return properties; +}