add `code` blocks styling

This commit is contained in:
Paul Wackerow 2022-11-21 15:26:37 -08:00
parent ebdb7a852e
commit cfc24e4bf8
No known key found for this signature in database
GPG Key ID: BB63E296FE9CAB8D
2 changed files with 99 additions and 31 deletions

View File

@ -1,7 +1,7 @@
import { Heading, Link, Stack, Text } 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 { nightOwl } from 'react-syntax-highlighter/dist/cjs/styles/prism'; import { 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';
@ -15,8 +15,11 @@ import solidity from 'react-syntax-highlighter/dist/cjs/languages/prism/solidity
import swift from 'react-syntax-highlighter/dist/cjs/languages/prism/swift'; import swift from 'react-syntax-highlighter/dist/cjs/languages/prism/swift';
import { textStyles } from '../theme/foundations'; import { textStyles } from '../theme/foundations';
import { getProgrammingLanguageName } from '../utils';
// syntax highlighting languages supported // syntax highlighting languages supported
SyntaxHighlighter.registerLanguage('bash', bash); SyntaxHighlighter.registerLanguage('bash', bash);
SyntaxHighlighter.registerLanguage('terminal', bash);
SyntaxHighlighter.registerLanguage('go', go); SyntaxHighlighter.registerLanguage('go', go);
SyntaxHighlighter.registerLanguage('graphql', graphql); SyntaxHighlighter.registerLanguage('graphql', graphql);
SyntaxHighlighter.registerLanguage('java', java); SyntaxHighlighter.registerLanguage('java', java);
@ -27,7 +30,7 @@ SyntaxHighlighter.registerLanguage('sh', sh);
SyntaxHighlighter.registerLanguage('solidity', solidity); SyntaxHighlighter.registerLanguage('solidity', solidity);
SyntaxHighlighter.registerLanguage('swift', swift); SyntaxHighlighter.registerLanguage('swift', swift);
import { getProgrammingLanguageName } from '../utils';
const { header1, header2, header3, header4 } = textStyles const { header1, header2, header3, header4 } = textStyles
const MDXComponents = { const MDXComponents = {
@ -81,6 +84,39 @@ const MDXComponents = {
</Heading> </Heading>
); );
}, },
// 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
table: ({ children }: any) => {
return (
<Flex maxW='100vw' overflowX='scroll'>
<Table
variant='striped'
colorScheme='greenAlpha'
border='1px'
borderColor='blackAlpha.50'
mb={10}
size={{ base: 'sm', lg: 'md' }}
w='auto'
>
{children}
</Table>
</Flex>
);
},
// pre // pre
pre: ({ children }: any) => { pre: ({ children }: any) => {
return ( return (
@ -88,30 +124,49 @@ const MDXComponents = {
<pre>{children}</pre> <pre>{children}</pre>
</Stack> </Stack>
); );
} },
// code // code
// code: (code: any) => { code: ({ className, ...code }: any) => {
// const language = getProgrammingLanguageName(code); const { colorMode } = useColorMode();
const isDark = colorMode === 'dark';
// return !!code.inline ? ( if (className?.includes("terminal")) return (
// <Text <SyntaxHighlighter
// as={'span'} language='bash'
// padding='0.125em 0.25em' style={nightOwl}
// color='red.300' customStyle={{ borderRadius: '0.5rem', padding: '1rem' }}
// background='code-bg-contrast' >
// borderRadius='0.25em' {code.children}
// fontFamily='code' </SyntaxHighlighter>
// fontSize='sm' )
// overflowY='scroll' if (code.inline) return (
// > <Text
// {code.children[0]} as={'span'}
// </Text> padding='0.125em 0.25em'
// ) : ( color='primary'
// <Stack style={nightOwl}> background='code-bg'
// {code.children[0]} borderRadius='0.25em'
// </Stack> fontFamily='code'
// ); fontSize='sm'
// } overflowY='scroll'
>
{code.children[0]}
</Text>
)
if (className?.startsWith('language-')) return (
<SyntaxHighlighter
language='json'
style={isDark ? materialDark : materialLight} // TODO: Update with code light/dark color themes
customStyle={{ borderRadius: '0.5rem', padding: '1rem' }}
>
{code.children}
</SyntaxHighlighter>
)
return (
<Stack>
{code.children[0]}
</Stack>
);
}
}; };
export default MDXComponents; export default MDXComponents;

View File

@ -1,7 +1,20 @@
// WIP const CLASSNAME_PREFIX = 'language-';
export const getProgrammingLanguageName = (code: any) => { const DEFAULT = 'bash';
// const hasLanguageNameProperty = Object.keys(code.node.properties).length > 0; const JS = ['javascript', 'js', 'jsx', 'ts', 'tsx'];
console.log({ code }); const SH = ['sh', 'shell'];
const PY = ['python', 'py'];
const SOL = ['solidity', 'sol'];
const LANGS = [JS, SH, PY, SOL];
// return hasLanguageNameProperty ? code.node.properties.className[0].split('-')[1] : 'bash'; export const getProgrammingLanguageName = (code: string) => {
for (const lang of LANGS) {
if (lang.includes(code.toLowerCase())) return lang[0];
}
const hasLanguageNameProperty = code.startsWith(CLASSNAME_PREFIX);
if (!hasLanguageNameProperty) return DEFAULT;
const newCode = code.replace(CLASSNAME_PREFIX, '').toLowerCase();
for (const lang of LANGS) {
if (lang.includes(code.toLowerCase())) return lang[0];
}
return newCode;
}; };