Code Highlighting
Clear and beautiful code snippets are essential for developer documentation. Docsta supports syntax highlighting out of the box using @tailwindcss/typography with the prose class and can be enhanced further with Shiki, Prism.js, or rehype plugins.
Whether you're writing in Markdown, MDX, or using content from Sanity, Docsta helps you highlight code effortlessly.
1. Using Tailwind Typography Plugin
Docsta includes the Tailwind Typography plugin for styled prose. Code blocks written in Markdown or MDX automatically receive nice default styles.
Example MDX:
<pre>
<code class="language-js">
```js function greet(name) { return `Hello, ${name}!` } ```
</code>
</pre>Installation (if not already added):
npm install @tailwindcss/typographyTailwind Config:
// tailwind.config.js
module.exports = {
plugins: [require('@tailwindcss/typography')],
}Usage:
<div className="prose prose-zinc dark:prose-invert max-w-none">
<article>{/* Rendered Markdown/MDX content */}</article>
</div>2. Adding Syntax Highlighting with Rehype + Shiki (Recommended)
For precise and themeable code syntax, you can use Shiki with rehype-shiki.
Setup with mdx-bundler or next-mdx-remote:
npm install shiki rehype-pretty-codeExample:
import rehypePrettyCode from 'rehype-pretty-code'
const mdxOptions = {
rehypePlugins: [
[
rehypePrettyCode,
{
theme: 'github-dark', // or 'dracula', 'nord', 'one-dark-pro'
},
],
],
}Custom Themes:
You can import your own Shiki themes (via JSON) or use popular presets like:
github-darkone-dark-prodraculanord
3. Optional: Prism.js
If you prefer Prism.js:
npm install prismjsAnd import a theme in your global CSS:
/* styles/globals.css */
@import 'prismjs/themes/prism-tomorrow.css';4. Rendering Code in Sanity
If you’re pulling code snippets from Sanity, you can create a custom portable text component:
export const PortableTextComponents = {
types: {
code: ({ value }) => (
<pre className="bg-zinc-900 text-white p-4 rounded-md overflow-x-auto">
<code className={`language-${value.language}`}>{value.code}</code>
</pre>
),
},
}5. Inline Code Styling
Use Tailwind’s prose defaults, or customize it:
.prose code {
background-color: #f4f4f5;
padding: 0.2rem 0.4rem;
border-radius: 0.25rem;
}For dark mode:
.dark .prose code {
background-color: #27272a;
}With Docsta’s flexible setup, you can highlight code cleanly, beautifully, and in sync with your brand—across Markdown, MDX, and CMS-powered content.
👉 Next Up: Learn how to add custom components like tabs, callouts, or live previews inside your MDX content.
What did you think of this content?
It was helpful
It was not helpful
I have a feedback