first commit
This commit is contained in:
90
templates/starter-cloudflare/src/pages/posts/[slug].astro
Normal file
90
templates/starter-cloudflare/src/pages/posts/[slug].astro
Normal file
@@ -0,0 +1,90 @@
|
||||
---
|
||||
import { getEmDashEntry, getEntryTerms, getSeoMeta } from "emdash";
|
||||
import { Image, PortableText, WidgetArea } from "emdash/ui";
|
||||
import Base from "../../layouts/Base.astro";
|
||||
|
||||
const { slug } = Astro.params;
|
||||
|
||||
if (!slug) {
|
||||
return Astro.redirect("/404");
|
||||
}
|
||||
|
||||
const { entry: post, cacheHint } = await getEmDashEntry("posts", slug);
|
||||
|
||||
if (!post) {
|
||||
return Astro.redirect("/404");
|
||||
}
|
||||
|
||||
Astro.cache.set(cacheHint);
|
||||
|
||||
// SEO meta from content fields
|
||||
const seo = getSeoMeta(post, {
|
||||
siteTitle: "My Site",
|
||||
siteUrl: Astro.url.origin,
|
||||
path: `/posts/${slug}`,
|
||||
});
|
||||
|
||||
// Taxonomy terms for this post (use post.data.id, not post.id)
|
||||
const tags = await getEntryTerms("posts", post.data.id, "tag");
|
||||
const categories = await getEntryTerms("posts", post.data.id, "category");
|
||||
---
|
||||
|
||||
<Base
|
||||
title={seo.title}
|
||||
description={seo.description}
|
||||
canonical={seo.canonical}
|
||||
image={seo.ogImage}
|
||||
content={{ collection: "posts", id: post.data.id, slug }}
|
||||
>
|
||||
<article>
|
||||
{
|
||||
post.data.featured_image && (
|
||||
<div {...post.edit.featured_image}>
|
||||
<Image image={post.data.featured_image} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
<h1 {...post.edit.title}>{post.data.title}</h1>
|
||||
|
||||
{
|
||||
post.data.publishedAt && (
|
||||
<time datetime={post.data.publishedAt.toISOString()}>
|
||||
{post.data.publishedAt.toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
})}
|
||||
</time>
|
||||
)
|
||||
}
|
||||
|
||||
{
|
||||
categories.length > 0 && (
|
||||
<div>
|
||||
{categories.map((c) => (
|
||||
<a href={`/category/${c.slug}`}>{c.label}</a>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
<div {...post.edit.content}>
|
||||
<PortableText value={post.data.content} />
|
||||
</div>
|
||||
|
||||
{
|
||||
tags.length > 0 && (
|
||||
<div>
|
||||
{tags.map((t) => (
|
||||
<a href={`/tag/${t.slug}`}>{t.label}</a>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
</article>
|
||||
|
||||
<aside>
|
||||
<WidgetArea name="sidebar" />
|
||||
</aside>
|
||||
</Base>
|
||||
49
templates/starter-cloudflare/src/pages/posts/index.astro
Normal file
49
templates/starter-cloudflare/src/pages/posts/index.astro
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
import { getEmDashCollection, getEntryTerms } from "emdash";
|
||||
import { Image } from "emdash/ui";
|
||||
import Base from "../../layouts/Base.astro";
|
||||
|
||||
const { entries: posts, cacheHint } = await getEmDashCollection("posts", {
|
||||
orderBy: { published_at: "desc" },
|
||||
});
|
||||
Astro.cache.set(cacheHint);
|
||||
|
||||
// Fetch tags for each post
|
||||
const postsWithTags = await Promise.all(
|
||||
posts.map(async (post) => {
|
||||
const tags = await getEntryTerms("posts", post.data.id, "tag");
|
||||
return { post, tags };
|
||||
})
|
||||
);
|
||||
---
|
||||
|
||||
<Base title="All Posts">
|
||||
<h1>Posts</h1>
|
||||
|
||||
{
|
||||
postsWithTags.length === 0 ? (
|
||||
<p>No posts yet.</p>
|
||||
) : (
|
||||
<ul>
|
||||
{postsWithTags.map(({ post, tags }) => (
|
||||
<li>
|
||||
<a href={`/posts/${post.id}`}>
|
||||
{post.data.featured_image && (
|
||||
<Image image={post.data.featured_image} />
|
||||
)}
|
||||
<h2>{post.data.title}</h2>
|
||||
</a>
|
||||
{post.data.excerpt && <p>{post.data.excerpt}</p>}
|
||||
{tags.length > 0 && (
|
||||
<div>
|
||||
{tags.map((t) => (
|
||||
<a href={`/tag/${t.slug}`}>{t.label}</a>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
</Base>
|
||||
Reference in New Issue
Block a user