Skip to content

Commit

Permalink
feat: Add MDX support for Next.js
Browse files Browse the repository at this point in the history
  • Loading branch information
codinsonn committed Apr 11, 2024
1 parent ee1aacb commit ae746e1
Show file tree
Hide file tree
Showing 9 changed files with 2,740 additions and 467 deletions.
4 changes: 4 additions & 0 deletions apps/next/app/(public)/markdown/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use client'
import MarkdownScreen from '@app/core/screens/MarkdownScreen'

export default MarkdownScreen
9 changes: 7 additions & 2 deletions apps/next/next.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const { withExpo } = require("@expo/next-adapter");
const withMDX = require("@next/mdx")();

/** @type {import('next').NextConfig} */
const nextConfig = withExpo({
const nextConfig = withMDX(withExpo({
reactStrictMode: true,
swcMinify: true,
transpilePackages: [
"react-native",
"react-native-web",
"expo",
"@bacons/mdx",
"@bacons/react-views",
"@expo/html-elements",
// Add more React Native / Expo packages here...
],
pageExtensions: ["js", "jsx", "ts", "tsx", "md", "mdx"],
experimental: {
forceSwcTransforms: true,
},
Expand All @@ -21,6 +26,6 @@ const nextConfig = withExpo({
}
]
}
});
}));

module.exports = nextConfig;
5 changes: 4 additions & 1 deletion apps/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"version": "1.0.0",
"private": true,
"dependencies": {
"@mdx-js/loader": "^3.0.1",
"@mdx-js/react": "^3.0.1",
"@next/mdx": "^14.1.4",
"@types/mdx": "^2.0.12",
"next": "~14.0.4"
},
"devDependencies": {},
"scripts": {
"dev": "next dev",
"build": "next build"
Expand Down
90 changes: 90 additions & 0 deletions features/app-core/mdx/MarkdownTheme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from 'react'
import { StyleSheet, View, Text } from 'react-native'
import { MDXStyles, MDXComponents } from '@bacons/mdx'
import { Link } from '../navigation/Link'
import './markdown.theme.css' // Duplicate of the React-Native styles from this file

/* --- Types -------------------------------------------------------------------------------------- */

type MarkdownScreenProps = {
children: React.ReactNode
}

/* --- <MarkdownTheme/> --------------------------------------------------------------------------- */

const MarkdownTheme = ({ children }: MarkdownScreenProps) => {
return (
<View nativeID="markdown-theme">
<MDXStyles
h1={styles.h1}
h2={styles.h2}
p={styles.p}
ul={styles.ul}
li={styles.li}
blockquote={styles.blockquote}
a={styles.link}
>
<MDXComponents
h1={(props) => <Text {...props} style={{ ...styles.h1 }} />}
h2={(props) => <Text {...props} style={{ ...styles.h2 }} />}
p={(props) => <Text {...props} style={{ ...styles.p }} />}
ul={(props) => <View {...props} style={{ ...styles.ul }} />}
li={(props) => <Text {...props} style={{ ...styles.li }} />}
blockquote={(props) => <View {...props} style={{ ...styles.blockquote }} />} // prettier-ignore
a={(props) => <Link {...props} style={{ ...styles.link }} />}
>
{children}
</MDXComponents>
</MDXStyles>
</View>
)
}

/* --- Styles ---------------------------------------------------------------------------------- */
// -i- These styles won't work in Next.js for some reason, duplicate them in markdown.theme.css

const styles = StyleSheet.create({
h1: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 16,
},
h2: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 16,
},
p: {
fontSize: 16,
marginBottom: 16,
lineHeight: 22,
},
ul: {
padding: 0,
},
li: {
fontSize: 16,
marginBottom: 16,
lineHeight: 22,
},
blockquote: {
borderLeftColor: 'lightgray',
borderStyle: 'solid',
borderLeftWidth: 6,
fontSize: 16,
paddingLeft: 16,
paddingTop: 4,
lineHeight: 22,
},
link: {
marginTop: 16,
fontSize: 16,
color: 'blue',
textAlign: 'center',
textDecorationLine: 'underline',
},
})

/* --- Exports --------------------------------------------------------------------------------- */

export default MarkdownTheme
62 changes: 62 additions & 0 deletions features/app-core/mdx/markdown.theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

/* --- Resets ---------------------------------------------------------------------------------- */

ul {
padding-inline-start: 0px;
list-style: none;
}

blockquote {
margin: 0;
padding: 0;
}

/* --- Markdown theme -------------------------------------------------------------------------- */
/* -i- duplicate from 'MarkdownTheme.tsx', keep them in sync */

#markdown-theme h1 {
font-size: 24px;
font-weight: bold;
margin-bottom: 16px;
}

#markdown-theme h2 {
font-size: 20px;
font-weight: bold;
margin-bottom: 16px;
}

#markdown-theme p {
font-size: 16px;
margin-bottom: 16px;
line-height: 22px;
}

#markdown-theme ul {
padding: 0;
}

#markdown-theme li {
font-size: 16px;
margin-bottom: 16px;
line-height: 22px;
}

#markdown-theme blockquote {
border-width: 0;
border-style: solid;
border-left-color: lightgray;
border-left-width: 6px;
font-size: 16px;
padding-left: 16px;
padding-top: 4px;
line-height: 22px;
}

#markdown-theme a {
margin-top: 16px;
font-size: 16px;
color: blue;
text-align: center;
text-decoration: underline;
}
54 changes: 13 additions & 41 deletions features/app-core/screens/MarkdownScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import { StyleSheet, View } from 'react-native'
import { MDXStyles } from '@bacons/mdx'
import { Link } from '../navigation/Link' // @ts-ignore
import ReadMe from '../mdx/readme.mdx'
import MarkdownTheme from '../mdx/MarkdownTheme'

/* --- <MarkdownScreen/> --------------------------------------------------------------------------- */

Expand All @@ -15,16 +15,11 @@ const MarkdownScreen = () => {
>
{`< Back`}
</Link>
<MDXStyles
h1={styles.h1}
h2={styles.h2}
p={styles.p}
li={styles.li}
blockquote={styles.blockquote}
a={styles.link}
>
<ReadMe />
</MDXStyles>
<View style={styles.markdownWrapper}>
<MarkdownTheme>
<ReadMe />
</MarkdownTheme>
</View>
</View>
)
}
Expand All @@ -35,9 +30,15 @@ const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'flex-start',
alignItems: 'center',
padding: 24,
},
markdownWrapper: {
width: '100%',
position: 'relative',
alignItems: 'flex-start',
maxWidth: 600,
},
backButton: {
position: 'absolute',
top: 16,
Expand All @@ -49,35 +50,6 @@ const styles = StyleSheet.create({
fontSize: 16,
textAlign: 'center',
},
h1: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 16,
},
h2: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 16,
},
p: {
fontSize: 16,
marginBottom: 16,
lineHeight: 22,
},
li: {
fontSize: 16,
marginBottom: 16,
lineHeight: 22,
},
blockquote: {
borderLeftColor: 'lightgray',
borderStyle: 'solid',
borderLeftWidth: 6,
fontSize: 16,
paddingLeft: 16,
paddingTop: 4,
lineHeight: 22,
},
link: {
marginTop: 16,
fontSize: 16,
Expand Down
Loading

0 comments on commit ae746e1

Please sign in to comment.