diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-01-26 12:31:37 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-01-26 12:31:37 +0100 |
| commit | a09d6f1639235e668a7dd0dee5392ab1cc16dd8c (patch) | |
| tree | db93748c2c7c34874adf24df9f3e2f1c96b29175 /src/components | |
| parent | 1ecc8089dd1f90ee20064cfda39855fc8805e9cf (diff) | |
chore: create a CodeBlock component for MDX rendering
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/MDX/CodeBlock/CodeBlock.tsx | 38 | ||||
| -rw-r--r-- | src/components/MDX/index.tsx | 3 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/components/MDX/CodeBlock/CodeBlock.tsx b/src/components/MDX/CodeBlock/CodeBlock.tsx new file mode 100644 index 0000000..ef8b587 --- /dev/null +++ b/src/components/MDX/CodeBlock/CodeBlock.tsx @@ -0,0 +1,38 @@ +import { config } from '@config/website'; +import { translateCopyButton } from '@utils/helpers/prism'; +import { useRouter } from 'next/router'; +import Prism from 'prismjs'; +import { ReactChildren, useEffect } from 'react'; + +const CodeBlock = ({ + className, + children, +}: { + className: string; + children: ReactChildren; +}) => { + const classNames = className.split('+'); + const languageClass = classNames.find((name: string) => + name.startsWith('language-') + ); + const router = useRouter(); + const locale = router.locale ? router.locale : config.locales.defaultLocale; + + useEffect(() => { + Prism.highlightAll(); + }); + + useEffect(() => { + translateCopyButton(locale); + }, [locale]); + + return ( + <div> + <pre className={classNames.join(' ')}> + <code className={languageClass}>{children}</code> + </pre> + </div> + ); +}; + +export default CodeBlock; diff --git a/src/components/MDX/index.tsx b/src/components/MDX/index.tsx new file mode 100644 index 0000000..d306f4e --- /dev/null +++ b/src/components/MDX/index.tsx @@ -0,0 +1,3 @@ +import CodeBlock from './CodeBlock/CodeBlock'; + +export { CodeBlock }; |
