Skip to content

Commit b50b2ee

Browse files
Merge branch 'main' into build/update_latest
# Conflicts: # CHANGELOG.md # docker-compose.yml # src/config.ts # src/data/blog/adding-new-post.md # src/data/blog/dynamic-og-images.md # src/data/blog/how-to-add-latex-equations-in-blog-posts.md # src/data/blog/how-to-configure-astropaper-theme.md # src/data/blog/how-to-integrate-giscus-comments.md # src/data/blog/how-to-update-dependencies.md # src/data/blog/predefined-color-schemes.md # src/data/blog/setting-dates-via-git-hooks.md # src/utils/loadGoogleFont.ts
2 parents 5318632 + ad9c26f commit b50b2ee

8 files changed

Lines changed: 55 additions & 69 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "astro-paper",
33
"type": "module",
4-
"version": "5.0.0",
4+
"version": "5.0.1",
55
"scripts": {
66
"dev": "astro dev",
77
"build": "astro check && astro build && pagefind --site dist && cp -r dist/pagefind public/",

src/components/Header.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const isActive = (path: string) => {
3737
>
3838
<div
3939
id="top-nav-wrap"
40-
class="relative flex w-full items-start justify-between bg-background p-4 sm:py-6"
40+
class="relative flex w-full items-baseline justify-between bg-background p-4 sm:py-6"
4141
>
4242
<a
4343
href="/"

src/config.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,6 @@ export const SITE = {
99
postPerPage: 4,
1010
scheduledPostMargin: 15 * 60 * 1000, // 15 minutes
1111
showArchives: true,
12-
showBackButton: true
12+
showBackButton: true,
13+
dynamicOgImage: true,
1314
};
14-
15-
export const SOCIALS = [
16-
{
17-
name: "Github",
18-
href: "https://github.com/iamstephenfang",
19-
linkTitle: ` ${SITE.author} on Github`,
20-
active: true,
21-
},
22-
{
23-
name: "Instagram",
24-
href: "https://instagram.com/iamstephenfang",
25-
linkTitle: `${SITE.author} on Instagram`,
26-
active: true,
27-
},
28-
{
29-
name: "Mail",
30-
href: "mailto:fangxuanmiao@hotmail.com",
31-
linkTitle: `Send an email to ${SITE.author}`,
32-
active: true,
33-
},
34-
{
35-
name: "X",
36-
href: "https://x.com/fangxuanmiao",
37-
linkTitle: `${SITE.author} on X`,
38-
active: true,
39-
},
40-
{
41-
name: "Telegram",
42-
href: "https://t.me/stephenfang",
43-
linkTitle: `${SITE.author} on Telegram`,
44-
active: true,
45-
},
46-
{
47-
name: "Jike",
48-
href: "https://okjk.co/LZFasc",
49-
linkTitle: `${SITE.author} on Jike`,
50-
active: true,
51-
},
52-
];

src/content.config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ const blog = defineCollection({
1313
featured: z.boolean().optional(),
1414
draft: z.boolean().optional(),
1515
tags: z.array(z.string()).default(["others"]),
16-
ogImage: image()
17-
.refine(img => img.width >= 1200 && img.height >= 630, {
18-
message: "OpenGraph image must be at least 1200 X 630 pixels!",
19-
})
20-
.or(z.string())
21-
.optional(),
16+
ogImage: image().or(z.string()).optional(),
2217
description: z.string(),
2318
canonicalURL: z.string().optional(),
2419
editPost: z

src/layouts/Layout.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const {
2323
author = SITE.author,
2424
profile = SITE.profile,
2525
description = SITE.desc,
26-
ogImage = SITE.ogImage,
26+
ogImage = SITE.ogImage ? `/${SITE.ogImage}` : "/og.png",
2727
canonicalURL = new URL(Astro.url.pathname, Astro.url),
2828
pubDatetime,
2929
modDatetime,
3030
scrollSmooth = false,
3131
} = Astro.props;
3232
33-
const socialImageURL = new URL(ogImage ?? SITE.ogImage ?? "og.png", Astro.url);
33+
const socialImageURL = new URL(ogImage, Astro.url);
3434
3535
const structuredData = {
3636
"@context": "https://schema.org",

src/layouts/PostDetails.astro

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const {
2424
title,
2525
author,
2626
description,
27-
ogImage,
27+
ogImage: initOgImage,
2828
canonicalURL,
2929
pubDatetime,
3030
modDatetime,
@@ -34,11 +34,24 @@ const {
3434
3535
const { Content } = await render(post);
3636
37-
const ogImageUrl = typeof ogImage === "string" ? ogImage : ogImage?.src;
38-
const ogUrl = new URL(
39-
ogImageUrl ?? `/posts/${slugifyStr(title)}/index.png`,
40-
Astro.url.origin
41-
).href;
37+
let ogImageUrl: string | undefined;
38+
39+
// Determine OG image source
40+
if (typeof initOgImage === "string") {
41+
ogImageUrl = initOgImage; // Remote OG image (absolute URL)
42+
} else if (initOgImage?.src) {
43+
ogImageUrl = initOgImage.src; // Local asset
44+
}
45+
46+
// Use dynamic OG image if enabled and no remote|local ogImage
47+
if (!ogImageUrl && SITE.dynamicOgImage) {
48+
ogImageUrl = `/posts/${slugifyStr(title)}/index.png`;
49+
}
50+
51+
// Resolve OG image URL (or fallback to SITE.ogImage / default `og.png`)
52+
const ogImage = ogImageUrl
53+
? new URL(ogImageUrl, Astro.url.origin).href
54+
: undefined;
4255
4356
const layoutProps = {
4457
title: `${title} | ${SITE.title}`,
@@ -47,7 +60,7 @@ const layoutProps = {
4760
pubDatetime,
4861
modDatetime,
4962
canonicalURL,
50-
ogImage: ogUrl,
63+
ogImage,
5164
scrollSmooth: true,
5265
};
5366

src/pages/posts/[slug]/index.png.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import type { APIRoute } from "astro";
22
import { getCollection, type CollectionEntry } from "astro:content";
33
import { generateOgImageForPost } from "@/utils/generateOgImages";
44
import { slugifyStr } from "@/utils/slugify";
5+
import { SITE } from "@/config";
56

67
export async function getStaticPaths() {
8+
if (!SITE.dynamicOgImage) {
9+
return [];
10+
}
11+
712
const posts = await getCollection("blog").then(p =>
813
p.filter(({ data }) => !data.draft && !data.ogImage)
914
);
@@ -14,7 +19,18 @@ export async function getStaticPaths() {
1419
}));
1520
}
1621

17-
export const GET: APIRoute = async ({ props }) =>
18-
new Response(await generateOgImageForPost(props as CollectionEntry<"blog">), {
19-
headers: { "Content-Type": "image/png" },
20-
});
22+
export const GET: APIRoute = async ({ props }) => {
23+
if (!SITE.dynamicOgImage) {
24+
return new Response(null, {
25+
status: 404,
26+
statusText: "Not found",
27+
});
28+
}
29+
30+
return new Response(
31+
await generateOgImageForPost(props as CollectionEntry<"blog">),
32+
{
33+
headers: { "Content-Type": "image/png" },
34+
}
35+
);
36+
};

src/utils/loadGoogleFont.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
async function loadGoogleFont(
22
font: string,
3-
text: string
3+
text: string,
4+
weight: number
45
): Promise<ArrayBuffer> {
5-
const API = `https://fonts.googleapis.com/css2?family=${font}&text=${encodeURIComponent(text)}`;
6+
const API = `https://fonts.googleapis.com/css2?family=${font}:wght@${weight}&text=${encodeURIComponent(text)}`;
67

78
const css = await (
89
await fetch(API, {
@@ -14,7 +15,7 @@ async function loadGoogleFont(
1415
).text();
1516

1617
const resource = css.match(
17-
/src: url\((.+)\) format\('(opentype|truetype)'\)/
18+
/src: url\((.+?)\) format\('(opentype|truetype)'\)/
1819
);
1920

2021
if (!resource) throw new Error("Failed to download dynamic font");
@@ -25,8 +26,7 @@ async function loadGoogleFont(
2526
throw new Error("Failed to download dynamic font. Status: " + res.status);
2627
}
2728

28-
const fonts: ArrayBuffer = await res.arrayBuffer();
29-
return fonts;
29+
return res.arrayBuffer();
3030
}
3131

3232
async function loadGoogleFonts(
@@ -42,16 +42,16 @@ async function loadGoogleFonts(
4242
style: "normal",
4343
},
4444
{
45-
name: "Source Han Serif",
46-
font: "Noto+Serif+SC:wght@700",
45+
name: "IBM Plex Mono",
46+
font: "IBM+Plex+Mono:wght@700",
4747
weight: 700,
4848
style: "bold",
4949
},
5050
];
5151

5252
const fonts = await Promise.all(
5353
fontsConfig.map(async ({ name, font, weight, style }) => {
54-
const data = await loadGoogleFont(font, text);
54+
const data = await loadGoogleFont(font, text, weight);
5555
return { name, data, weight, style };
5656
})
5757
);

0 commit comments

Comments
 (0)