Installation
Code Hike is a remark plugin for MDX v2. The specific set up will depend on your stack, it usually involves five steps:
Frameworks
Installation guides for specific frameworks. Pick one:
Next.js + Contentlayer + Code Hike
Contentlayer is a content preprocessor that transforms MDX files into type-safe JSON you can easily import into your application.
A demo of Code Hike + Contentlayer is available on GitHub. You can also try it out from your browser on StackBlitz.
We are going to use it together with Next.js, so let's start by installing it:
npm install react react-dom next
We also need the dependencies for Contentlayer and its plugin:
npm install contentlayer next-contentlayer
And, of course, we need Code Hike:
npm install @code-hike/mdx
./posts
We are going to build a very minimal blog. Let's start with the content.
Create a ./posts/
folder with some files in it. For example, posts/one.mdx
and posts/two.mdx
.
We can add some metadata, like a title, using frontmatter. Contentlayer will understand that out of the box.
next.config.js
Then create a next.config.js
file at the root of your project.
Here we tell Next.js that we want to use Contentlayer using the withContentlayer
plugin from "next-contentlayer"
.
The {}
parameter is the Next.js configuration object. So if you need to set any option, you can do it there.
contentlayer.config.js
Contentlayer lets you have different types of content, each with its own schema. In our case, we'll have only one type of content: posts.
Create a configuration file for Contentlayer at the root of your project, and use defnineDocumentType
and makeSource
to define that the content comes from the ./posts
folder, that it should be treated as mdx.
Now we add Code Hike configuration.
Import remarkCodeHike
and pick a theme For more information about themes, see the themes docs.
Then add the Code Hike plugin to the mdx options. You can pass more options inside the configuration object if you need to, see the configuration docs.
There's one more thing we need to add to the contentlayer config: information about the document metadata.
We add two fields:
One for the title, that comes directly from the post frontmatter.
And another one for the URL, this one is computed from the path of the file.
jsconfig.json
We need to create one more config file.
Contentlayer generates files in the .contentlayer/generated
folder. To import those files, we need a jsconfig.json
file that tells the compiler where to look for the generated files.
If you are using typescript, you need the same but for the tsconfig.json
file.
pages/_app.js
Then we use pages/_app.js
file to import Code Hike's stylesheet.
You can find more information about the _app.js
file in the Next.js official docs.
If you want to customize Code Hike's styles with a global stylesheet make sure to import it after this import to avoid specificity issues.
You can learn more about customizing Code Hike styles in the styling docs.
pages/index.js
Now we can add a page to our Next.js app.
In pages/index.js
, we want to show a list of all the posts. So we import allPosts
from the generated files, and pass it as a prop to the page using getStaticProps
.
The posts
array will be something like this:
[ { title: "Post one", _id: "one.mdx", _raw: { ... }, body: { ... }, type: "Post", url: "/posts/one", }, { title: "Post two", ... },]
Now we render the lists of posts using the fields we defined in the document type: the post url and title.
If you run the app now, you should see the following:
pages/posts/[slug].js
To render a specific post, we use Next.js dynamic routes and create a [slug].js
page inside the pages/posts/
folder (the name of the posts
folder should be the same that we used when we created the url
field).
We use getStaticPaths
to tell Next.js the post URLs, and getStaticProps
to pass the post data to the page.
Then we finally render the post.
We use the useMDXComponent
hook from Contentlayer. It transforms the post.body.code
into a React component.
Now if we open the post in the browser, we should see the following:
./posts
We are going to build a very minimal blog. Let's start with the content.
Create a ./posts/
folder with some files in it. For example, posts/one.mdx
and posts/two.mdx
.
We can add some metadata, like a title, using frontmatter. Contentlayer will understand that out of the box.
next.config.js
Then create a next.config.js
file at the root of your project.
Here we tell Next.js that we want to use Contentlayer using the withContentlayer
plugin from "next-contentlayer"
.
The {}
parameter is the Next.js configuration object. So if you need to set any option, you can do it there.
contentlayer.config.js
Contentlayer lets you have different types of content, each with its own schema. In our case, we'll have only one type of content: posts.
Create a configuration file for Contentlayer at the root of your project, and use defnineDocumentType
and makeSource
to define that the content comes from the ./posts
folder, that it should be treated as mdx.
Now we add Code Hike configuration.
Import remarkCodeHike
and pick a theme For more information about themes, see the themes docs.
Then add the Code Hike plugin to the mdx options. You can pass more options inside the configuration object if you need to, see the configuration docs.
There's one more thing we need to add to the contentlayer config: information about the document metadata.
We add two fields:
One for the title, that comes directly from the post frontmatter.
And another one for the URL, this one is computed from the path of the file.
jsconfig.json
We need to create one more config file.
Contentlayer generates files in the .contentlayer/generated
folder. To import those files, we need a jsconfig.json
file that tells the compiler where to look for the generated files.
If you are using typescript, you need the same but for the tsconfig.json
file.
pages/_app.js
Then we use pages/_app.js
file to import Code Hike's stylesheet.
You can find more information about the _app.js
file in the Next.js official docs.
If you want to customize Code Hike's styles with a global stylesheet make sure to import it after this import to avoid specificity issues.
You can learn more about customizing Code Hike styles in the styling docs.
pages/index.js
Now we can add a page to our Next.js app.
In pages/index.js
, we want to show a list of all the posts. So we import allPosts
from the generated files, and pass it as a prop to the page using getStaticProps
.
The posts
array will be something like this:
[ { title: "Post one", _id: "one.mdx", _raw: { ... }, body: { ... }, type: "Post", url: "/posts/one", }, { title: "Post two", ... },]
Now we render the lists of posts using the fields we defined in the document type: the post url and title.
If you run the app now, you should see the following:
pages/posts/[slug].js
To render a specific post, we use Next.js dynamic routes and create a [slug].js
page inside the pages/posts/
folder (the name of the posts
folder should be the same that we used when we created the url
field).
We use getStaticPaths
to tell Next.js the post URLs, and getStaticProps
to pass the post data to the page.
Then we finally render the post.
We use the useMDXComponent
hook from Contentlayer. It transforms the post.body.code
into a React component.
Now if we open the post in the browser, we should see the following:
Again, a demo of Code Hike + Contentlayer is available on GitHub. You can also try it out from your browser on StackBlitz.