refactor: update for new content collection api

This commit is contained in:
satnaing
2023-01-28 01:23:37 +06:30
parent 38761e2e8f
commit e8171d327e
4 changed files with 28 additions and 36 deletions
+10 -12
View File
@@ -1,22 +1,20 @@
--- ---
import { getCollection } from "astro:content";
import Layout from "@layouts/Layout.astro"; import Layout from "@layouts/Layout.astro";
import Header from "@components/Header.astro"; import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro"; import Footer from "@components/Footer.astro";
import LinkButton from "@components/LinkButton.astro"; import LinkButton from "@components/LinkButton.astro";
import Hr from "@components/Hr.astro"; import Hr from "@components/Hr.astro";
import Card from "@components/Card"; import Card from "@components/Card";
import Socials from "@components/Socials.astro";
import getSortedPosts from "@utils/getSortedPosts"; import getSortedPosts from "@utils/getSortedPosts";
import slugify from "@utils/slugify"; import slugify from "@utils/slugify";
import type { Frontmatter } from "src/types";
import Socials from "@components/Socials.astro";
import { SOCIALS } from "@config"; import { SOCIALS } from "@config";
const posts = await Astro.glob<Frontmatter>("../contents/**/*.md"); const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts); const sortedPosts = getSortedPosts(posts);
const featuredPosts = sortedPosts.filter( const featuredPosts = sortedPosts.filter(({ data }) => data.featured);
({ frontmatter }) => frontmatter.featured
);
const socialCount = SOCIALS.filter(social => social.active).length; const socialCount = SOCIALS.filter(social => social.active).length;
--- ---
@@ -76,10 +74,10 @@ const socialCount = SOCIALS.filter(social => social.active).length;
<section id="featured"> <section id="featured">
<h2>Featured</h2> <h2>Featured</h2>
<ul> <ul>
{featuredPosts.map(({ frontmatter }) => ( {featuredPosts.map(({ data }) => (
<Card <Card
href={`/posts/${slugify(frontmatter)}`} href={`/posts/${slugify(data)}`}
post={frontmatter} frontmatter={data}
secHeading={false} secHeading={false}
/> />
))} ))}
@@ -95,11 +93,11 @@ const socialCount = SOCIALS.filter(social => social.active).length;
<ul> <ul>
{ {
sortedPosts.map( sortedPosts.map(
({ frontmatter }, index) => ({ data }, index) =>
index < 4 && ( index < 4 && (
<Card <Card
href={`/posts/${slugify(frontmatter)}`} href={`/posts/${slugify(data)}`}
post={frontmatter} frontmatter={data}
secHeading={false} secHeading={false}
/> />
) )
+8 -9
View File
@@ -1,15 +1,14 @@
--- ---
import { SITE } from "src/config"; import { CollectionEntry, getCollection } from "astro:content";
import Posts from "@layouts/Posts.astro"; import Posts from "@layouts/Posts.astro";
import PostDetails from "@layouts/PostDetails.astro"; import PostDetails from "@layouts/PostDetails.astro";
import getSortedPosts from "@utils/getSortedPosts"; import getSortedPosts from "@utils/getSortedPosts";
import getPageNumbers from "@utils/getPageNumbers"; import getPageNumbers from "@utils/getPageNumbers";
import type { MarkdownInstance } from "astro";
import type { Frontmatter } from "src/types";
import slugify from "@utils/slugify"; import slugify from "@utils/slugify";
import { SITE } from "@config";
export interface Props { export interface Props {
post: MarkdownInstance<Frontmatter>; post: CollectionEntry<"blog">;
} }
type PostResult = { type PostResult = {
@@ -17,7 +16,7 @@ type PostResult = {
slug: string | number; slug: string | number;
}; };
props?: { props?: {
post: MarkdownInstance<Record<string, any>>; post: CollectionEntry<"blog">;
}; };
}[]; }[];
@@ -28,14 +27,14 @@ type PagePaths = {
}[]; }[];
export async function getStaticPaths() { 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 => { let postResult: PostResult = filteredPosts.map(post => {
return { return {
params: { params: {
slug: slugify(post.frontmatter), slug: slugify(post.data),
}, },
props: { props: {
post, post,
@@ -55,7 +54,7 @@ export async function getStaticPaths() {
const { slug } = Astro.params; const { slug } = Astro.params;
const { post } = Astro.props; const { post } = Astro.props;
const posts = await Astro.glob<Frontmatter>("../../contents/**/*.md"); const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts); const sortedPosts = getSortedPosts(posts);
+7 -12
View File
@@ -1,5 +1,5 @@
--- ---
import { SITE } from "src/config"; import { CollectionEntry, getCollection } from "astro:content";
import Layout from "@layouts/Layout.astro"; import Layout from "@layouts/Layout.astro";
import Main from "@layouts/Main.astro"; import Main from "@layouts/Main.astro";
import Header from "@components/Header.astro"; import Header from "@components/Header.astro";
@@ -7,19 +7,16 @@ import Footer from "@components/Footer.astro";
import Card from "@components/Card"; import Card from "@components/Card";
import getUniqueTags from "@utils/getUniqueTags"; import getUniqueTags from "@utils/getUniqueTags";
import getPostsByTag from "@utils/getPostsByTag"; import getPostsByTag from "@utils/getPostsByTag";
import type { MarkdownInstance } from "astro";
import type { Frontmatter } from "src/types";
import slugify from "@utils/slugify"; import slugify from "@utils/slugify";
import { SITE } from "@config";
export interface Props { export interface Props {
post: MarkdownInstance<Frontmatter>; post: CollectionEntry<"blog">;
tag: string; tag: string;
} }
export async function getStaticPaths() { export async function getStaticPaths() {
const posts: MarkdownInstance<Frontmatter>[] = await Astro.glob( const posts: CollectionEntry<"blog">[] = await getCollection("blog");
"../../contents/**/*.md"
);
const tags = getUniqueTags(posts); const tags = getUniqueTags(posts);
@@ -37,9 +34,7 @@ export async function getStaticPaths() {
const { tag } = Astro.props; const { tag } = Astro.props;
const posts: MarkdownInstance<Frontmatter>[] = await Astro.glob( const posts: CollectionEntry<"blog">[] = await getCollection("blog");
"../../contents/**/*.md"
);
const tagPosts = getPostsByTag(posts, tag); const tagPosts = getPostsByTag(posts, tag);
--- ---
@@ -52,8 +47,8 @@ const tagPosts = getPostsByTag(posts, tag);
> >
<ul> <ul>
{ {
tagPosts.map(({ frontmatter }) => ( tagPosts.map(({ data }) => (
<Card href={`/posts/${slugify(frontmatter)}`} post={frontmatter} /> <Card href={`/posts/${slugify(data)}`} frontmatter={data} />
)) ))
} }
</ul> </ul>
+3 -3
View File
@@ -1,14 +1,14 @@
--- ---
import { SITE } from "src/config"; import { getCollection } from "astro:content";
import Header from "@components/Header.astro"; import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro"; import Footer from "@components/Footer.astro";
import Layout from "@layouts/Layout.astro"; import Layout from "@layouts/Layout.astro";
import Main from "@layouts/Main.astro"; import Main from "@layouts/Main.astro";
import Tag from "@components/Tag.astro"; import Tag from "@components/Tag.astro";
import getUniqueTags from "@utils/getUniqueTags"; 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); let tags = getUniqueTags(posts);
--- ---