Overview
TheBlogCard component displays a preview of your recent blog posts on the homepage. It shows your 2 most recent posts with title, description, and publication date.
Location
Source:src/components/sections/BlogCard.astro
Purpose
This component:- Showcases your latest blog content
- Displays post title, description, and date
- Links to individual blog post pages
- Provides a “View More” button to access all posts
- Uses line clamping to keep the preview clean and consistent
Data Source
The component fetches data from theposts Content Collection:
src/content/config.ts):
Usage
The component is used insrc/pages/index.astro:
This component spans 9 columns on large screens, giving it significant horizontal space to display blog content.
Sorting Logic
Posts are sorted by date in descending order (most recent first):Display Format
For each blog post, the component shows:- Title - Linked to the post detail page, with 2-line clamping
- Description - Short excerpt, also with 2-line clamping
- Publication Date - Formatted date
The
line-clamp-2 CSS class ensures that both titles and descriptions are truncated after 2 lines, maintaining consistent card heights.Date Formatting
The component uses theformateLocalDate() utility function to display dates in a localized format.
Adding Blog Posts
To add a new blog post that will appear in this component:- Create a new
.mdor.mdxfile insrc/content/posts/ - Add the required frontmatter:
Frontmatter Fields
title(required) - The post titlestartDate(required) - Publication datedescription(required) - Brief summary for previewsimage(optional) - Featured image object withurlandalttags(optional) - Array of tags for categorizationcanonical(optional) - Canonical URL if post is republished from elsewhere
View More Button
The component includes a link to view all blog posts:/posts route in your Astro site.
Customization
Change Number of Posts Displayed
Modify the slice parameter:Remove Line Clamping
If you want full titles and descriptions, remove theline-clamp-2 classes:
Add Read Time Estimation
You could extend the component to show estimated read time:- Update the schema to include
readTime:
- Display it in the component:
Styling
- Post titles use large font, semibold weight, and primary color
- Descriptions use small font and are muted
- Dates use gray color for visual hierarchy
- Hover effect adds underline to title links
- Consistent spacing creates clean separation
Related Components
- See the Card component for container styling