Why Code Blocks Deserve Special Attention Copied!
Technical articles depend heavily on readable code blocks. Raw HTML produced from Markdown is functional but rarely pleasant to read.
A typical Markdown code block becomes this.
<pre><code>const x = 10</code></pre>This project replaces that output with a richer component.
File.
src/components/ui/CodeBlock.astroThe component combines two tools.
- Shiki for syntax highlighting
- DaisyUI mockup code styles for visual presentation
Both work during the build process. The result is static HTML with styling already applied.
The Role of Shiki Copied!
Shiki is a syntax highlighter built on top of the same engine used by VS Code.
Instead of highlighting code in the browser, Shiki performs highlighting during the build.
Diagram.
Markdown code block
│
▼
MDX compiler
│
▼
Shiki highlighting
│
▼
Styled HTML tokens
│
▼
Static page outputExample input.
const greet = (name) => {
console.log("Hello " + name);
};Shiki produces HTML with token level styling.
<span class="line">
<span style="color:#C586C0">const</span>
<span style="color:#9CDCFE"> greet</span>
</span>Each token receives a color derived from a theme.
Shiki Configuration in Astro Copied!
Astro integrates Shiki directly through the MDX configuration.
File.
astro.config.mjsExample configuration.
import { defineConfig } from "astro/config";
export default defineConfig({
markdown: {
shikiConfig: {
theme: "github-dark",
wrap: true,
},
},
});Key settings.
- theme defines token colors
- wrap enables long line wrapping
Because highlighting runs during the build, the generated HTML already contains the color information.
This eliminates the need for runtime JavaScript.
Why Build Time Highlighting Matters Copied!
Many documentation platforms highlight code after the page loads.
A common example is highlight.js.
Typical flow.
HTML page loads
│
▼
highlight.js scans the DOM
│
▼
JavaScript parses code
│
▼
Tokens are highlightedThis approach introduces several drawbacks.
- extra JavaScript
- slower rendering
- layout shifts during highlighting
The journal system avoids this entirely.
Build time flow.
MDX file
│
▼
Astro build
│
▼
Shiki highlighting
│
▼
Static HTML
│
▼
Browser displays final resultFrom the reader perspective the code appears immediately.
The DaisyUI Mockup Code Component Copied!
Syntax colors alone do not create a good code block layout.
The project uses DaisyUI mockup code styles to define the container appearance.
The mockup component creates a layout similar to code editors.
Example structure.
<div class="mockup-code">
<pre data-prefix="$"><code>npm install</code></pre>
</div>Themockup-codeclass controls several visual elements.
- padding
- background color
- border radius
- line markers
- code spacing
This produces a container that resembles a terminal or editor window.
Diagram.
mockup-code container
│
├── line prefix
├── code lines
└── background stylingThe result feels closer to a development environment than a plain text block.
How Shiki and DaisyUI Work Together Copied!
Shiki and DaisyUI handle different layers.
Shiki manages token level color.
DaisyUI manages the block level layout.
Diagram.
CodeBlock Component
│
▼
Shiki
│
└── token colors
│
▼
DaisyUI mockup-code
│
└── layout stylingTogether they produce readable code blocks that match the site design.
Theme Integration Copied!
Both tools respond to the site theme.
DaisyUI controls background colors through theme variables.
Shiki controls token colors through its theme configuration.
Diagram.
Site Theme
│
├── DaisyUI variables
│ │
│ ▼
│ Code block background
│
└── Shiki theme
│
▼
Syntax token colorsThis keeps code blocks visually consistent with the rest of the interface.
Activating Custom Styles in the Journal Page Copied!
The journal route imports additional styling.
File.
src/pages/thejournal/[...slug].astroImport.
import "@styles/mockup-code.css";This stylesheet extends the DaisyUI component with custom rules.
It adjusts spacing, typography, and container behavior to match the journal layout.
Overview of mockup-code.css Copied!
File.
@styles/mockup-code.cssThe stylesheet modifies the base mockup component.
Key responsibilities.
Code Container Layout Copied!
The container rules control spacing and width.
.mockup-code {
margin-top: 1.5rem;
margin-bottom: 1.5rem;
}This separates code blocks from surrounding text.
Line Spacing Copied!
Readable code depends on proper line spacing.
.mockup-code pre {
line-height: 1.6;
}This prevents dense code blocks.
Horizontal Scrolling Copied!
Long lines require scroll behavior.
.mockup-code pre {
overflow-x: auto;
}Readers can scroll without breaking the layout.
Font Consistency Copied!
Code blocks must use a monospace font.
.mockup-code code {
font-family: var(--font-mono);
}This keeps alignment predictable.
Prefix Handling Copied!
Some blocks use prefixes such as terminal prompts.
.mockup-code pre[data-prefix]::before {
opacity: 0.5;
}The prefix appears visually distinct from the command.
The Final Rendering Pipeline Copied!
Putting everything together.
MDX Article
│
▼
Markdown code block
│
▼
Shiki highlighting
│
▼
CodeBlock component
│
▼
DaisyUI mockup layout
│
▼
Custom CSS adjustments
│
▼
Static HTML outputAll styling and highlighting exist before the browser loads the page.
The user sees formatted code instantly.
No client side processing occurs.
This approach keeps the journal fast while still delivering polished technical content.
