diff --git a/app.js b/app.js index 71df531..12545e8 100644 --- a/app.js +++ b/app.js @@ -68,6 +68,10 @@ function getAllBlogPosts(page = 1, postsPerPage = 5, searchQuery = '') { blogFiles = blogFiles.filter(file => file.toLowerCase().includes(lowerCaseQuery)); } + if (blogFiles.length === 0) { + return { blogPosts: [], totalPages: 0 }; // Return empty results if no files + } + blogFiles.sort((a, b) => { const statA = fs.statSync(path.join(__dirname, 'markdown', a)).birthtime; const statB = fs.statSync(path.join(__dirname, 'markdown', b)).birthtime; @@ -105,15 +109,19 @@ app.get('/', (req, res) => { const postsPerPage = 5; const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage, searchQuery); + const noResults = blogPosts.length === 0; // Check if there are no results + res.render('index', { - title: `${process.env.OWNER_NAME} Blog`, + title: `${process.env.OWNER_NAME}'s Blog`, blogPosts, currentPage: page, totalPages, - searchQuery // Pass search query to the view + searchQuery, // Pass search query to the view + noResults // Pass this flag to indicate no results found }); }); + // About Route app.get('/about', (req, res) => { res.render('about', { title: `About ${process.env.OWNER_NAME}` }); diff --git a/views/index.ejs b/views/index.ejs index 17ddde2..99cbf65 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -49,7 +49,11 @@
-

<%= searchQuery ? `Search results for "${searchQuery}"` : 'Recent Posts' %>

+ <% if (noResults) { %> +

Sorry, no blog posts found matching "<%= searchQuery %>"

+ <% } else { %> +

<%= searchQuery ? 'Search results for "' + searchQuery + '"' : 'Recent Posts' %>

+ <% } %>