update code blocks

pull/26459/head^2
Paul Wackerow 2 years ago
parent 46c2aa3cea
commit f6962d5b85
No known key found for this signature in database
GPG Key ID: BB63E296FE9CAB8D
  1. 44
      src/components/MDXComponents.tsx
  2. 2
      src/utils/getProgrammingLanguageName.ts

@ -1,11 +1,7 @@
import { Flex, Heading, Link, Stack, Table, Text, useColorMode } from '@chakra-ui/react'; import { Flex, Heading, Link, Stack, Table, Text, useColorMode } from '@chakra-ui/react';
import NextLink from 'next/link'; import NextLink from 'next/link';
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter'; import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import { import { nightOwl, prism } from 'react-syntax-highlighter/dist/cjs/styles/prism';
nightOwl,
materialLight,
materialDark
} from 'react-syntax-highlighter/dist/cjs/styles/prism';
import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash'; import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash';
import go from 'react-syntax-highlighter/dist/cjs/languages/prism/go'; import go from 'react-syntax-highlighter/dist/cjs/languages/prism/go';
@ -36,19 +32,10 @@ SyntaxHighlighter.registerLanguage('swift', swift);
const { header1, header2, header3, header4 } = textStyles; const { header1, header2, header3, header4 } = textStyles;
const Code = ({ className, ...code }: any) => { const Code = ({ className, children, ...code }: any) => {
const { colorMode } = useColorMode(); const { colorMode } = useColorMode();
const isDark = colorMode === 'dark'; const isDark = colorMode === 'dark';
if (className?.includes('terminal')) const isTerminal = className?.includes('terminal') ;
return (
<SyntaxHighlighter
language='bash'
style={nightOwl}
customStyle={{ borderRadius: '0.5rem', padding: '1rem' }}
>
{code.children}
</SyntaxHighlighter>
);
if (code.inline) if (code.inline)
return ( return (
<Text <Text
@ -61,20 +48,21 @@ const Code = ({ className, ...code }: any) => {
fontSize='sm' fontSize='sm'
overflowY='scroll' overflowY='scroll'
> >
{code.children[0]} {children[0]}
</Text> </Text>
); );
if (className?.startsWith('language-')) if (className?.startsWith('language-')) {
return ( return (
<SyntaxHighlighter <SyntaxHighlighter
language={getProgrammingLanguageName(className)} language={getProgrammingLanguageName(className)}
style={isDark ? materialDark : materialLight} // TODO: Update with code light/dark color themes style={isTerminal || isDark ? nightOwl : prism} // TODO: Update with code light/dark color themes
customStyle={{ borderRadius: '0.5rem', padding: '1rem' }} customStyle={{ borderRadius: '0.5rem', padding: '1rem' }}
> >
{code.children} {children}
</SyntaxHighlighter> </SyntaxHighlighter>
); );
return <Stack>{code.children[0]}</Stack>; }
return <Stack>{children[0]}</Stack>;
}; };
const MDXComponents = { const MDXComponents = {
@ -129,20 +117,6 @@ const MDXComponents = {
); );
}, },
// lists // lists
ul: ({ children }: any) => {
return (
<Stack as='ul' spacing={2} mb={7} _last={{ mb: 0 }}>
{children}
</Stack>
);
},
ol: ({ children }: any) => {
return (
<Stack as='ol' spacing={2} mb={7} _last={{ mb: 0 }}>
{children}
</Stack>
);
},
// tables // tables
table: ({ children }: any) => ( table: ({ children }: any) => (
<Flex maxW='min(100%, 100vw)' overflowX='scroll'> <Flex maxW='min(100%, 100vw)' overflowX='scroll'>

@ -1,5 +1,6 @@
const CLASSNAME_PREFIX = 'language-'; const CLASSNAME_PREFIX = 'language-';
const DEFAULT = 'bash'; const DEFAULT = 'bash';
const TERMINAL = 'terminal';
const JS = ['javascript', 'js', 'jsx', 'ts', 'tsx']; const JS = ['javascript', 'js', 'jsx', 'ts', 'tsx'];
const SH = ['sh', 'shell']; const SH = ['sh', 'shell'];
const PY = ['python', 'py']; const PY = ['python', 'py'];
@ -13,6 +14,7 @@ export const getProgrammingLanguageName = (code: string) => {
const hasLanguageNameProperty = code.startsWith(CLASSNAME_PREFIX); const hasLanguageNameProperty = code.startsWith(CLASSNAME_PREFIX);
if (!hasLanguageNameProperty) return DEFAULT; if (!hasLanguageNameProperty) return DEFAULT;
const newCode = code.replace(CLASSNAME_PREFIX, '').toLowerCase(); const newCode = code.replace(CLASSNAME_PREFIX, '').toLowerCase();
if (newCode === TERMINAL) return DEFAULT;
for (const lang of LANGS) { for (const lang of LANGS) {
if (lang.includes(code.toLowerCase())) return lang[0]; if (lang.includes(code.toLowerCase())) return lang[0];
} }

Loading…
Cancel
Save