mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
refactor: update querying logic with content collections api
This commit is contained in:
@@ -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
@@ -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),
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user