next-markdown-blog
Version:
An npm package that allows Next.js users to create blogs using markdown
50 lines (49 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateRouteInfo = generateRouteInfo;
exports.generateAllRoutes = generateAllRoutes;
exports.parseRouteParams = parseRouteParams;
exports.generateStaticParams = generateStaticParams;
const markdown_js_1 = require("./markdown.js");
/**
* Generate route information for a blog post
*/
function generateRouteInfo(post, basePath) {
const fullPath = (0, markdown_js_1.generateRoutePath)(post.slug, post.category, basePath);
return {
slug: post.slug,
category: post.category,
fullPath,
};
}
/**
* Generate all route information for blog posts
*/
function generateAllRoutes(posts, basePath) {
return posts.map((post) => generateRouteInfo(post, basePath));
}
/**
* Parse route parameters from a URL path
*/
function parseRouteParams(pathname, basePath) {
const cleanBasePath = basePath.replace(/\/$/, '');
const pathWithoutBase = pathname.replace(cleanBasePath, '').replace(/^\//, '');
if (!pathWithoutBase) {
return null;
}
const pathParts = pathWithoutBase.split('/');
if (pathParts.length === 1) {
// /blog/slug format - category will be determined by finding the post
return {
slug: pathParts[0],
category: '', // Will be determined by finding the actual post
};
}
return null;
}
/**
* Generate static params for Next.js dynamic routes
*/
function generateStaticParams(posts, _basePath) {
return posts.map((post) => ({ slug: post.slug }));
}