From 1717222a8bd48ba5ba0daacb155e684396d7aa1d Mon Sep 17 00:00:00 2001 From: satnaing Date: Sat, 28 Jan 2023 01:15:36 +0630 Subject: [PATCH] refactor: update querying logic with content collections api --- src/pages/[ogTitle].svg.ts | 18 ++++++------------ src/pages/rss.xml.ts | 33 ++++++++++++--------------------- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/pages/[ogTitle].svg.ts b/src/pages/[ogTitle].svg.ts index 2fa08e2..bdf1206 100644 --- a/src/pages/[ogTitle].svg.ts +++ b/src/pages/[ogTitle].svg.ts @@ -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>( - "../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 }, })); } diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index d98a8e9..e9d77fb 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -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>( - "../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), + })), }); +}