refactor: move pubDate to date in posts front matter

This commit is contained in:
草师傅 2025-08-11 17:44:56 +08:00
parent 123da1747f
commit b2270003bd
Signed by: gb
GPG key ID: 43330A030E2D6478
15 changed files with 21 additions and 22 deletions

View file

@ -4,7 +4,7 @@ const { posts, displayDate = false, placeholder = false } = Astro.props;
<div style="margin-top: 1rem; margin-left: 1rem;"> <div style="margin-top: 1rem; margin-left: 1rem;">
{posts.map((post) => ( {posts.map((post) => (
<p> <p>
{displayDate && <span class="list-date">{new Date(post.data.pubDate).toISOString().split('T')[0]}</span>} {displayDate && <span class="list-date">{new Date(post.data.date).toISOString().split('T')[0]}</span>}
<a href={`/blog/${post.slug}`}>{post.data.title}</a> <a href={`/blog/${post.slug}`}>{post.data.title}</a>
</p> </p>
))} ))}

View file

@ -75,7 +75,7 @@ const domain = Astro.url.host
title: string; title: string;
description?: string; description?: string;
slug?: string; slug?: string;
pubDate?: Date; date?: Date;
searchableTitle: string; searchableTitle: string;
searchableDesc: string; searchableDesc: string;
searchableContent: string; searchableContent: string;
@ -199,7 +199,7 @@ const domain = Astro.url.host
<div class="search-result"> <div class="search-result">
<a href="/blog/${item.slug}" tabindex="0"> <a href="/blog/${item.slug}" tabindex="0">
<span class="result-title">${item.title}</span><br /> <span class="result-title">${item.title}</span><br />
<span class="result-date">${new Date(item.pubDate).toISOString().split('T')[0]|| '' } — <span class="result-date">${new Date(item.date).toISOString().split('T')[0]|| '' } —
<em>${item.description || ''}</em></span> <em>${item.description || ''}</em></span>
</a> </a>
</div> </div>

View file

@ -5,8 +5,7 @@ export const posts = ({ image }) => z.object({
title: z.string(), title: z.string(),
description: z.string(), description: z.string(),
summary: z.string().optional(), summary: z.string().optional(),
pubDate: z.coerce.date(), date: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
categories: z.array(z.string()).default(['uncategorized']), categories: z.array(z.string()).default(['uncategorized']),
tags: z.array(z.string()).optional(), tags: z.array(z.string()).optional(),
cover: image().optional(), cover: image().optional(),

View file

@ -1,7 +1,7 @@
--- ---
title: 'First Post' title: 'First Post'
description: 'Welcome to my terminal blog' description: 'Welcome to my terminal blog'
pubDate: '2025-06-01' date: '2025-06-01'
--- ---
Hello world! This is the first post on my new terminal blog. Hello world! This is the first post on my new terminal blog.

View file

@ -1,6 +1,6 @@
--- ---
title: "Markdown Syntax Example" title: "Markdown Syntax Example"
pubDate: '2023-07-15T12:00:00Z' date: '2023-07-15T12:00:00Z'
description: "Sample article showcasing basic Markdown syntax and formatting for HTML elements." description: "Sample article showcasing basic Markdown syntax and formatting for HTML elements."
tags: ["markdown", "css", "html", "sample"] tags: ["markdown", "css", "html", "sample"]
--- ---
@ -98,9 +98,9 @@ This most simple one can be used like this:
<LinkCard url="https://astro.build"/> <LinkCard url="https://astro.build"/>
``` ```
or with a archive link: or with a archive link:
<LinkCard url="https://astro.build" showArchive="true" pubDate={frontmatter.pubDate} updatedDate={frontmatter.updatedDate} /> <LinkCard url="https://astro.build" showArchive="true" pubDate={frontmatter.date} updatedDate={frontmatter.updatedDate} />
```mdx ```mdx
<LinkCard url="https://astro.build" showArchive="true" pubDate={frontmatter.pubDate} updatedDate={frontmatter.updatedDate} /> <LinkCard url="https://astro.build" showArchive="true" pubDate={frontmatter.date} updatedDate={frontmatter.updatedDate} />
``` ```
<Callout type="info"> <Callout type="info">
The `pubDate` or `updatedDate` props is optional, but in order to make the component to display the archived version around the date correctly, they should be passed. The `pubDate` or `updatedDate` props is optional, but in order to make the component to display the archived version around the date correctly, they should be passed.

View file

@ -2,7 +2,7 @@
title: 'The Art of Minimalism' title: 'The Art of Minimalism'
description: 'Thoughts on minimalism in design and code' description: 'Thoughts on minimalism in design and code'
summary: 'Exploring the principles of minimalism and its application in design and coding practices.' summary: 'Exploring the principles of minimalism and its application in design and coding practices.'
pubDate: '2025-06-05' date: '2025-06-05'
author: 'Wheatley' author: 'Wheatley'
tags: tags:
- 'design' - 'design'

View file

@ -1,7 +1,7 @@
--- ---
title: 'My Terminal Setup' title: 'My Terminal Setup'
description: 'A walkthrough of my current terminal configuration' description: 'A walkthrough of my current terminal configuration'
pubDate: '2025-06-08' date: '2025-06-08'
author: author:
- 'Glados' - 'Glados'
- 'Wheatley' - 'Wheatley'

View file

@ -10,7 +10,7 @@ const lang = getLangFromUrl(Astro.url);
const t = useTranslations(lang); const t = useTranslations(lang);
const translatePath = useTranslatedPath(lang); const translatePath = useTranslatedPath(lang);
const posts = await getCollection('posts'); const posts = await getCollection('posts');
posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime()); posts.sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime());
--- ---
<Layout title="Blog Posts" description="List all posts on the website."> <Layout title="Blog Posts" description="List all posts on the website.">

View file

@ -34,7 +34,7 @@ const author = Array.isArray(entry.data.author) ? entry.data.author : (entry.dat
const wordcount = remarkPluginFrontmatter.wordcount; const wordcount = remarkPluginFrontmatter.wordcount;
const lastUpdated = remarkPluginFrontmatter.lastModified; const lastUpdated = remarkPluginFrontmatter.lastModified;
const pubDate = new Date(entry.data.pubDate).toISOString().split('T')[0] const pubDate = new Date(entry.data.date).toISOString().split('T')[0]
const lastUpdatedDate = new Date(lastUpdated).toISOString().split('T')[0] const lastUpdatedDate = new Date(lastUpdated).toISOString().split('T')[0]
// Get author data // Get author data

View file

@ -11,7 +11,7 @@ export async function GET({ props }) {
const { entry } = props; const { entry } = props;
// Format the content as plain text // Format the content as plain text
const title = entry.data.title; const title = entry.data.title;
const date = entry.data.pubDate.toISOString().split('T')[0]; const date = entry.data.date.toISOString().split('T')[0];
const content = entry.body; const content = entry.body;
// Combine the post info and body into a single text file // Combine the post info and body into a single text file

View file

@ -19,10 +19,10 @@ ${await generateMarkdownSection("pages", "Page", baseUrl)}`;
async function generateMarkdownSection(collectionName, sectionTitle, baseUrl) { async function generateMarkdownSection(collectionName, sectionTitle, baseUrl) {
const items = await getCollection(collectionName); const items = await getCollection(collectionName);
// Sort posts by pubDate if available // Sort posts by date if available
const sortedItems = const sortedItems =
collectionName === "posts" collectionName === "posts"
? items.sort((a, b) => (a.data.pubDate > b.data.pubDate ? -1 : 1)) ? items.sort((a, b) => (a.data.date > b.data.date ? -1 : 1))
: items; : items;
let markdown = `## ${sectionTitle}\n`; let markdown = `## ${sectionTitle}\n`;

View file

@ -23,7 +23,7 @@ export async function GET(context) {
// Load the content collection entries to add to our RSS feed. // Load the content collection entries to add to our RSS feed.
const posts = (await getCollection("posts")).sort((a, b) => const posts = (await getCollection("posts")).sort((a, b) =>
// Sort by publication date descending. // Sort by publication date descending.
a.data.pubDate > b.data.pubDate ? -1 : 1 a.data.date > b.data.date ? -1 : 1
); );
// Loop over blog posts to create feed items for each, including full content. // Loop over blog posts to create feed items for each, including full content.
@ -57,7 +57,7 @@ export async function GET(context) {
feedItems.push({ feedItems.push({
title: post.data.title, title: post.data.title,
description: post.data.description || '', description: post.data.description || '',
pubDate: post.data.pubDate, date: post.data.date,
link: `${baseUrl}/blog/${post.slug}`, link: `${baseUrl}/blog/${post.slug}`,
content, content,
}); });

View file

@ -6,7 +6,7 @@ export async function GET() {
title: post.data.title, title: post.data.title,
description: post.data.description, description: post.data.description,
content: post.body, content: post.body,
pubDate: post.data.pubDate, date: post.data.date,
slug: post.slug slug: post.slug
})); }));

View file

@ -3,10 +3,10 @@ import { getCollection } from 'astro:content';
export async function GET(context) { export async function GET(context) {
const posts = await getCollection('posts'); const posts = await getCollection('posts');
// Sort posts by date (newest first) // Sort posts by date (newest first)
posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime()); posts.sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime());
const twtxt = posts.map(post => ( const twtxt = posts.map(post => (
`${new Date(post.data.pubDate).toISOString()}\t${post.data.title} ${context.site}blog/${post.slug}` `${new Date(post.data.date).toISOString()}\t${post.data.title} ${context.site}blog/${post.slug}`
)).join('\n'); )).join('\n');
return new Response(twtxt, { return new Response(twtxt, {

View file

@ -6,7 +6,7 @@ async function fetchPostsData() {
for (const post of posts) { for (const post of posts) {
const { remarkPluginFrontmatter } = await post.render(); const { remarkPluginFrontmatter } = await post.render();
const dateKey = post.data.pubDate.toISOString().split('T')[0]; // "2025-07-25" const dateKey = post.data.date.toISOString().split('T')[0]; // "2025-07-25"
entriesData[dateKey] = { entriesData[dateKey] = {
wordCount: remarkPluginFrontmatter.wordcount.words / 1000 || 0, wordCount: remarkPluginFrontmatter.wordcount.words / 1000 || 0,
link: `/blog/${post.slug}`, link: `/blog/${post.slug}`,