Skip to content

Commit 16d05f6

Browse files
committed
add code support in markdown
1 parent c2f0893 commit 16d05f6

File tree

5 files changed

+271
-5
lines changed

5 files changed

+271
-5
lines changed

mdx-components.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import clsx from "clsx";
2+
import { fontMono } from "@/config/fonts";
13
import type { MDXComponents } from 'mdx/types'
24
import Image, { ImageProps } from 'next/image'
3-
45
interface ImageOption {
56
scale: number
67
}
@@ -46,6 +47,18 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
4647
/>
4748
),
4849
p: ({ children }) => <p>{children}</p>,
50+
code: ({ children }) => <code className={clsx('font-mono p-4 block', fontMono.variable)}>{children}</code>,
51+
pre: ({ children, ...props }) => (
52+
<pre
53+
className={clsx(
54+
'rounded-md shadow-lg overflow-auto',
55+
props.className // Preserve any existing className
56+
)}
57+
{...props}
58+
>
59+
{children}
60+
</pre>
61+
),
4962
...components,
5063
}
5164
}

next.config.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import createMDX from '@next/mdx'
22
import remarkMath from 'remark-math'
33
import rehypeKatex from 'rehype-katex'
4+
import rehypeStringify from "rehype-stringify";
45
import remarkMdxImages from 'remark-mdx-images'
56
import remarkGfm from 'remark-gfm'
67
import rehypeUnwrapImages from 'rehype-unwrap-images'
8+
import rehypePrettyCode from "rehype-pretty-code";
79

810
/** @type {import('next').NextConfig} */
911
const nextConfig = {
@@ -24,11 +26,18 @@ const nextConfig = {
2426

2527
}
2628

29+
/** @type {import('rehype-pretty-code').Options} */
30+
const code_options = {
31+
theme: "github-dark-dimmed",
32+
keepBackground: true
33+
};
34+
35+
2736
const withMDX = createMDX({
2837
// Add markdown plugins here, as desired
2938
options: {
3039
remarkPlugins: [remarkGfm, remarkMath, remarkMdxImages],
31-
rehypePlugins: [rehypeKatex, rehypeUnwrapImages],
40+
rehypePlugins: [rehypeKatex, [rehypePrettyCode, code_options]],
3241
},
3342
})
3443
export default withMDX(nextConfig)

package-lock.json

Lines changed: 212 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@
4141
"react-icons": "^5.3.0",
4242
"rehype-katex": "^7.0.1",
4343
"rehype-mdx-import-media": "^1.2.0",
44+
"rehype-pretty-code": "^0.14.0",
45+
"rehype-stringify": "^10.0.1",
4446
"rehype-unwrap-images": "^1.0.0",
4547
"remark-gfm": "^4.0.0",
4648
"remark-math": "^6.0.0",
4749
"remark-mdx-images": "^3.0.0",
48-
"sharp": "^0.33.5"
50+
"sharp": "^0.33.5",
51+
"shiki": "^1.24.4"
4952
},
5053
"devDependencies": {
5154
"@types/node": "20.5.7",

0 commit comments

Comments
 (0)