How to Create a Custom Code Block With Nuxt Content v2
Code blocks are essential for blogs about software development. In this article, I want to show you how can define a custom code block component in Nuxt Content v2 with the following features:
- Custom styling for code blocks inside Markdown files
- Show language name (if available)
- Show file name (if available)
- Show a "Copy Code" button
Nuxt Content v2
Nuxt Content v2 is a Nuxt 3 module that reads local files from the /content
directory in your project. It supports .md
, .yml
, .csv
and .json
files. Additionally, it's possible to use Vue components in Markdown with the MDC Syntax.
Setup Nuxt App
First, let's start a new Nuxt Content project with:
Then we need to install the dependencies in the nuxt-custom-code-blocks
folder:
Now we can start the Nuxt content app in development mode:
A browser window should automatically open for http://localhost:3000
. Alternatively, you can start playing with Nuxt Content in your browser using StackBlitz or CodeSandbox.
The following StackBlitz sandbox demonstrates the application we create in this article:
Custom Prose Component
Prose represents the HTML tags output from the Markdown syntax in Nuxt Content. Nuxt Content provides a Vue component for each HTML tag like links, title levels, etc.
It's possible to override these Vue components, which is precisely what we'll do to create a custom code block component.
To customize a Prose component, we have to perform these steps:
- Check out the original component sources.
- Use the same props.
- Name it the same in our
components/content/
directory.
In our example, we want to override ProseCode, which is Nuxt Content's default Vue component to render code blocks in Markdown files.
This component accepts the following props:
code
: the provided code as a stringlanguage
: the provided language namefilename
: the provided filenamehighlights
: a list of highlighted line numbers
Let's take a look at how we can set these values in a Markdown file:
In the above example:
js
is the value passed to thelanguage
propsrc/index.js
is the value passed to thefilename
prop[1, 2, 3]
is the value passed to thehighlights
prop
To override the component, we create ProseCode.vue
in the components/content
directory and use the exact same props that are defined in the default component:
Now we can customize this component however we want.
Style Container
First, we want to style the container that includes the code. Therefore, we wrap the <slot />
in a div
and style it:
Let's take a look at our custom code block:
Show Language
Next, we want to show the name of the language on the top right, if it is available.
We define a map called languageMap
that contains the displayed text, the CSS background, and text color for each programming language.
We style the span
tag that renders the language inside our template based on this map and the provided language
prop:
Show File Name
Next, we want to show the file's name on the top left, if it is available:
The result looks like this:
Add Copy Code Button
Finally, we want to show a button that copies the code to the clipboard. Therefore, we use the useClipboard composable from VueUse:
Let's take a look at the final result with language & file name, copy code button, and line highlighting:
Conclusion
Custom code blocks are essential for my blog as my blog posts contain a lot of code snippets. Features like copy code or line highlighting provide excellent value to my readers, and it is straightforward to add such features by creating a custom code block component in Nuxt Content v2.
The source code of this demo is available at GitHub or as StackBlitz sandbox.
You can expect more Nuxt 3 posts in the following months as I plan to blog about interesting topics that I discover while rewriting my portfolio website.
If you liked this article, follow me on Twitter to get notified about new blog posts and more content from me.
Alternatively (or additionally), you can also subscribe to my newsletter.
Create a Blog With Nuxt Content v2
I prefer simple Markdown files as the content source for my blog posts. In this article, I want to show you how can set up a simple blog using Nuxt Content v2.
Create an RSS Feed With Nuxt 3 and Nuxt Content v2
My portfolio website is built with Nuxt 3 and Nuxt Content v2. An RSS feed with my latest five blog posts is available here. In this article, you'll learn how to add an RSS feed to your Nuxt website.