refactor: update querying logic with content collections api

This commit is contained in:
satnaing
2023-01-28 01:15:36 +06:30
parent db385b6567
commit 1717222a8b
2 changed files with 18 additions and 33 deletions
+6 -12
View File
@@ -1,24 +1,18 @@
import type { APIRoute, MarkdownInstance } from "astro";
import { getCollection } from "astro:content";
import generateOgImage from "@utils/generateOgImage";
import type { Frontmatter } from "@types";
import type { APIRoute } from "astro";
export const get: APIRoute = async ({ params }) => ({
body: await generateOgImage(params.ogTitle),
});
const postImportResult = import.meta.glob<MarkdownInstance<Frontmatter>>(
"../contents/**/**/*.md",
{
eager: true,
}
);
const postImportResult = await getCollection("blog", ({ data }) => !data.draft);
const posts = Object.values(postImportResult);
export function getStaticPaths() {
return posts
.filter(({ frontmatter }) => !frontmatter.draft)
.filter(({ frontmatter }) => !frontmatter.ogImage)
.map(({ frontmatter }) => ({
params: { ogTitle: frontmatter.title },
.filter(({ data }) => !data.ogImage?.src)
.map(({ data }) => ({
params: { ogTitle: data.title },
}));
}
+11 -20
View File
@@ -1,28 +1,19 @@
import { SITE } from "src/config";
import rss from "@astrojs/rss";
import type { Frontmatter } from "src/types";
import type { MarkdownInstance } from "astro";
import { getCollection } from "astro:content";
import { SITE } from "@config";
import slugify from "@utils/slugify";
const postImportResult = import.meta.glob<MarkdownInstance<Frontmatter>>(
"../contents/**/**/*.md",
{
eager: true,
}
);
const posts = Object.values(postImportResult);
export const get = () =>
rss({
export async function get() {
const posts = await getCollection("blog", ({ data }) => !data.draft);
return rss({
title: SITE.title,
description: SITE.desc,
site: SITE.website,
items: posts
.filter(({ frontmatter }) => !frontmatter.draft)
.map(({ frontmatter }) => ({
link: `posts/${slugify(frontmatter)}`,
title: frontmatter.title,
description: frontmatter.description,
pubDate: new Date(frontmatter.datetime),
items: posts.map(({ data }) => ({
link: `posts/${slugify(data)}`,
title: data.title,
description: data.description,
pubDate: new Date(data.pubDatetime),
})),
});
}