refactor: migrate route from post/... to blog/...
This commit is contained in:
parent
03ce3caefd
commit
414cd7d3d7
12 changed files with 18 additions and 9 deletions
68
src/pages/blog/[...slug].astro
Normal file
68
src/pages/blog/[...slug].astro
Normal file
|
@ -0,0 +1,68 @@
|
|||
---
|
||||
import Layout from '../../layouts/Layout.astro';
|
||||
|
||||
import { getCollection, getEntry } from 'astro:content';
|
||||
import Comments from "../../components/Comments.astro";
|
||||
import {getImage} from "astro:assets";
|
||||
import {siteConfig} from "../../config";
|
||||
import ReplyViaEmail from "../../components/ReplyViaEmail.astro";
|
||||
import { ExtractFirstImage } from '../../plugins/extract-images';
|
||||
import AuthorInfo from "../../components/helper/authors/Info.astro";
|
||||
import TableOfContents from "../../components/TableOfContents.astro";
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const blogEntries = await getCollection('posts');
|
||||
return blogEntries.map(entry => ({
|
||||
params: { slug: entry.slug }, props: { entry },
|
||||
}));
|
||||
}
|
||||
|
||||
const { entry } = Astro.props;
|
||||
const { Content } = await entry.render();
|
||||
const headings = await entry.render().then(rendered => rendered.headings);
|
||||
|
||||
const noscript = siteConfig.noClientJavaScript
|
||||
const slug = Astro.params.slug;
|
||||
const author = entry.data.author || {collection: 'authors', id: siteConfig.defaultAuthor.id};
|
||||
|
||||
// Get author data
|
||||
const authorData = await getEntry(author);
|
||||
const authorInfo = authorData ? authorData.data : siteConfig.defaultAuthor;
|
||||
|
||||
// get featured image and use it as og:image
|
||||
// use the custom cover image if it exists, otherwise use the featured image file in the same directory
|
||||
const featuredImages = import.meta.glob(`/src/content/posts/*/featured.{avif,png,jpg,jpeg,webp}`,{import:'default',eager:true});
|
||||
const customFeaturedImage = entry.data.cover?.src
|
||||
const matchedImage = Object.keys(featuredImages).find(path => path.includes(slug));
|
||||
let matchedImage_src;
|
||||
if (matchedImage && !customFeaturedImage) {
|
||||
matchedImage_src = await getImage({src: featuredImages[matchedImage], format: 'webp'}) || null;
|
||||
}
|
||||
const firstImageURL = await ExtractFirstImage(Content)
|
||||
|
||||
const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `/blog/${slug}/featured.png` || '';
|
||||
---
|
||||
|
||||
<Layout
|
||||
title={entry.data.title}
|
||||
description={entry.data.description}
|
||||
ogImage={cover}
|
||||
author={authorInfo.name}
|
||||
>
|
||||
<h1 class="title">{entry.data.title}</h1>
|
||||
<AuthorInfo data={authorData} />
|
||||
<span class="date">{new Date(entry.data.pubDate).toISOString().split('T')[0]}</span>
|
||||
{headings.length !== 0 && <TableOfContents headings={headings} />}
|
||||
<div class="content">
|
||||
<Content />
|
||||
</div>
|
||||
|
||||
<div class="extra-post" style="margin-top: 2rem; border-top: 1px solid var(--border-color); padding-top: 1rem;">
|
||||
<ReplyViaEmail title={entry.data.title} email={authorInfo.email} />
|
||||
<br>
|
||||
<a href="/blog">← Back to posts</a>
|
||||
{noscript && <h2>Comments</h2> <Comments path={`blog/${slug}`} />}
|
||||
</div>
|
||||
|
||||
</Layout>
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue