From 35a77aa74de9f0034db3289c4a0e570335875393 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 15 Apr 2022 10:37:51 +0800 Subject: [PATCH] refactor --- .../src/theme/CodeBlock/index.tsx | 57 +++++++++---------- .../src/utils/codeBlockUtils.ts | 3 +- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx index f77860afdea0..dbf000cb0aa3 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx @@ -118,10 +118,13 @@ function CodeBlockContainer({ as: As, ...props }: {as: T} & ComponentProps) { + const prismTheme = usePrismTheme(); + const prismCssVariables = getPrismCssVariables(prismTheme); return ( ({ // When the children is not a simple string, we just return a styled block // without actually highlighting. function CodeBlockJSX({children, className}: Props): JSX.Element { - const prismTheme = usePrismTheme(); - const prismCssVariables = getPrismCssVariables(prismTheme); return ( + className={clsx(styles.codeBlockStandalone, 'thin-scrollbar', className)}> {children} ); @@ -152,7 +152,7 @@ function CodeBlockString({ children, className: blockClassName = '', metastring, - title, + title: titleProp, showLineNumbers: showLineNumbersProp, language: languageProp, }: Omit & {children: string}): JSX.Element { @@ -162,35 +162,33 @@ function CodeBlockString({ const language = languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage; const prismTheme = usePrismTheme(); - const prismCssVariables = getPrismCssVariables(prismTheme); // We still parse the metastring in case we want to support more syntax in the // future. Note that MDX doesn't strip quotes when parsing metastring: // "title=\"xyz\"" => title: "\"xyz\"" - const codeBlockTitle = parseCodeBlockTitle(metastring) || title; + const title = parseCodeBlockTitle(metastring) || titleProp; const {highlightLines, code} = parseLines(children, metastring, language); const showLineNumbers = showLineNumbersProp || containsLineNumbers(metastring); return ( - - {({className, tokens, getLineProps, getTokenProps}) => ( - - {codeBlockTitle && ( -
{codeBlockTitle}
- )} -
+ + {title &&
{title}
} +
+ + {({className, tokens, getLineProps, getTokenProps}) => (
             
- - -
-
- )} - + )} + + +
+
); } diff --git a/packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts b/packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts index 0dd49cfb2bde..8dd5108d93f1 100644 --- a/packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts +++ b/packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts @@ -182,11 +182,10 @@ export function getPrismCssVariables(prismTheme: PrismTheme): CSSProperties { backgroundColor: '--prism-background-color', }; - const properties: CSSProperties = {}; + const properties: {[key: string]: string} = {}; 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; } });