Skip to content

Commit

Permalink
feat: sort posts by desc
Browse files Browse the repository at this point in the history
  • Loading branch information
taga3s committed Sep 14, 2024
1 parent 2a9b22f commit ce125ea
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/api/posts/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Post = {
id: string;
title: string;
publishedAt: string;
publishedAt: Date;
};

export type { Post };
7 changes: 6 additions & 1 deletion app/components/posts/Posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ const Posts: FC<Props> = ({ posts }) => {
<Section title="Posts">
<div class={postsWrapper}>
{posts.map((post) => {
const publishedAt = post.publishedAt.toLocaleDateString("ja-JP", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
return (
<a key={post.id} href={`/${post.id}`} class={postsItem}>
<span>{post.publishedAt}</span>
<time datetime={publishedAt}>{publishedAt}</time>
<span class={postsItemTitle}>{post.title}</span>
</a>
);
Expand Down
12 changes: 7 additions & 5 deletions app/routes/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export default createRoute(async (c) => {
const rawPosts = import.meta.glob<{ frontmatter: Meta }>("./posts/*.mdx", {
eager: true,
});
const posts = Object.entries(rawPosts).map(([id, module]) => ({
id: id.replace(/\.mdx$/, ""),
title: module.frontmatter.title ?? "",
publishedAt: module.frontmatter.publishedAt ?? "",
}));
const posts = Object.entries(rawPosts)
.map(([id, module]) => ({
id: id.replace(/\.mdx$/, ""),
title: module.frontmatter.title ?? "",
publishedAt: new Date(module.frontmatter.publishedAt) ?? "",
}))
.sort((a, b) => b.publishedAt.getTime() - a.publishedAt.getTime());

return c.render(<PostsPresenter posts={posts} />);
});
2 changes: 1 addition & 1 deletion app/routes/posts/md_test.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Markdown試し打ち"
publishedAt: "2024-09-XX"
publishedAt: "2024/09/14"
---

# # Section1
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"deploy": "vite build --mode client && vite build && wrangler pages deploy ./dist",
"lint": "biome lint --write --unsafe ./app",
"format": "biome format --write ./app package.json biome.json tsconfig.json",
"article": "speedymd",
"prepare": "husky"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion speedymd.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"required": true
},
{
"name": "published_at",
"name": "publishedAt",
"type": "text",
"question": "Please enter `published_at`",
"placeholder": "2000/01/01",
Expand Down

0 comments on commit ce125ea

Please sign in to comment.