Skip to content

Commit

Permalink
Merge pull request #43 from ethereum/markdown-styling
Browse files Browse the repository at this point in the history
Markdown styling
  • Loading branch information
corwintines authored Nov 28, 2022
2 parents 7af02a2 + b3f7f2d commit bc030df
Show file tree
Hide file tree
Showing 13 changed files with 3,704 additions and 3,345 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@mdx-js/loader": "^2.1.3",
"@mdx-js/react": "^2.1.3",
"@next/mdx": "^12.3.0",
"chakra-ui-markdown-renderer": "^4.1.0",
"focus-visible": "^5.2.0",
"framer-motion": "^7.3.2",
"gray-matter": "^4.0.3",
Expand All @@ -27,7 +28,8 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-markdown": "^8.0.3",
"react-syntax-highlighter": "^15.5.0"
"react-syntax-highlighter": "^15.5.0",
"remark-gfm": "^3.0.1"
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
Expand Down
112 changes: 51 additions & 61 deletions src/components/MDXComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import { Heading, Link, Stack, Text } from '@chakra-ui/react';
import {
Flex,
Heading,
Link,
ListItem,
OrderedList,
Stack,
Table,
Text,
UnorderedList
} from '@chakra-ui/react';
import NextLink from 'next/link';
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';

import { Code } from './UI/docs'
import { Code } from './UI/docs';
import { textStyles } from '../theme/foundations';

import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash';
import go from 'react-syntax-highlighter/dist/cjs/languages/prism/go';
import graphql from 'react-syntax-highlighter/dist/cjs/languages/prism/graphql';
import java from 'react-syntax-highlighter/dist/cjs/languages/prism/java';
import javascript from 'react-syntax-highlighter/dist/cjs/languages/prism/javascript';
import json from 'react-syntax-highlighter/dist/cjs/languages/prism/json';
import python from 'react-syntax-highlighter/dist/cjs/languages/prism/python';
import sh from 'react-syntax-highlighter/dist/cjs/languages/prism/shell-session';
import solidity from 'react-syntax-highlighter/dist/cjs/languages/prism/solidity';
import swift from 'react-syntax-highlighter/dist/cjs/languages/prism/swift';

// syntax highlighting languages supported
SyntaxHighlighter.registerLanguage('bash', bash);
SyntaxHighlighter.registerLanguage('go', go);
SyntaxHighlighter.registerLanguage('graphql', graphql);
SyntaxHighlighter.registerLanguage('java', java);
SyntaxHighlighter.registerLanguage('javascript', javascript);
SyntaxHighlighter.registerLanguage('json', json);
SyntaxHighlighter.registerLanguage('python', python);
SyntaxHighlighter.registerLanguage('sh', sh);
SyntaxHighlighter.registerLanguage('solidity', solidity);
SyntaxHighlighter.registerLanguage('swift', swift);
const { header1, header2, header3, header4 } = textStyles;

const MDXComponents = {
// paragraphs
p: ({ children }: any) => {
return (
<Text mb={7} _last={{ mb: 0 }} size='sm' lineHeight={1.5}>
<Text mb={7} lineHeight={1.5}>
{children}
</Text>
);
Expand All @@ -42,7 +31,7 @@ const MDXComponents = {
<NextLink href={href} passHref>
<Link
isExternal={href.startsWith('http') && !href.includes('geth.ethereum.org')}
color='primary'
variant='light'
>
{children}
</Link>
Expand All @@ -52,64 +41,65 @@ const MDXComponents = {
// headings
h1: ({ children }: any) => {
return (
<Heading as='h1' textAlign='start' fontSize='4xl' mb={5}>
<Heading as='h1' textAlign='start' mb={5} {...header1}>
{children}
</Heading>
);
},
h2: ({ children }: any) => {
return (
<Heading as='h2' textAlign='start' fontSize='3xl' mb={4}>
<Heading as='h2' textAlign='start' mt='16 !important' mb={4} {...header2}>
{children}
</Heading>
);
},
h3: ({ children }: any) => {
return (
<Heading as='h3' fontSize='2xl' mt={5} mb={2.5}>
<Heading as='h3' mt={5} mb={2.5} {...header3}>
{children}
</Heading>
);
},
h4: ({ children }: any) => {
return (
<Heading as='h4' fontSize='lg' mb={2.5}>
<Heading as='h4' mb={2.5} {...header4}>
{children}
</Heading>
);
},
// tables
table: ({ children }: any) => (
<Flex maxW='min(100%, 100vw)' overflowX='scroll'>
<Table
variant='striped'
colorScheme='greenAlpha'
border='1px'
borderColor='blackAlpha.50'
my={6}
size={{ base: 'sm', lg: 'md' }}
w='auto'
>
{children}
</Table>
</Flex>
),
// pre
pre: ({ children }: any) => {
return (
<Stack mb={5}>
<pre>{children}</pre>
</Stack>
);
},
pre: ({ children }: any) => (
<Stack mb={5}>
<pre>{children}</pre>
</Stack>
),
// code
code: (code: any) => {
return (
<Code code={code} />
)

// return !!code.inline ? (
// <Text
// as={'span'}
// padding='0.125em 0.25em'
// color='red.300'
// background='code-bg-contrast'
// borderRadius='0.25em'
// fontFamily='code'
// fontSize='sm'
// overflowY='scroll'
// >
// {code.children[0]}
// </Text>
// ) : (
// <Stack style={nightOwl}>
// {code.children[0]}
// </Stack>
// );
code: ({ children, ...props }: any) => <Code {...props}>{children}</Code>,
// list
ul: ({children}: any) => {
return <UnorderedList mb={7} px={4}>{children}</UnorderedList>
},
ol: ({children}: any) => {
return <OrderedList mb={7} px={4}>{children}</OrderedList>
},
li: ({ children }: any) => {
return <ListItem color='primary'>{children}</ListItem>
}
};

Expand Down
105 changes: 72 additions & 33 deletions src/components/UI/docs/Code.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
// Libraries
import { Code as ChakraCode, Stack, Text } from '@chakra-ui/react';
import { FC } from 'react';
import { Code as ChakraCode, Stack, Text, useColorMode } from '@chakra-ui/react';
import { nightOwl, prism } from 'react-syntax-highlighter/dist/cjs/styles/prism';
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';

// Constants, utilities
import { CLASSNAME_PREFIX } from '../../../constants';
import { getProgrammingLanguageName } from '../../../utils';

// Programming lang syntax highlighters
import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash';
import go from 'react-syntax-highlighter/dist/cjs/languages/prism/go';
import graphql from 'react-syntax-highlighter/dist/cjs/languages/prism/graphql';
import java from 'react-syntax-highlighter/dist/cjs/languages/prism/java';
import javascript from 'react-syntax-highlighter/dist/cjs/languages/prism/javascript';
import json from 'react-syntax-highlighter/dist/cjs/languages/prism/json';
import python from 'react-syntax-highlighter/dist/cjs/languages/prism/python';
import sh from 'react-syntax-highlighter/dist/cjs/languages/prism/shell-session';
import solidity from 'react-syntax-highlighter/dist/cjs/languages/prism/solidity';
import swift from 'react-syntax-highlighter/dist/cjs/languages/prism/swift';

// syntax highlighting languages supported
SyntaxHighlighter.registerLanguage('bash', bash);
SyntaxHighlighter.registerLanguage('terminal', bash);
SyntaxHighlighter.registerLanguage('go', go);
SyntaxHighlighter.registerLanguage('graphql', graphql);
SyntaxHighlighter.registerLanguage('java', java);
SyntaxHighlighter.registerLanguage('javascript', javascript);
SyntaxHighlighter.registerLanguage('json', json);
SyntaxHighlighter.registerLanguage('python', python);
SyntaxHighlighter.registerLanguage('sh', sh);
SyntaxHighlighter.registerLanguage('solidity', solidity);
SyntaxHighlighter.registerLanguage('swift', swift);

interface Props {
code: any;
className: string;
children: string[];
inline?: boolean;
}

export const Code: FC<Props> = ({ code }) => {
return (
!!code.inline ?
(
<Text
as='span'
background='code-bg'
textStyle='inline-code-snippet'
pb={2}
mb={-2}
>
{code.children[0]}
</Text>
)
:
(
<Stack>
<ChakraCode
overflow='auto'
p={6}
background='code-bg-contrast'
textStyle='code-block'
color='code-text'
>
{code.children[0]}
</ChakraCode>
</Stack>
)
);
export const Code: React.FC<Props> = ({ className, children, inline }) => {
const { colorMode } = useColorMode();
const isDark = colorMode === 'dark';
const isTerminal = className?.includes('terminal');
const [content] = children;
if (inline)
return (
<Text
as='span'
px={1}
color='primary'
bg='code-bg'
borderRadius='0.25em'
textStyle='inline-code-snippet'
>
{content}
</Text>
);
if (isTerminal)
return (
<Stack>
<ChakraCode overflow='auto' p={6} background='terminal-bg' color='terminal-text'>
{content}
</ChakraCode>
</Stack>
);
if (className?.startsWith(CLASSNAME_PREFIX))
return (
<SyntaxHighlighter
language={getProgrammingLanguageName(className)}
style={isDark ? nightOwl : prism}
customStyle={{ borderRadius: '0.5rem', padding: '1rem' }}
>
{content}
</SyntaxHighlighter>
);
return <Stack>{content}</Stack>;
};
2 changes: 1 addition & 1 deletion src/components/docs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Breadcrumbs: FC = () => {
pathSplit = pathSplit.splice(1, pathSplit.length);

return (
<Breadcrumb mb={10}>
<Breadcrumb>
{pathSplit.map((path: string, idx: number) => {
return (
<BreadcrumbItem key={path}>
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,6 @@ export const WINDOWS_BINARY_BASE_URL =

export const LATEST_SOURCES_BASE_URL = 'https://github.com/ethereum/go-ethereum/archive/';
export const RELEASE_NOTES_BASE_URL = 'https://github.com/ethereum/go-ethereum/releases/tag/';

// Code snippet class constants
export const CLASSNAME_PREFIX = 'language-';
22 changes: 14 additions & 8 deletions src/pages/[...slug].tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import fs from 'fs';
import matter from 'gray-matter';
import yaml from 'js-yaml';
import { Stack, Heading } from '@chakra-ui/react';
import ChakraUIRenderer from 'chakra-ui-markdown-renderer';
import ReactMarkdown from 'react-markdown';
import { Heading, Stack } from '@chakra-ui/react';
import MDXComponents from '../components/';
import gfm from 'remark-gfm';
import { ParsedUrlQuery } from 'querystring';
import type { GetStaticPaths, GetStaticProps, NextPage } from 'next';
import { Breadcrumbs } from '../components/docs';

import MDXComponents from '../components/';
import { Breadcrumbs } from '../components/docs';
import { PageMetadata } from '../components/UI';
import { textStyles } from '../theme/foundations';

const MATTER_OPTIONS = {
engines: {
Expand Down Expand Up @@ -76,13 +79,16 @@ const DocPage: NextPage<Props> = ({ frontmatter, content }) => {
<PageMetadata title={frontmatter.title} description={frontmatter.description} />

<main>
<Stack py={8} px={4}>
<Stack mb={16}>
<Breadcrumbs />

<Heading as='h1'>{frontmatter.title}</Heading>

<ReactMarkdown components={MDXComponents}>{content}</ReactMarkdown>
<Heading as='h1' mt='4 !important' mb={0} {...textStyles.header1}>
{frontmatter.title}
</Heading>
{/* <Text as='span' mt='0 !important'>last edited {TODO: get last edited date}</Text> */}
</Stack>
<ReactMarkdown remarkPlugins={[gfm]} components={ChakraUIRenderer(MDXComponents)}>
{content}
</ReactMarkdown>
</main>
</>
);
Expand Down
7 changes: 7 additions & 0 deletions src/theme/foundations/fonts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const fonts = {
// set base fonts as fallback
heading: '"JetBrains Mono", monospace',
button: '"JetBrains Mono", monospace',
code: '"JetBrains Mono", monospace',
body: '"Inter", sans-serif'
};
1 change: 1 addition & 0 deletions src/theme/foundations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './colors';
export * from './config';
export * from './fonts';
export * from './shadows';
export * from './sizes';
export * from './textStyles';
Loading

0 comments on commit bc030df

Please sign in to comment.