summaryrefslogtreecommitdiffstats
path: root/src/components/MDX
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/MDX')
-rw-r--r--src/components/MDX/CodeBlock/CodeBlock.tsx38
-rw-r--r--src/components/MDX/index.tsx3
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 };