Skip to content

Commit

Permalink
Add Date object to post metadata
Browse files Browse the repository at this point in the history
Avoid constructing a new Date multiple times since it's error-prone (see #206)
  • Loading branch information
n0samu committed Jan 17, 2024
1 parent 8e27492 commit 8504f40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function generateMetadata({
params: PostPath;
}): Promise<Metadata> {
const post = getPostData(
`${params.year}-${params.month}-${params.date}-${params.slug}.markdown`,
`${params.year}-${params.month}-${params.day}-${params.slug}.markdown`,
);
const baseUrl = process.env.BASE_URL || "";
return {
Expand All @@ -20,11 +20,7 @@ export async function generateMetadata({
type: "article",
title: post.title,
siteName: "Ruffle",
publishedTime: new Date(
Number(post.year),
Number(post.month),
Number(post.date),
).toISOString(),
publishedTime: post.date.toISOString(),
authors: [post.author],
images: post.images?.map((path) => baseUrl + path),
videos: post.videos?.map((path) => baseUrl + path),
Expand All @@ -34,7 +30,7 @@ export async function generateMetadata({

export default function Page({ params }: { params: PostPath }) {
const post = getPostData(
`${params.year}-${params.month}-${params.date}-${params.slug}.markdown`,
`${params.year}-${params.month}-${params.day}-${params.slug}.markdown`,
);
return (
<Container size="xl">
Expand Down
4 changes: 2 additions & 2 deletions src/app/blog/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function BlogPostAndIcon({ metadata, type }: BlogPostProps) {
}

export function BlogPost({ metadata, type }: BlogPostProps) {
const url = `/blog/${metadata.year}/${metadata.month}/${metadata.date}/${metadata.slug}`;
const url = `/blog/${metadata.year}/${metadata.month}/${metadata.day}/${metadata.slug}`;
return (
<Stack gap={0} className={classes.postInfo}>
{type == "full" ? (
Expand All @@ -46,7 +46,7 @@ export function BlogPost({ metadata, type }: BlogPostProps) {
</Text>
<Text size="sm" c="dimmed">
{metadata.year}-{metadata.month}-{metadata.date}
{metadata.year}-{metadata.month}-{metadata.day}
</Text>
</Group>
<div>
Expand Down
19 changes: 9 additions & 10 deletions src/app/blog/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ interface PostInformation {
export interface PostPath {
year: string;
month: string;
date: string;
day: string;
slug: string;
}

export interface PostMetadata extends PostInformation, PostPath {
date: Date;
excerpt: string;
content: string;
}
Expand All @@ -38,17 +39,18 @@ export function getPostData(filename: string): PostMetadata {
// Use gray-matter to parse the post metadata section
const matterResult = matter(fileContents, { excerpt: true });

const date = id.slice(0, 10).split("-");
const [year, month, day] = id.slice(0, 10).split("-");
const slug = id.substring(11);
// Combine the data with the id
return {
...(matterResult.data as PostInformation),
excerpt: matterResult.excerpt || "",
content: matterResult.content,
year: date[0],
month: date[1],
date: date[2],
slug: slug,
year,
month,
day,
date: new Date(Number(year), Number(month) - 1, Number(day)),
slug,
};
}

Expand All @@ -58,10 +60,7 @@ export function getSortedPostsData(): PostMetadata[] {
const allPostsData = fileNames.map(getPostData);
// Sort posts by date
return allPostsData.sort((a, b) => {
if (
new Date(Number(a.year), Number(a.month), Number(a.date)) <
new Date(Number(b.year), Number(b.month), Number(b.date))
) {
if (a.date < b.date) {
return 1;
} else {
return -1;
Expand Down
12 changes: 3 additions & 9 deletions src/app/feed.xml/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ export async function GET() {
favicon: "https://ruffle.rs/favicon-180.png",
copyright: `Copyright Ruffle, ${new Date().getFullYear()}`,
updated:
posts.length == 0
? undefined
: new Date(
Number(posts[0].year),
Number(posts[0].month)-1,
Number(posts[0].date),
),
posts.length == 0 ? undefined : posts[0].date,
feedLinks: {
atom: "https://ruffle.rs/feed.xml",
},
});

for (const post of posts) {
feed.addItem({
date: new Date(Number(post.year), Number(post.month)-1, Number(post.date)),
link: `https://ruffle.rs/blog/${post.year}/${post.month}/${post.date}/${post.slug}`,
date: post.date,
link: `https://ruffle.rs/blog/${post.year}/${post.month}/${post.day}/${post.slug}`,
title: post.title,
description: post.excerpt,
author: [{ name: post.author }],
Expand Down

0 comments on commit 8504f40

Please sign in to comment.