feat: redirect if page is below 1

This commit is contained in:
Cyber 2024-09-16 21:53:31 +02:00
parent 5a956cb708
commit 9caedb3f87

15
app.js
View File

@ -31,7 +31,7 @@ app.use(express.static(path.join(__dirname, 'public')));
// Function to load and parse markdown files and extract lead // Function to load and parse markdown files and extract lead
function loadMarkdownWithLead(file) { function loadMarkdownWithLead(file) {
const markdownContent = fs.readFileSync(path.join(__dirname, 'markdown', file), 'utf-8'); const markdownContent = fs.readFileSync(path.join(__dirname, 'markdown', file), 'utf-8');
let lead = ''; let lead = '';
let contentMarkdown = markdownContent; let contentMarkdown = markdownContent;
@ -74,7 +74,7 @@ function getAllBlogPosts(page = 1, postsPerPage = 5) {
const end = start + postsPerPage; const end = start + postsPerPage;
const paginatedFiles = blogFiles.slice(start, end); const paginatedFiles = blogFiles.slice(start, end);
const blogPosts = paginatedFiles.map(file => { const blogPosts = paginatedFiles.map(file => {
const title = file.replace('.md', '').replace(/-/g, ' '); // Keep original casing for title const title = file.replace('.md', '').replace(/-/g, ' '); // Keep original casing for title
const slug = titleToSlug(title); // Convert title to slug (lowercase) const slug = titleToSlug(title); // Convert title to slug (lowercase)
@ -82,7 +82,7 @@ function getAllBlogPosts(page = 1, postsPerPage = 5) {
// Get the last modified date of the markdown file // Get the last modified date of the markdown file
const stats = fs.statSync(path.join(__dirname, 'markdown', file)); const stats = fs.statSync(path.join(__dirname, 'markdown', file));
const lastModifiedDate = new Date(stats.mtime); // Use mtime for last modification time const lastModifiedDate = new Date(stats.mtime); // Use mtime for last modification time
// Format the date // Format the date
const formattedDate = lastModifiedDate.toLocaleDateString('en-US', { const formattedDate = lastModifiedDate.toLocaleDateString('en-US', {
year: 'numeric', year: 'numeric',
@ -103,10 +103,15 @@ function getAllBlogPosts(page = 1, postsPerPage = 5) {
// Home Route (Blog Home with Pagination) // Home Route (Blog Home with Pagination)
app.get('/', (req, res) => { app.get('/', (req, res) => {
const page = parseInt(req.query.page) || 1; const page = parseInt(req.query.page) || 1;
if (page < 1) {
return res.redirect(req.hostname);
}
const postsPerPage = 5; // Set how many posts to display per page const postsPerPage = 5; // Set how many posts to display per page
const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage); const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage);
res.render('index', { res.render('index', {
title: 'Raven Scott Blog', title: 'Raven Scott Blog',
blogPosts, blogPosts,
@ -185,7 +190,7 @@ app.post('/contact', (req, res) => {
app.get('/blog/:slug', (req, res) => { app.get('/blog/:slug', (req, res) => {
const slug = req.params.slug; const slug = req.params.slug;
const markdownFile = fs.readdirSync(path.join(__dirname, 'markdown')) const markdownFile = fs.readdirSync(path.join(__dirname, 'markdown'))
.find(file => titleToSlug(file.replace('.md', '')) === slug); .find(file => titleToSlug(file.replace('.md', '')) === slug);
if (markdownFile) { if (markdownFile) {
const originalTitle = markdownFile.replace('.md', ''); // Original title with casing const originalTitle = markdownFile.replace('.md', ''); // Original title with casing