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 generateOgImage from "@utils/generateOgImage";
import type { Frontmatter } from "@types"; import type { APIRoute } from "astro";
export const get: APIRoute = async ({ params }) => ({ export const get: APIRoute = async ({ params }) => ({
body: await generateOgImage(params.ogTitle), body: await generateOgImage(params.ogTitle),
}); });
const postImportResult = import.meta.glob<MarkdownInstance<Frontmatter>>( const postImportResult = await getCollection("blog", ({ data }) => !data.draft);
"../contents/**/**/*.md",
{
eager: true,
}
);
const posts = Object.values(postImportResult); const posts = Object.values(postImportResult);
export function getStaticPaths() { export function getStaticPaths() {
return posts return posts
.filter(({ frontmatter }) => !frontmatter.draft) .filter(({ data }) => !data.ogImage?.src)
.filter(({ frontmatter }) => !frontmatter.ogImage) .map(({ data }) => ({
.map(({ frontmatter }) => ({ params: { ogTitle: data.title },
params: { ogTitle: frontmatter.title },
})); }));
} }
+11 -20
View File
@@ -1,28 +1,19 @@
import { SITE } from "src/config";
import rss from "@astrojs/rss"; import rss from "@astrojs/rss";
import type { Frontmatter } from "src/types"; import { getCollection } from "astro:content";
import type { MarkdownInstance } from "astro"; import { SITE } from "@config";
import slugify from "@utils/slugify"; import slugify from "@utils/slugify";
const postImportResult = import.meta.glob<MarkdownInstance<Frontmatter>>( export async function get() {
"../contents/**/**/*.md", const posts = await getCollection("blog", ({ data }) => !data.draft);
{ return rss({
eager: true,
}
);
const posts = Object.values(postImportResult);
export const get = () =>
rss({
title: SITE.title, title: SITE.title,
description: SITE.desc, description: SITE.desc,
site: SITE.website, site: SITE.website,
items: posts items: posts.map(({ data }) => ({
.filter(({ frontmatter }) => !frontmatter.draft) link: `posts/${slugify(data)}`,
.map(({ frontmatter }) => ({ title: data.title,
link: `posts/${slugify(frontmatter)}`, description: data.description,
title: frontmatter.title, pubDate: new Date(data.pubDatetime),
description: frontmatter.description,
pubDate: new Date(frontmatter.datetime),
})), })),
}); });
}