Skip to content

Commit dd42bf6

Browse files
authored
Merge pull request #167 from 7i7o/dw-2098/mdx-migration
**Please rebase all branches you've been working with, for this PR moves the lessons location in the folder structure from /lessons to /pages/lessons**
2 parents aa865d7 + acebc01 commit dd42bf6

28 files changed

+266
-321
lines changed
File renamed without changes.

components/ContentCallout.tsx renamed to components/mdx/Callout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Box, HStack, useStyleConfig } from '@chakra-ui/react'
22
import React, { FC } from 'react'
33

4-
const ComponentCallout: FC = (props: any) => {
4+
const Callout: FC = (props: any) => {
55
const { size, variant, emoji, children, ...rest } = props
66

7-
const styles = useStyleConfig('ContentCallout', { size, variant })
7+
const styles = useStyleConfig('Callout', { size, variant })
88
return (
99
<Box __css={styles} {...rest}>
1010
<HStack spacing={4}>
@@ -17,4 +17,4 @@ const ComponentCallout: FC = (props: any) => {
1717
)
1818
}
1919

20-
export default ComponentCallout
20+
export default Callout

components/mdx/Components.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Box, Code, Heading, Image, Text } from '@chakra-ui/react'
2+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
3+
import dracula from 'react-syntax-highlighter/dist/cjs/styles/prism/dracula'
4+
import { CopyToClipboard } from '../CopyToClipboard'
5+
import SideDrawer from './SideDrawer'
6+
import Callout from './Callout'
7+
8+
const Components = {
9+
code: (props: any) => {
10+
const [, language] =
11+
(props.className as string)?.match(/language-(\w+)/) ?? []
12+
13+
if (language) {
14+
return (
15+
<Box position="relative">
16+
<SyntaxHighlighter language={language} {...props} style={dracula} />
17+
<CopyToClipboard {...props} />
18+
</Box>
19+
)
20+
}
21+
22+
return <Code fontSize="md" wordBreak="break-all" {...props} />
23+
},
24+
h1: (props: any) => (
25+
<Heading as="h1" apply="mdx.h1" fontSize="4xl" {...props} />
26+
),
27+
h2: (props: any) => (
28+
<Heading as="h2" apply="mdx.h2" fontSize="3xl" {...props} />
29+
),
30+
h3: (props: any) => (
31+
<Heading as="h3" apply="mdx.h3" fontSize="2xl" {...props} />
32+
),
33+
h4: (props: any) => (
34+
<Heading as="h4" apply="mdx.h4" fontSize="xl" {...props} />
35+
),
36+
p: (props: any) => <Text as="p" apply="mdx.p" fontSize="xl" {...props} />,
37+
a: (props: any) => <Text as="a" apply="mdx.a" {...props} />,
38+
ul: (props: any) => <Text as="ul" apply="mdx.ul" fontSize="xl" {...props} />,
39+
img: (props: any) => (
40+
<Image as="img" apply="mdx.image" m="0 auto" alt="" {...props} />
41+
),
42+
SideDrawer,
43+
Callout,
44+
}
45+
46+
export default Components

components/ContentSideDrawer.tsx renamed to components/mdx/SideDrawer.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import {
1212
} from '@chakra-ui/react'
1313
import React from 'react'
1414

15-
function ContentSideDrawer(props: any) {
15+
function SideDrawer(props: any) {
1616
const { isOpen, onOpen, onClose } = useDisclosure()
1717

18+
const { buttonText, title } = props
19+
const drawerTitle = title ? title : buttonText
20+
1821
return (
1922
<>
2023
<Button
@@ -25,14 +28,14 @@ function ContentSideDrawer(props: any) {
2528
fontWeight="bold"
2629
bg="gray.700"
2730
mt="1em"
28-
height={[`${props.title.length > 30 ? '3.75rem' : '2.5rem'}`, '2.5rem']}
31+
height={[`${buttonText.length > 30 ? '3.75rem' : '2.5rem'}`, '2.5rem']}
2932
style={{
3033
whiteSpace: 'normal',
3134
wordWrap: 'break-word',
3235
}}
36+
rightIcon={<ArrowRightIcon />}
3337
>
34-
{props.title}&nbsp;&nbsp;
35-
<ArrowRightIcon />
38+
{buttonText}
3639
</Button>
3740
<Drawer isOpen={isOpen} placement="right" onClose={onClose} size="xl">
3841
<DrawerOverlay
@@ -44,7 +47,7 @@ function ContentSideDrawer(props: any) {
4447
<DrawerContent bg="#00000f" px="4" pb="8">
4548
<DrawerCloseButton />
4649
<DrawerHeader fontSize="3xl" textColor="yellow.300">
47-
{props.title}
50+
{drawerTitle}
4851
</DrawerHeader>
4952
<DrawerBody>{props.children}</DrawerBody>
5053
</DrawerContent>
@@ -53,4 +56,4 @@ function ContentSideDrawer(props: any) {
5356
)
5457
}
5558

56-
export default ContentSideDrawer
59+
export default SideDrawer

lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CONTENT_PATH = 'pages/lessons'

next.config.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {
3-
reactStrictMode: true,
4-
}
52

6-
module.exports = nextConfig
3+
import remarkFrontmatter from 'remark-frontmatter'
4+
import nextMDX from '@next/mdx'
5+
6+
const withMDX = nextMDX({
7+
extension: /\.mdx?$/,
8+
options: {
9+
remarkPlugins: [remarkFrontmatter],
10+
rehypePlugins: [],
11+
// If you use `MDXProvider`, uncomment the following line.
12+
providerImportSource: '@mdx-js/react',
13+
},
14+
})
15+
16+
export default withMDX({
17+
reactStrictMode: true,
18+
// Append the default value with md extensions
19+
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
20+
})

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "dd-academy",
33
"private": true,
4+
"type": "module",
45
"scripts": {
56
"dev": "next dev",
67
"build": "next build",
@@ -35,6 +36,9 @@
3536
"devDependencies": {
3637
"@babel/core": "^7.18.6",
3738
"@chakra-ui/storybook-addon": "^4.0.8",
39+
"@mdx-js/loader": "^2.1.3",
40+
"@mdx-js/react": "^2.1.3",
41+
"@next/mdx": "^12.3.1",
3842
"@storybook/addon-actions": "^6.5.10",
3943
"@storybook/addon-essentials": "^6.5.10",
4044
"@storybook/addon-interactions": "^6.5.10",
@@ -62,9 +66,9 @@
6266
"jest": "^28.1.1",
6367
"jest-environment-jsdom": "^28.1.1",
6468
"lint-staged": "^13.0.0",
65-
"next-mdx-remote": "^4.0.3",
6669
"prettier": "^2.6.2",
6770
"react-syntax-highlighter": "^15.5.0",
71+
"remark-frontmatter": "^4.0.1",
6872
"typescript": "4.6.4"
6973
},
7074
"lint-staged": {

pages/_app.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Header from '../components/Header'
77
import Footer from '../components/footer/Footer'
88
import ConsentBanner from '../components/ConsentBanner'
99
import { DefaultSeo } from 'next-seo'
10+
import { MDXProvider } from '@mdx-js/react'
11+
import Components from '../components/mdx/Components'
1012

1113
function MyApp({ Component, pageProps }: AppProps) {
1214
return (
@@ -72,7 +74,9 @@ function MyApp({ Component, pageProps }: AppProps) {
7274
/>
7375
<Box p="1.25em" px="5%" mx={{ base: '2rem', md: '6rem', lg: '10rem' }}>
7476
<Header />
75-
<Component {...pageProps} />
77+
<MDXProvider components={Components}>
78+
<Component {...pageProps} />
79+
</MDXProvider>
7680
<Footer />
7781
<ConsentBanner />
7882
</Box>

pages/getting-started/index.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import fs from 'fs'
1616
import path from 'path'
1717
import matter from 'gray-matter'
18+
import { CONTENT_PATH } from '../../lib/constants'
1819

1920
interface LessonProps {
2021
lessons: {
@@ -221,22 +222,27 @@ const GettingStarted: React.FC<LessonProps> = ({ lessons }) => {
221222
export default GettingStarted
222223

223224
export const getStaticProps = async () => {
224-
const directories = fs.readdirSync(path.join('lessons'))
225+
const contentDir = path.join(CONTENT_PATH)
226+
const directories = fs.readdirSync(path.join(contentDir))
225227
const lessons: object[] = []
226-
directories.reverse().map((filename) => {
227-
fs.readdirSync(path.join('lessons', filename)).map((file) => {
228-
const markdownWithMeta = fs.readFileSync(
229-
path.join('lessons', filename, file),
230-
'utf-8',
231-
)
228+
directories.reverse().map((folder) => {
229+
if (fs.lstatSync(path.join(contentDir, folder)).isDirectory()) {
230+
fs.readdirSync(path.join(contentDir, folder)).map((file) => {
231+
if (!fs.lstatSync(path.join(contentDir, folder, file)).isDirectory()) {
232+
const markdownWithMeta = fs.readFileSync(
233+
path.join(contentDir, folder, file),
234+
'utf-8',
235+
)
232236

233-
const { data: frontMatter } = matter(markdownWithMeta)
234-
lessons.push({
235-
path: filename,
236-
frontMatter,
237-
slug: file.replace('.mdx', ''),
237+
const { data: frontMatter } = matter(markdownWithMeta)
238+
lessons.push({
239+
path: folder,
240+
frontMatter,
241+
slug: file.replace('.mdx', ''),
242+
})
243+
}
238244
})
239-
})
245+
}
240246
})
241247
return {
242248
props: {

0 commit comments

Comments
 (0)