yufan.me/src/pages/og/[slug].png.ts
Yufan Sheng 901cd8219c
feat: add upyun support. move images to upyun on build. ()
* style: better reply button.

* feat: add astro badge.

* chore: drop useless styles.

* feat: use new options.ts for global configuration.

* fix: the invalid import.meta.env.PROD in astro build.

* feat: move og to new assert prefix.

* chore: add better og description.

* chore: make sure all the images link could be correctly replaced.

* feat: add upyun uploader.
2024-06-19 03:34:36 +08:00

54 lines
1.2 KiB
TypeScript

import { defaultOpenGraph, drawOpenGraph } from '@/helpers/og';
import { getPage, getPost, pages, posts } from '@/helpers/schema';
import type { APIRoute } from 'astro';
const fallback = async () =>
new Response(await defaultOpenGraph(), {
headers: { 'Content-Type': 'image/png' },
});
export const prerender = true;
export const GET: APIRoute = async ({ params }) => {
const slug = params.slug;
if (!slug) {
return await fallback();
}
let title: string;
let summary: string;
let cover: string;
// Query the post
const post = getPost(slug);
if (!post) {
// Fallback to query from pages
const page = getPage(slug);
if (!page) {
return await fallback();
}
title = page.title;
summary = '';
cover = page.cover.src;
} else {
title = post.title;
summary = post.summary;
cover = post.cover.src;
}
// Fetch the cover image as the background
const buffer = await drawOpenGraph({ title, summary, cover });
return new Response(buffer, {
headers: { 'Content-Type': 'image/png' },
});
};
export const getStaticPaths = async () => {
return [
...posts.map((post) => ({ params: { slug: post.slug } })),
...pages.map((page) => ({ params: { slug: page.slug } })),
];
};