Express middleware for rendering markdown blog posts with pagination.
- Markdown blog post rendering with HTML output
- Blog utilities for easy post management
- Cache support
- TypeScript support
- API endpoints for posts
- Tag support
npm install express-markdown-blog-middleware
const express = require('express');
const path = require('path');
const createBlogMiddleware = require('express-markdown-blog-middleware');
const app = express();
// Initialize blog middleware
app.use(createBlogMiddleware(path.join(__dirname, 'content')));
// Use blog utilities
app.get('/api/posts', (req, res) => {
const posts = req.blog.getAllPosts();
res.json(posts);
});
app.get('/api/posts/:slug', (req, res) => {
const post = req.blog.getPost(req.params.slug);
if (!post) return res.status(404).json({ error: 'Post not found' });
res.json(post);
});
app.listen(3000);
your-project/
└── content/ # Markdown blog posts
├── post-1.md
└── post-2.md
---
title: My First Post
date: 2024-03-15
description: This is my first blog post
keywords: [blog, first post]
lang: en
---
# My First Post
Blog post content here...
The middleware adds blog utilities to the request object:
// Get all posts in English
const posts = req.blog.getPostsByLang('en');
// Get paginated posts in Turkish
const posts = req.blog.getPaginatedPosts(1, 10, 'tr');
// Get single post in Spanish
const post = req.blog.getPost('post-slug', 'es');
// Get posts by tag in German
const posts = req.blog.getPostsByTag('javascript', 'de');
createBlogMiddleware(contentDir, {
// Cache duration in ms (default: 5 minutes)
cacheMaxAge: 5 * 60 * 1000
});
Types are included and will be automatically available when using TypeScript.
import { Request } from 'express';
interface BlogPost {
title: string;
date: string;
description?: string;
keywords?: string[];
content: string;
slug: string;
}
// Blog utilities are typed
app.get('/posts', (req: Request, res) => {
const posts: BlogPost[] = req.blog.getAllPosts();
res.json(posts);
});
MIT
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.