-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cards for 3 most recent blog posts on homepage
Add NewsSummary.astro and NewsSummaryItem.astro to render the blog cards. Add getBlogImage() to dynamically get a linked image for a blog post. Remove Layout.astro. Fix the image name in 2020-02-06-code-for-san-francisco-2019-year-in-review.md.
- Loading branch information
1 parent
a58a6a9
commit 3f32d4f
Showing
7 changed files
with
110 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
import { getCollection } from "astro:content"; | ||
import NewsSummaryItem from "@/components/NewsSummaryItem.astro"; | ||
interface Props { | ||
title?: string; | ||
} | ||
const { title = "In the News" } = Astro.props; | ||
const posts = await getCollection("blog"); | ||
const recentPosts = posts.slice(-3); | ||
--- | ||
|
||
<section> | ||
<h2>{title}</h2> | ||
<div class="grid"> | ||
{recentPosts.map((post) => <NewsSummaryItem post={post} />)} | ||
</div> | ||
</section> | ||
|
||
<style> | ||
|
||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
import { getBlogImage } from "@/utils/getBlogImage"; | ||
interface Props { | ||
post: object; | ||
} | ||
const { post: { slug, data: { title, image } } } = Astro.props; | ||
const imageData = await getBlogImage(image); | ||
const backgroundImageURL = imageData?.src ?? ""; | ||
const postURL = "blog/" + slug; | ||
--- | ||
|
||
<article style={`background-image: url(${backgroundImageURL})`}> | ||
<a href={postURL}> | ||
<div class="dimmer"></div> | ||
<h3>{title}</h3> | ||
</a> | ||
</article> | ||
|
||
<style> | ||
article { | ||
aspect-ratio: 1; | ||
background-size: cover; | ||
background-position: center; | ||
padding: var(--block-spacing-horizontal); | ||
overflow: hidden; | ||
position: relative; | ||
flex-direction: column; | ||
justify-content: flex-end; | ||
display: flex; | ||
} | ||
|
||
article > a { | ||
text-decoration: none; | ||
} | ||
|
||
article h3 { | ||
--color: white; | ||
font-size: 1rem; | ||
margin-bottom: 0; | ||
position: relative; | ||
} | ||
|
||
.dimmer { | ||
background-color: rgba(0, 0, 0, 40%); | ||
width: 100%; | ||
height: 100%; | ||
left: 0; | ||
top: 0; | ||
transition: background-color .2s; | ||
position: absolute; | ||
} | ||
|
||
article:hover .dimmer { | ||
background-color: rgba(0, 0, 0, 25%); | ||
} | ||
</style> |
2 changes: 1 addition & 1 deletion
2
...astro/src/content/blog/2020-02-06-code-for-san-francisco-2019-year-in-review.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ImageMetadata } from "astro"; | ||
|
||
const images = import.meta.glob<{ default: ImageMetadata }>("/src/assets/blog/*.{jpeg,jpg,png,gif}"); | ||
|
||
export async function getBlogImage( | ||
filename: string): Promise<ImageMetadata | null> | ||
{ | ||
if (filename) { | ||
// this Vite import method requires that its parameter be a literal string, | ||
// not even one assembled from other literals | ||
const imagePath = "/src/assets/blog/" + filename.replace(/^\/img\/uploads\//, ""); | ||
|
||
if (images[imagePath]) { | ||
return (await images[imagePath]()).default; | ||
// return await images[imagePath](); | ||
} | ||
|
||
console.error(`Missing image: ${imagePath}`); | ||
} | ||
|
||
return null; | ||
} |