Skip to content

Commit

Permalink
refactor: update for new content collection api
Browse files Browse the repository at this point in the history
  • Loading branch information
satnaing committed Jan 27, 2023
1 parent 38761e2 commit e8171d3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
22 changes: 10 additions & 12 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
---
import { getCollection } from "astro:content";
import Layout from "@layouts/Layout.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import LinkButton from "@components/LinkButton.astro";
import Hr from "@components/Hr.astro";
import Card from "@components/Card";
import Socials from "@components/Socials.astro";
import getSortedPosts from "@utils/getSortedPosts";
import slugify from "@utils/slugify";
import type { Frontmatter } from "src/types";
import Socials from "@components/Socials.astro";
import { SOCIALS } from "@config";
const posts = await Astro.glob<Frontmatter>("../contents/**/*.md");
const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts);
const featuredPosts = sortedPosts.filter(
({ frontmatter }) => frontmatter.featured
);
const featuredPosts = sortedPosts.filter(({ data }) => data.featured);
const socialCount = SOCIALS.filter(social => social.active).length;
---
Expand Down Expand Up @@ -76,10 +74,10 @@ const socialCount = SOCIALS.filter(social => social.active).length;
<section id="featured">
<h2>Featured</h2>
<ul>
{featuredPosts.map(({ frontmatter }) => (
{featuredPosts.map(({ data }) => (
<Card
href={`/posts/${slugify(frontmatter)}`}
post={frontmatter}
href={`/posts/${slugify(data)}`}
frontmatter={data}
secHeading={false}
/>
))}
Expand All @@ -95,11 +93,11 @@ const socialCount = SOCIALS.filter(social => social.active).length;
<ul>
{
sortedPosts.map(
({ frontmatter }, index) =>
({ data }, index) =>
index < 4 && (
<Card
href={`/posts/${slugify(frontmatter)}`}
post={frontmatter}
href={`/posts/${slugify(data)}`}
frontmatter={data}
secHeading={false}
/>
)
Expand Down
17 changes: 8 additions & 9 deletions src/pages/posts/[slug].astro
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
---
import { SITE } from "src/config";
import { CollectionEntry, getCollection } from "astro:content";
import Posts from "@layouts/Posts.astro";
import PostDetails from "@layouts/PostDetails.astro";
import getSortedPosts from "@utils/getSortedPosts";
import getPageNumbers from "@utils/getPageNumbers";
import type { MarkdownInstance } from "astro";
import type { Frontmatter } from "src/types";
import slugify from "@utils/slugify";
import { SITE } from "@config";
export interface Props {
post: MarkdownInstance<Frontmatter>;
post: CollectionEntry<"blog">;
}
type PostResult = {
params: {
slug: string | number;
};
props?: {
post: MarkdownInstance<Record<string, any>>;
post: CollectionEntry<"blog">;
};
}[];
Expand All @@ -28,14 +27,14 @@ type PagePaths = {
}[];
export async function getStaticPaths() {
const posts = await Astro.glob<Frontmatter>("../../contents/**/*.md");
const posts = await getCollection("blog");
const filteredPosts = posts.filter(({ frontmatter }) => !frontmatter.draft);
const filteredPosts = posts.filter(({ data }) => !data.draft);
let postResult: PostResult = filteredPosts.map(post => {
return {
params: {
slug: slugify(post.frontmatter),
slug: slugify(post.data),
},
props: {
post,
Expand All @@ -55,7 +54,7 @@ export async function getStaticPaths() {
const { slug } = Astro.params;
const { post } = Astro.props;
const posts = await Astro.glob<Frontmatter>("../../contents/**/*.md");
const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts);
Expand Down
19 changes: 7 additions & 12 deletions src/pages/tags/[tag].astro
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
---
import { SITE } from "src/config";
import { CollectionEntry, getCollection } from "astro:content";
import Layout from "@layouts/Layout.astro";
import Main from "@layouts/Main.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import Card from "@components/Card";
import getUniqueTags from "@utils/getUniqueTags";
import getPostsByTag from "@utils/getPostsByTag";
import type { MarkdownInstance } from "astro";
import type { Frontmatter } from "src/types";
import slugify from "@utils/slugify";
import { SITE } from "@config";
export interface Props {
post: MarkdownInstance<Frontmatter>;
post: CollectionEntry<"blog">;
tag: string;
}
export async function getStaticPaths() {
const posts: MarkdownInstance<Frontmatter>[] = await Astro.glob(
"../../contents/**/*.md"
);
const posts: CollectionEntry<"blog">[] = await getCollection("blog");
const tags = getUniqueTags(posts);
Expand All @@ -37,9 +34,7 @@ export async function getStaticPaths() {
const { tag } = Astro.props;
const posts: MarkdownInstance<Frontmatter>[] = await Astro.glob(
"../../contents/**/*.md"
);
const posts: CollectionEntry<"blog">[] = await getCollection("blog");
const tagPosts = getPostsByTag(posts, tag);
---
Expand All @@ -52,8 +47,8 @@ const tagPosts = getPostsByTag(posts, tag);
>
<ul>
{
tagPosts.map(({ frontmatter }) => (
<Card href={`/posts/${slugify(frontmatter)}`} post={frontmatter} />
tagPosts.map(({ data }) => (
<Card href={`/posts/${slugify(data)}`} frontmatter={data} />
))
}
</ul>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/tags/index.astro
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
import { SITE } from "src/config";
import { getCollection } from "astro:content";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import Layout from "@layouts/Layout.astro";
import Main from "@layouts/Main.astro";
import Tag from "@components/Tag.astro";
import getUniqueTags from "@utils/getUniqueTags";
import type { Frontmatter } from "src/types";
import { SITE } from "@config";
const posts = await Astro.glob<Frontmatter>("../../contents/**/*.md");
const posts = await getCollection("blog");
let tags = getUniqueTags(posts);
---
Expand Down

0 comments on commit e8171d3

Please sign in to comment.