Skip to content
Merouane Mousselmal

Search

0 results

Type to search

WebTechnologies

WebTechnologies

MDX in practice: components for a technical blog

Learn how to use callouts, code tabs, link cards and figures in MDX articles, with live examples from this blog and the rules that keep builds from failing.

By Merouane MousselmalPublished 5 min read
On this page

Markdown is great for plain text, but technical articles often need more: a highlighted warning, the same command for three package managers, a captioned diagram. MDX solves this by letting you use components inside Markdown. This article walks through the four components built for this blog, shows a live example of each, and ends with the rules that keep them from breaking a build.

What MDX adds to Markdown

MDX is Markdown that can also contain components. You write headings, lists and code as usual, and wherever plain Markdown is not enough, you drop in a component such as <Callout>. The article is compiled into React and rendered as part of the page.

On this blog, every article is an .mdx file in the content folder. The build validates its frontmatter, renders the Markdown and the components, and publishes a static page.

A four-step diagram: write an MDX file, validate the frontmatter, render the Markdown and components, and publish a static HTML page.
From an MDX file to a published page on this blog.

The diagram above is itself one of the components. The Figures section below shows how it is written.

Callouts

A callout pulls a short message out of the flow of the text. There are three types: note, tip and warning.

This is how a callout is written:

<Callout type="tip" title="Optional title">
 
The message, written in normal Markdown.
 
</Callout>

The type can be note, tip or warning, and the default is note. Each callout also starts with a hidden label, so screen readers announce the type and the meaning never depends on color alone.

Code tabs

Many tutorials repeat the same command for npm, pnpm and yarn. Code tabs put the variants in one block, and the reader picks theirs. For example, installing the library that renders MDX on this blog:

npm install next-mdx-remote

Now start the development server:

npm run dev

Try switching to pnpm in one block and look at the other: both change together, because they share a group. The choice is also remembered for the next articles. Each block keeps its copy button, which copies the tab you can see.

This is the syntax. The outer fence below uses four backticks, so the code fences inside it are shown as text:

<CodeTabs labels="npm, pnpm, yarn" group="package-manager">
 
```bash
npm install next-mdx-remote
```
 
```bash
pnpm add next-mdx-remote
```
 
```bash
yarn add next-mdx-remote
```
 
</CodeTabs>

The number of code blocks must match the number of labels, and every label must be unique. Otherwise the build stops with a message that names the problem.

When an article points to a resource, a link card is clearer than a bare URL. It shows the title, the domain of the site and a short description, so readers know where the link leads before they click.

The syntax is a single self-closing tag:

<LinkCard
  href="https://mdxjs.com"
  title="MDX documentation"
  description="The official guide to the MDX format and its syntax."
/>

Links must use https, and they open in a new tab. The description is optional.

Figures

A figure is an image with a caption. The diagram near the top of this article is one:

<Figure
  src="/images/mdx-pipeline.png"
  alt="Describe what the image shows"
  caption="A short caption"
  wide
/>

A few things to know:

  • Images live in the public/images folder, and src starts with /images/
  • The alt text is required, because it is what screen readers and search engines use. Purely decorative images are the only exception.
  • Remote image URLs are rejected, which keeps pages fast and avoids requests to other sites
  • The width and height are read from the file automatically, so the page does not jump while the image loads
  • wide lets the figure extend beyond the text column on large screens
  • For photographs, add a credit (and an optional creditUrl) to name the author

Rules that keep the build green

Every rule below is checked when the site is built, so a mistake fails the build with a clear message and never reaches the live site.

  1. Leave blank lines between a component's tags and the Markdown inside
  2. Use only plain text values and bare attributes such as wide, never JavaScript expressions
  3. Give code tabs one code block per label, with unique labels
  4. Use local images with meaningful alt text
  5. Use https links in link cards

Takeaway

Components let a technical blog stay consistent without repeating formatting by hand. Write the article in Markdown, reach for a callout, code tabs, a link card or a figure when plain text is not enough, and let the build catch the mistakes.

Comments

Comments are optional. Loading them connects your browser to Giscus and GitHub. Read the privacy details.

Get new notes by email

Which topics do you want?

This site uses Google Analytics to count visits and see which articles are read. It only runs if you accept. Read the privacy page.