|
| 1 | +import React, {type ReactNode} from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import {useThemeConfig, usePrismTheme} from '@docusaurus/theme-common'; |
| 4 | +import { |
| 5 | + parseCodeBlockTitle, |
| 6 | + parseLanguage, |
| 7 | + parseLines, |
| 8 | + containsLineNumbers, |
| 9 | + useCodeWordWrap, |
| 10 | +} from '@docusaurus/theme-common/internal'; |
| 11 | +import {Highlight, type Language} from 'prism-react-renderer'; |
| 12 | +import Line from '@theme/CodeBlock/Line'; |
| 13 | +import CopyButton from '@theme/CodeBlock/CopyButton'; |
| 14 | +import WordWrapButton from '@theme/CodeBlock/WordWrapButton'; |
| 15 | +import Container from '@theme/CodeBlock/Container'; |
| 16 | +import type {Props} from '@theme/CodeBlock/Content/String'; |
| 17 | + |
| 18 | +import styles from './styles.module.css'; |
| 19 | + |
| 20 | +// Prism languages are always lowercase |
| 21 | +// We want to fail-safe and allow both "php" and "PHP" |
| 22 | +// See https://github.com/facebook/docusaurus/issues/9012 |
| 23 | +function normalizeLanguage(language: string | undefined): string | undefined { |
| 24 | + return language?.toLowerCase(); |
| 25 | +} |
| 26 | + |
| 27 | +export default function CodeBlockString({ |
| 28 | + children, |
| 29 | + className: blockClassName = '', |
| 30 | + metastring, |
| 31 | + title: titleProp, |
| 32 | + showLineNumbers: showLineNumbersProp, |
| 33 | + language: languageProp, |
| 34 | +}: Props): ReactNode { |
| 35 | + const { |
| 36 | + prism: {defaultLanguage, magicComments}, |
| 37 | + } = useThemeConfig(); |
| 38 | + const language = normalizeLanguage( |
| 39 | + languageProp ?? parseLanguage(blockClassName) ?? defaultLanguage, |
| 40 | + ); |
| 41 | + |
| 42 | + const prismTheme = usePrismTheme(); |
| 43 | + const wordWrap = useCodeWordWrap(); |
| 44 | + |
| 45 | + // We still parse the metastring in case we want to support more syntax in the |
| 46 | + // future. Note that MDX doesn't strip quotes when parsing metastring: |
| 47 | + // "title=\"xyz\"" => title: "\"xyz\"" |
| 48 | + const title = parseCodeBlockTitle(metastring) || titleProp; |
| 49 | + |
| 50 | + const {lineClassNames, code} = parseLines(children, { |
| 51 | + metastring, |
| 52 | + language, |
| 53 | + magicComments, |
| 54 | + }); |
| 55 | + const showLineNumbers = |
| 56 | + showLineNumbersProp ?? containsLineNumbers(metastring); |
| 57 | + |
| 58 | + return ( |
| 59 | + <Container |
| 60 | + as="div" |
| 61 | + className={clsx( |
| 62 | + blockClassName, |
| 63 | + language && |
| 64 | + !blockClassName.includes(`language-${language}`) && |
| 65 | + `language-${language}`, |
| 66 | + )}> |
| 67 | + {title && <div className={styles.codeBlockTitle}>{title}</div>} |
| 68 | + <div className={styles.codeBlockContent}> |
| 69 | + <Highlight |
| 70 | + theme={prismTheme} |
| 71 | + code={code} |
| 72 | + language={(language ?? 'text') as Language}> |
| 73 | + {({className, style, tokens, getLineProps, getTokenProps}) => ( |
| 74 | + <pre |
| 75 | + /* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */ |
| 76 | + tabIndex={0} |
| 77 | + ref={wordWrap.codeBlockRef} |
| 78 | + className={clsx(className, styles.codeBlock, 'thin-scrollbar')} |
| 79 | + style={style}> |
| 80 | + <code |
| 81 | + className={clsx( |
| 82 | + styles.codeBlockLines, |
| 83 | + showLineNumbers && styles.codeBlockLinesWithNumbering, |
| 84 | + )}> |
| 85 | + {tokens.map((line, i) => ( |
| 86 | + <Line |
| 87 | + key={i} |
| 88 | + line={line} |
| 89 | + getLineProps={getLineProps} |
| 90 | + getTokenProps={getTokenProps} |
| 91 | + classNames={lineClassNames[i]} |
| 92 | + showLineNumbers={showLineNumbers} |
| 93 | + /> |
| 94 | + ))} |
| 95 | + </code> |
| 96 | + </pre> |
| 97 | + )} |
| 98 | + </Highlight> |
| 99 | + <div className={styles.buttonGroup}> |
| 100 | + {(wordWrap.isEnabled || wordWrap.isCodeScrollable) && ( |
| 101 | + <WordWrapButton |
| 102 | + className={styles.codeButton} |
| 103 | + onClick={() => wordWrap.toggle()} |
| 104 | + isEnabled={wordWrap.isEnabled} |
| 105 | + /> |
| 106 | + )} |
| 107 | + <CopyButton className={styles.codeButton} code={code} /> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </Container> |
| 111 | + ); |
| 112 | +} |
0 commit comments