Skip to content

Commit acdbbad

Browse files
committed
refactor: migrating mdx to MDXProvider first draft
1 parent aa865d7 commit acdbbad

File tree

6 files changed

+136
-7
lines changed

6 files changed

+136
-7
lines changed

components/mdx/MDXComponents.tsx

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

next.config.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {
3-
reactStrictMode: true,
4-
}
2+
// const nextConfig = {
3+
// reactStrictMode: true,
4+
// }
5+
6+
// module.exports = nextConfig
7+
8+
import remarkFrontmatter from 'remark-frontmatter'
9+
import nextMDX from '@next/mdx'
510

6-
module.exports = nextConfig
11+
const withMDX = nextMDX({
12+
extension: /\.mdx?$/,
13+
options: {
14+
remarkPlugins: [remarkFrontmatter],
15+
// remarkPlugins: [],
16+
rehypePlugins: [],
17+
// If you use `MDXProvider`, uncomment the following line.
18+
providerImportSource: '@mdx-js/react',
19+
},
20+
})
21+
22+
export default withMDX({
23+
reactStrictMode: true,
24+
// Append the default value with md extensions
25+
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
26+
})

package.json

Lines changed: 5 additions & 0 deletions
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",
@@ -65,6 +69,7 @@
6569
"next-mdx-remote": "^4.0.3",
6670
"prettier": "^2.6.2",
6771
"react-syntax-highlighter": "^15.5.0",
72+
"remark-frontmatter": "^4.0.1",
6873
"typescript": "4.6.4"
6974
},
7075
"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 MDXcomponents from '../components/mdx/MDXComponents'
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={MDXcomponents}>
78+
<Component {...pageProps} />
79+
</MDXProvider>
7680
<Footer />
7781
<ConsentBanner />
7882
</Box>

pages/lessons/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path from 'path'
44
import matter from 'gray-matter'
55
import { ContentBanner } from '../../components/ContentBanner'
66

7+
const CONTENT_PATH = 'lessons'
8+
79
interface Lesson {
810
path: string
911
frontMatter: any
@@ -61,7 +63,7 @@ const Lessons: React.FC<LessonProps> = ({ lessons }: { lessons: Lesson[] }) => {
6163
export default Lessons
6264

6365
export const getStaticProps = async () => {
64-
const directories = fs.readdirSync(path.join('lessons'))
66+
const directories = fs.readdirSync(path.join(CONTENT_PATH))
6567
const lessons: Lesson[] = []
6668
directories.reverse().map((filename) => {
6769
fs.readdirSync(path.join('lessons', filename)).map((file) => {

yarn.lock

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,14 @@
29102910
"@json-rpc-tools/types" "^1.7.6"
29112911
"@pedrouid/environment" "^1.0.1"
29122912

2913+
"@mdx-js/loader@^2.1.3":
2914+
version "2.1.3"
2915+
resolved "https://registry.yarnpkg.com/@mdx-js/loader/-/loader-2.1.3.tgz#b89f6e7b02933b1bf8a9cb69f73b43dab1bdb4d1"
2916+
integrity sha512-7LtklcfzZC9aWWFREop0ivemhwcp/cke2tICHEhnDyGn+hTg7LIbWCfSos68kJv9w7Z47KYfNcg9/8zBD+8eXA==
2917+
dependencies:
2918+
"@mdx-js/mdx" "^2.0.0"
2919+
source-map "^0.7.0"
2920+
29132921
"@mdx-js/mdx@^1.6.22":
29142922
version "1.6.22"
29152923
resolved "https://registry.npmjs.org/@mdx-js/mdx/-/mdx-1.6.22.tgz"
@@ -2971,6 +2979,14 @@
29712979
"@types/mdx" "^2.0.0"
29722980
"@types/react" ">=16"
29732981

2982+
"@mdx-js/react@^2.1.3":
2983+
version "2.1.3"
2984+
resolved "https://registry.yarnpkg.com/@mdx-js/react/-/react-2.1.3.tgz#4b28a774295ed1398cf6be1b8ddef69d6a30e78d"
2985+
integrity sha512-11n4lTvvRyxq3OYbWJwEYM+7q6PE0GxKbk0AwYIIQmrRkxDeljIsjDQkKOgdr/orgRRbYy5zi+iERdnwe01CHQ==
2986+
dependencies:
2987+
"@types/mdx" "^2.0.0"
2988+
"@types/react" ">=16"
2989+
29742990
29752991
version "1.6.22"
29762992
resolved "https://registry.npmjs.org/@mdx-js/util/-/util-1.6.22.tgz"
@@ -3001,6 +3017,11 @@
30013017
dependencies:
30023018
glob "7.1.7"
30033019

3020+
"@next/mdx@^12.3.1":
3021+
version "12.3.1"
3022+
resolved "https://registry.yarnpkg.com/@next/mdx/-/mdx-12.3.1.tgz#7951f27e3fb178319adfcb68b8e6365dbae5c36e"
3023+
integrity sha512-b7G3CsAwyD/8Eo6uDjCCqNwEi68LTyo/JYPC0N2Yu3yl/iveMDCi03ME+9JsnFPr3DcjmGSKc1ioiYM4dJUkTg==
3024+
30043025
30053026
version "12.1.6"
30063027
resolved "https://registry.yarnpkg.com/@next/swc-android-arm-eabi/-/swc-android-arm-eabi-12.1.6.tgz#79a35349b98f2f8c038ab6261aa9cd0d121c03f9"
@@ -8554,6 +8575,13 @@ fault@^1.0.0:
85548575
dependencies:
85558576
format "^0.2.0"
85568577

8578+
fault@^2.0.0:
8579+
version "2.0.1"
8580+
resolved "https://registry.yarnpkg.com/fault/-/fault-2.0.1.tgz#d47ca9f37ca26e4bd38374a7c500b5a384755b6c"
8581+
integrity sha512-WtySTkS4OKev5JtpHXnib4Gxiurzh5NCGvWrFaZ34m6JehfTUhKZvn9njTfw48t6JumVQOmrKqpmGcdwxnhqBQ==
8582+
dependencies:
8583+
format "^0.2.0"
8584+
85578585
fb-watchman@^2.0.0:
85588586
version "2.0.1"
85598587
resolved "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.1.tgz"
@@ -11231,6 +11259,13 @@ mdast-util-from-markdown@^1.0.0:
1123111259
unist-util-stringify-position "^3.0.0"
1123211260
uvu "^0.5.0"
1123311261

11262+
mdast-util-frontmatter@^1.0.0:
11263+
version "1.0.0"
11264+
resolved "https://registry.yarnpkg.com/mdast-util-frontmatter/-/mdast-util-frontmatter-1.0.0.tgz#ef12469379782e4a0fd995fed60cc3b871e6c819"
11265+
integrity sha512-7itKvp0arEVNpCktOET/eLFAYaZ+0cNjVtFtIPxgQ5tV+3i+D4SDDTjTzPWl44LT59PC+xdx+glNTawBdF98Mw==
11266+
dependencies:
11267+
micromark-extension-frontmatter "^1.0.0"
11268+
1123411269
mdast-util-mdx-expression@^1.0.0:
1123511270
version "1.2.1"
1123611271
resolved "https://registry.npmjs.org/mdast-util-mdx-expression/-/mdast-util-mdx-expression-1.2.1.tgz"
@@ -11443,6 +11478,15 @@ micromark-core-commonmark@^1.0.0, micromark-core-commonmark@^1.0.1:
1144311478
micromark-util-types "^1.0.1"
1144411479
uvu "^0.5.0"
1144511480

11481+
micromark-extension-frontmatter@^1.0.0:
11482+
version "1.0.0"
11483+
resolved "https://registry.yarnpkg.com/micromark-extension-frontmatter/-/micromark-extension-frontmatter-1.0.0.tgz#612498e6dad87c132c95e25f0918e7cc0cd535f6"
11484+
integrity sha512-EXjmRnupoX6yYuUJSQhrQ9ggK0iQtQlpi6xeJzVD5xscyAI+giqco5fdymayZhJMbIFecjnE2yz85S9NzIgQpg==
11485+
dependencies:
11486+
fault "^2.0.0"
11487+
micromark-util-character "^1.0.0"
11488+
micromark-util-symbol "^1.0.0"
11489+
1144611490
micromark-extension-mdx-expression@^1.0.0:
1144711491
version "1.0.3"
1144811492
resolved "https://registry.npmjs.org/micromark-extension-mdx-expression/-/micromark-extension-mdx-expression-1.0.3.tgz"
@@ -13644,6 +13688,16 @@ [email protected]:
1364413688
resolved "https://registry.npmjs.org/remark-footnotes/-/remark-footnotes-2.0.0.tgz"
1364513689
integrity sha512-3Clt8ZMH75Ayjp9q4CorNeyjwIxHFcTkaektplKGl2A1jNGEUey8cKL0ZC5vJwfcD5GFGsNLImLG/NGzWIzoMQ==
1364613690

13691+
remark-frontmatter@^4.0.1:
13692+
version "4.0.1"
13693+
resolved "https://registry.yarnpkg.com/remark-frontmatter/-/remark-frontmatter-4.0.1.tgz#84560f7ccef114ef076d3d3735be6d69f8922309"
13694+
integrity sha512-38fJrB0KnmD3E33a5jZC/5+gGAC2WKNiPw1/fdXJvijBlhA7RCsvJklrYJakS0HedninvaCYW8lQGf9C918GfA==
13695+
dependencies:
13696+
"@types/mdast" "^3.0.0"
13697+
mdast-util-frontmatter "^1.0.0"
13698+
micromark-extension-frontmatter "^1.0.0"
13699+
unified "^10.0.0"
13700+
1364713701
1364813702
version "1.6.22"
1364913703
resolved "https://registry.npmjs.org/remark-mdx/-/remark-mdx-1.6.22.tgz"
@@ -14350,7 +14404,7 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1:
1435014404
resolved "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz"
1435114405
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
1435214406

14353-
source-map@^0.7.3:
14407+
source-map@^0.7.0, source-map@^0.7.3:
1435414408
version "0.7.4"
1435514409
resolved "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz"
1435614410
integrity sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==

0 commit comments

Comments
 (0)