express-markdown-blog-middleware
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Express Markdown Blog Middleware

Express middleware for rendering markdown blog posts with pagination.

Features

  • Markdown blog post rendering with HTML output
  • Blog utilities for easy post management
  • Cache support
  • TypeScript support
  • API endpoints for posts
  • Tag support

Installation

npm install express-markdown-blog-middleware

Quick Start

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);

Directory Structure

your-project/
  └── content/           # Markdown blog posts
      ├── post-1.md
      └── post-2.md

Blog Post Format

---
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...

Blog Utils

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');

Configuration Options

createBlogMiddleware(contentDir, {
  // Cache duration in ms (default: 5 minutes)
  cacheMaxAge: 5 * 60 * 1000
});

TypeScript Support

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);
});

License

MIT

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Package Sidebar

Install

npm i express-markdown-blog-middleware

Weekly Downloads

252

Version

1.1.0

License

MIT

Unpacked Size

11.5 kB

Total Files

6

Last publish

Collaborators

  • cond