Markdown, since its introduction to the web, has allowed content creators (such as technical writers and documentation engineers, etc.) to have a more simplistic tool to do their authoring in. In this article, we will have a look at MDX and how to get started with it in Nuxt.js applications.
MDX is a syntax extension that allows you to write JSX in your Markdown documents. Also, according to the official MDX documentation, MDX is:
An authorable format that lets you seamlessly write JSX in your Markdown documents. You can import components, such as interactive charts or alerts, and embed them within your content
So basically, MDX is Markdown with JSX. Before MDX, writing JSX in Markdown was a hassle because the simplicity of authoring Markdown was lost in the process of sprinkling JSX in it.
MDX opens the door to a lot of fun applications to Markdown documents. For example, the Chakra UI documentation website uses MDX to allow readers to edit components and see their changes live on the page.
MDX is useful for design systems documentations like we mentioned above with the Chakra UI documentation usage of MDX. MDX also allows you to import Markdown(.md or .mdx) files as components!
Though JSX was originally used in React projects, you can now use it as well in Vue.js. With that said, you can freely integrate MDX in your Vue.js projects.
.md
or .mdx
files as Vue componentsLet’s see these features in action by integrating MDX and Vue. We will get started by creating a fresh Vue project. Using the Vue CLI run:
vue create mdx-vue-demo
After creating the Vue projects we will add MDX support by installing @mdx-js/vue-loader
which is the official loader that makes it possible to use MDX in Vue. Let’s install it below:
npm install @mdx-js/vue-loader
After we’ve installed the loader we will then include the loader inside our project’s webpack config. Since we are using Vue CLI, we can create a vue.config.js
file in the root of our projects, and on the configureWebpack
property, we include @mdx-js/vue-loader
under the module rules:
// vue.config.js
module.exports = {
configureWebpack: {
module: {
rules: [
{
test: /\.mdx?$/,
use: ['babel-loader', '@mdx-js/vue-loader'],
},
],
},
},
};
What the code means is that any file ending with .md
or .mdx
will be handled by both babel-loader
and @mdx-js/vue-loader
The step above is still the same if you are configuring your Vue application using webpack. You just have to include the loader in webpack.config.js
likes so:
// webpack.config.js
module: {
rules: [
// ...
{
test: /\.mdx?$/,
use: ['babel-loader', '@mdx-js/vue-loader']
}
]
}
Now that we have both installed and configured @mdx-js/vue-loader
let’s create a md
file in src/
directory and render it as a Vue component in src/App.vue
. We will create MyFirstMdx.md
in the components/
directory and add the following content:
## Hello LogRocket
from markdown powered by MDX
Import the .mdx
file in src/App.vue
as you will import a Vue component. Like so:
import MyFirstMDX from '@/components/MyFirstMdx.md'
We will register the component:
components: {
MyFirstMdx
}
Then replace the template in App.vue
with <my-first-mdx />
. The App.vue
file should now look like this:
<template>
<div id="app">
<my-first-mdx />
</div>
</template>
<script>
import MyFirstMdx from "./components/MyFirstMdx.mdx";
export default {
name: "App",
components: {
MyFirstMdx,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Start the Vue development server and you will see the content of MyFirstMdx.md
when you visit your app in the browser.
#nuxt #vue #javascript #programming #developer