From eaa8eccb79e7d266d1e10fd3c5e7506356378e0d Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 16 Sep 2024 13:42:27 -0400 Subject: [PATCH] Adding list and pagination] --- app.js | 47 ++++++++++++++++++++++++++++++++++++------- public/css/styles.css | 43 +++++++++++++++++++++++++++++++++++---- views/index.ejs | 41 ++++++++++++++++++++++++++++--------- 3 files changed, 110 insertions(+), 21 deletions(-) diff --git a/app.js b/app.js index e925847..7e97d2d 100644 --- a/app.js +++ b/app.js @@ -62,23 +62,56 @@ function slugToTitle(slug) { return slug.replace(/-/g, ' '); } -// Function to load all blog posts -function getAllBlogPosts() { +// Function to load all blog posts with pagination support +function getAllBlogPosts(page = 1, postsPerPage = 5) { const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md')); - return blogFiles.map(file => { + + // Paginate the results + const totalPosts = blogFiles.length; + const totalPages = Math.ceil(totalPosts / postsPerPage); + const start = (page - 1) * postsPerPage; + const end = start + postsPerPage; + + const paginatedFiles = blogFiles.slice(start, end); + + const blogPosts = paginatedFiles.map(file => { const title = file.replace('.md', '').replace(/-/g, ' '); // Keep original casing for title const slug = titleToSlug(title); // Convert title to slug (lowercase) + + // Get the last modified date of the markdown file + const stats = fs.statSync(path.join(__dirname, 'markdown', file)); + const lastModifiedDate = new Date(stats.mtime); // Use mtime for last modification time + + // Format the date + const formattedDate = lastModifiedDate.toLocaleDateString('en-US', { + year: 'numeric', + month: 'long', + day: 'numeric' + }); + return { title, // Original casing title - slug + slug, + date: formattedDate // Include the formatted date }; }); + + return { blogPosts, totalPages }; } -// Home Route (Blog Home) +// Home Route (Blog Home with Pagination) app.get('/', (req, res) => { - const blogPosts = getAllBlogPosts(); - res.render('index', { title: 'Raven Scott Blog', blogPosts }); + const page = parseInt(req.query.page) || 1; + const postsPerPage = 5; // Set how many posts to display per page + + const { blogPosts, totalPages } = getAllBlogPosts(page, postsPerPage); + + res.render('index', { + title: 'Raven Scott Blog', + blogPosts, + currentPage: page, + totalPages + }); }); // About Route diff --git a/public/css/styles.css b/public/css/styles.css index 9646f99..00b31fe 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -11,6 +11,11 @@ main { flex-grow: 1; } +a { + color: #000; + text-decoration: none; +} + .bg-primary { --bs-bg-opacity: 1; background-color: rgb(0 0 0) !important; @@ -49,15 +54,45 @@ p.lead { font-size: 1.5rem; } -.btn-primary { +/* Read Article Button Styling */ +.btn-outline-primary { font-size: 1.25rem; padding: 10px 20px; + color: #ffffff; + border: 2px solid #000000; background-color: #000000; - border: none; + transition: background-color 0.3s ease, color 0.3s ease; } -.btn-primary:hover { - background-color: #000000; +.btn-outline-primary:hover { + background-color: #2c5364; + border-color: #2c5364; + color: #ffffff; +} + +/* Pagination Styling */ +.pagination { + margin-top: 20px; +} + +.pagination .page-item .page-link { + color: #ffffff; + background-color: #1e1e1e; + border: 1px solid #2c5364; + padding: 10px 15px; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.pagination .page-item.active .page-link { + background-color: #2c5364; + border-color: #2c5364; + color: #ffffff; +} + +.pagination .page-item .page-link:hover { + background-color: #2c5364; + border-color: #2c5364; + color: #ffffff; } footer { diff --git a/views/index.ejs b/views/index.ejs index e4e5096..9c22229 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -31,7 +31,6 @@ -

Welcome to my long form post blog

@@ -42,18 +41,40 @@

Recent Posts

-
+ + + +