From cfc24e4bf8ba6b39d381b537470336822710bb3d Mon Sep 17 00:00:00 2001 From: Paul Wackerow <54227730+wackerow@users.noreply.github.com> Date: Mon, 21 Nov 2022 15:26:37 -0800 Subject: [PATCH] add `code` blocks styling --- src/components/MDXComponents.tsx | 107 ++++++++++++++++++------ src/utils/getProgrammingLanguageName.ts | 23 +++-- 2 files changed, 99 insertions(+), 31 deletions(-) diff --git a/src/components/MDXComponents.tsx b/src/components/MDXComponents.tsx index ed8f87144b..493d47f124 100644 --- a/src/components/MDXComponents.tsx +++ b/src/components/MDXComponents.tsx @@ -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 { 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 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 { textStyles } from '../theme/foundations'; +import { getProgrammingLanguageName } from '../utils'; + // syntax highlighting languages supported SyntaxHighlighter.registerLanguage('bash', bash); +SyntaxHighlighter.registerLanguage('terminal', bash); SyntaxHighlighter.registerLanguage('go', go); SyntaxHighlighter.registerLanguage('graphql', graphql); SyntaxHighlighter.registerLanguage('java', java); @@ -27,7 +30,7 @@ SyntaxHighlighter.registerLanguage('sh', sh); SyntaxHighlighter.registerLanguage('solidity', solidity); SyntaxHighlighter.registerLanguage('swift', swift); -import { getProgrammingLanguageName } from '../utils'; + const { header1, header2, header3, header4 } = textStyles const MDXComponents = { @@ -81,6 +84,39 @@ const MDXComponents = { ); }, + // lists + ul: ({ children }: any) => { + return ( + + {children} + + ); + }, + ol: ({ children }: any) => { + return ( + + {children} + + ); + }, + // tables + table: ({ children }: any) => { + return ( + + + {children} +
+
+ ); + }, // pre pre: ({ children }: any) => { return ( @@ -88,30 +124,49 @@ const MDXComponents = {
{children}
); - } + }, // code - // code: (code: any) => { - // const language = getProgrammingLanguageName(code); - - // return !!code.inline ? ( - // - // {code.children[0]} - // - // ) : ( - // - // {code.children[0]} - // - // ); - // } + code: ({ className, ...code }: any) => { + const { colorMode } = useColorMode(); + const isDark = colorMode === 'dark'; + if (className?.includes("terminal")) return ( + + {code.children} + + ) + if (code.inline) return ( + + {code.children[0]} + + ) + if (className?.startsWith('language-')) return ( + + {code.children} + + ) + return ( + + {code.children[0]} + + ); + } }; export default MDXComponents; diff --git a/src/utils/getProgrammingLanguageName.ts b/src/utils/getProgrammingLanguageName.ts index 40f454f300..1201f3e862 100644 --- a/src/utils/getProgrammingLanguageName.ts +++ b/src/utils/getProgrammingLanguageName.ts @@ -1,7 +1,20 @@ -// WIP -export const getProgrammingLanguageName = (code: any) => { - // const hasLanguageNameProperty = Object.keys(code.node.properties).length > 0; - console.log({ code }); +const CLASSNAME_PREFIX = 'language-'; +const DEFAULT = 'bash'; +const JS = ['javascript', 'js', 'jsx', 'ts', 'tsx']; +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; };