Overview
The blog utilities module provides server-side functions for reading, parsing, and rendering markdown blog posts with gray-matter frontmatter and remark HTML processing. File:src/lib/blog.ts
Type Definitions
BlogPost
Base blog post metadata structure:src/lib/blog.ts
Unique identifier derived from filename (without .md extension)
Post title from frontmatter
Short description/summary of the post
Publication date in ISO format (YYYY-MM-DD)
Estimated reading time (e.g., “5 min read”)
Hero image path for the post
URL-friendly identifier for the post
Post author name (optional)
Array of topic tags (optional)
Post category (optional)
Short excerpt or preview text (optional)
Full HTML content of the post (optional, populated by getPostData)
BlogPostWithContent
Extended interface that guarantees content is present:src/lib/blog.ts
Functions
getSortedPostsData()
Retrieves all blog posts sorted by date (newest first).Function Signature
BlogPost objects without content, sorted by date descending
Usage:
src/app/blog/page.tsx
- Reads all
.mdfiles fromsrc/content/blog/ - Parses frontmatter using gray-matter
- Sorts by date (latest first)
- Excludes post content for performance
getAllPostSlugs()
Retrieves slugs for all blog posts (used for static generation).Function Signature
slug property
Usage:
src/app/blog/post/[slug]/page.tsx
getPostData()
Fetches a single blog post with full HTML content.Function Signature
The post identifier (filename without .md extension)
BlogPostWithContent object with rendered HTML, or null if not found
Usage:
src/app/blog/post/[slug]/page.tsx
getPostBySlug()
Retrieves post metadata without content (lighter alternative to getPostData).Function Signature
The post identifier
BlogPost object without content, or null if not found
Usage:
Usage
Custom Styling
TheapplyCustomStyling() function transforms plain HTML into Spotify-themed content:
Styling Features
Headers
Gradient text effects, border accents, and background highlights
Links
Spotify green with hover effects, underlines, and background transitions
Code Blocks
Dark background with green text, borders, and shadows
Images
Figure captions, hover zoom, rounded corners, and shadows
HTML Transformations
The styling function modifies these elements:- H1: Gradient text from green to light green
- H2: Left border accent with background
- H3: Smaller left border with padding
- Paragraphs: Enhanced line height and tracking
- Links: Internal vs external styling with transitions
- Code: Inline and block code with Spotify theme
- Bold/Italic: Gradient backgrounds and accent styling
- Blockquotes: Border, gradient background, quotation marks
- Lists: Custom bullets with Spotify green
- Images: Figure wrapper with captions and hover effects
- Horizontal Rules: Gradient line with glow
Dependencies
package.json
Directory Structure
All blog utility functions run on the server side. They use Node.js
fs module to read files and should not be called from client components.Error Handling
ThegetPostData() function includes try-catch error handling:
Error Handling
Performance Considerations
- getSortedPostsData(): Fast - only parses frontmatter, not content
- getPostData(): Slower - processes full markdown to HTML with styling
- Static Generation: Use
getAllPostSlugs()+getPostData()ingenerateStaticParams()for optimal performance