Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 7 additions & 84 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"capture-gif": "node scripts/capture-gif.mjs"
},
"dependencies": {
"@mdx-js/mdx": "^3.1.1",
"@vercel/analytics": "^2.0.1",
"@vercel/og": "^1.0.0",
"@vercel/speed-insights": "^2.0.0",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"next": "^16.3.0",
"next-mdx-remote": "^6.0.0",
"react": "^18",
"react-dom": "^18",
"reading-time": "^1.5.0",
Expand All @@ -29,6 +29,7 @@
"shiki": "^4.2.0"
},
"devDependencies": {
"@types/mdx": "^2.0.14",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
63 changes: 32 additions & 31 deletions pages/blog/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react'
import * as runtime from 'react/jsx-runtime'
import Link from 'next/link'
import type { GetStaticPaths, GetStaticProps } from 'next'
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote, type MDXRemoteSerializeResult } from 'next-mdx-remote'
import { compile, runSync } from '@mdx-js/mdx'
import remarkGfm from 'remark-gfm'
import rehypePrettyCode from 'rehype-pretty-code'
import rehypeSlug from 'rehype-slug'
Expand Down Expand Up @@ -29,7 +30,7 @@ import { SITE_URL, githubSourceUrl, ogCardUrl } from '@/lib/site'

interface PostPageProps {
meta: PostMeta
mdxSource: MDXRemoteSerializeResult
mdxCode: string
toc: TocItem[]
related: PostMeta[]
sourceUrl: string
Expand All @@ -44,7 +45,7 @@ function ogImageFor(meta: PostMeta): string {

export default function PostPage({
meta,
mdxSource,
mdxCode,
toc,
related,
sourceUrl,
Expand All @@ -53,6 +54,7 @@ export default function PostPage({
}: PostPageProps) {
const url = `${SITE_URL}/blog/${meta.slug}`
const ogImage = ogImageFor(meta)
const Content = useMemo(() => runSync(mdxCode, runtime).default, [mdxCode])

return (
<>
Expand Down Expand Up @@ -111,7 +113,7 @@ export default function PostPage({
<div className="post-main">
<article className="post">
<div className="prose">
<MDXRemote {...mdxSource} components={mdxComponents} />
<Content components={mdxComponents} />
</div>

<div className="post-actions">
Expand Down Expand Up @@ -195,32 +197,31 @@ export const getStaticProps: GetStaticProps<PostPageProps> = async ({ params })
const post = getPostBySlug(slug)
if (!post) return { notFound: true }

const mdxSource = await serialize(post.content, {
mdxOptions: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
// Lift standalone images out of their wrapping <p> so our <figure>/<figcaption>
// image renderer doesn't produce invalid <p><figure> nesting (a hydration error).
rehypeUnwrapImages,
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
properties: { className: ['heading-anchor'], ariaHidden: true, tabIndex: -1 },
content: { type: 'text', value: '#' },
},
],
[
rehypePrettyCode,
{
theme: 'github-dark',
// Let our CSS own the background so code blocks match the deep-space theme.
keepBackground: false,
},
],
Comment on lines -200 to -221

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these options are basically the same, their indentation just changed

const mdxCode = await compile(post.content, {
outputFormat: 'function-body',
remarkPlugins: [remarkGfm],
rehypePlugins: [
// Lift standalone images out of their wrapping <p> so our <figure>/<figcaption>
// image renderer doesn't produce invalid <p><figure> nesting (a hydration error).
rehypeUnwrapImages,
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
properties: { className: ['heading-anchor'], ariaHidden: true, tabIndex: -1 },
content: { type: 'text', value: '#' },
},
],
},
[
rehypePrettyCode,
{
theme: 'github-dark',
// Let our CSS own the background so code blocks match the deep-space theme.
keepBackground: false,
},
],
],
})

// Compute prev/next within the visible, date-sorted set (newest first).
Expand All @@ -234,7 +235,7 @@ export const getStaticProps: GetStaticProps<PostPageProps> = async ({ params })
return {
props: {
meta: post.meta,
mdxSource,
mdxCode: String(mdxCode),
toc: extractToc(post.content),
related: getRelatedPosts(slug, 3),
sourceUrl: folder ? githubSourceUrl(folder) : SITE_URL,
Expand Down
4 changes: 2 additions & 2 deletions src/components/mdx/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MDXRemoteProps } from 'next-mdx-remote'
import type { MDXComponents } from 'mdx/types'
import Video from './Video'
import YouTube from './YouTube'
import Embed from './Embed'
Expand All @@ -17,7 +17,7 @@ function A({ href = '', children, ...rest }: React.AnchorHTMLAttributes<HTMLAnch

// The complete allowlist of components available inside MDX posts. Anything not
// listed here (and not a standard HTML tag) fails the build, keeping PR review safe.
export const mdxComponents: MDXRemoteProps['components'] = {
export const mdxComponents: MDXComponents = {
Video,
YouTube,
Embed,
Expand Down
Loading