fix: correctly correspond to the og of each article

This commit is contained in:
radishzzz 2025-03-13 01:21:19 +00:00
parent 66d055f512
commit 9b9bdb2811
6 changed files with 39 additions and 32 deletions

View file

@ -31,7 +31,8 @@ export const themeConfig: ThemeConfig = {
// background color // background color
background: 'oklch(96% 0.005 298)', background: 'oklch(96% 0.005 298)',
// code block theme // code block theme
codeTheme: 'github-light', // available themes: https://shiki.style/themes // available themes: https://shiki.style/themes
codeTheme: 'github-light',
}, },
dark: { dark: {
// title color // title color
@ -41,7 +42,8 @@ export const themeConfig: ThemeConfig = {
// background color // background color
background: 'oklch(22% 0.005 298)', background: 'oklch(22% 0.005 298)',
// code block theme // code block theme
codeTheme: 'github-dark', // available themes: https://shiki.style/themes // available themes: https://shiki.style/themes
codeTheme: 'github-dark',
}, },
}, },
// COLOR SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END // COLOR SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
@ -52,7 +54,7 @@ export const themeConfig: ThemeConfig = {
locale: 'zh', // zh, zh-tw, ja, en, es, ru locale: 'zh', // zh, zh-tw, ja, en, es, ru
// more languages // more languages
// not fill in the locale above again // not fill in the locale above again
moreLocale: ['zh-tw', 'ja', 'en', 'es', 'ru'], // zh, zh-tw, ja, en, es, ru moreLocale: ['zh-tw', 'ja', 'en', 'es', 'ru'], // ['zh', 'zh-tw', 'ja', 'en', 'es', 'ru']
// font styles for text // font styles for text
fontStyle: 'sans', // sans, serif fontStyle: 'sans', // sans, serif
// date format for posts // date format for posts
@ -76,7 +78,8 @@ export const themeConfig: ThemeConfig = {
// gif search // gif search
search: false, // true, false search: false, // true, false
// image uploader // image uploader
imageUploader: false, // true, false (BUGunable to hide image uploader icon) // bug: unable to hide image uploader icon
imageUploader: false, // true, false
}, },
// disqus: TODO // disqus: TODO
// giscus: TODO // giscus: TODO
@ -91,26 +94,37 @@ export const themeConfig: ThemeConfig = {
// site verification // site verification
verification: { verification: {
// Google Search Console // Google Search Console
google: '', // https://search.google.com/search-console // docs: https://search.google.com/search-console
google: '',
// Bing Webmaster Tools // Bing Webmaster Tools
bing: '', // https://www.bing.com/webmasters // docs: https://www.bing.com/webmasters
bing: '',
// Yandex Webmaster // Yandex Webmaster
yandex: '', // https://webmaster.yandex.com // docs: https://webmaster.yandex.com
yandex: '',
// Baidu Search // Baidu Search
baidu: '', // https://ziyuan.baidu.com // docs: https://ziyuan.baidu.com
baidu: '',
}, },
// Google Analytics // Google Analytics
googleAnalyticsID: '', // https://analytics.google.com // docs: https://analytics.google.com
googleAnalyticsID: '',
// Umami Analytics // Umami Analytics
umamiAnalyticsID: '520af332-bfb7-4e7c-9386-5f273ee3d697', // https://cloud.umami.is // docs: https://cloud.umami.is
umamiAnalyticsID: '520af332-bfb7-4e7c-9386-5f273ee3d697',
// follow verification // follow verification
// https://follow.is/ // docs: https://follow.is/
follow: { follow: {
// feed ID // feed ID
feedID: '', feedID: '',
// user ID // user ID
userID: '', userID: '',
}, },
// Open Graph
openGraph: {
// image url (1200x630)
url: '', // https://placehold.co/1200x630
},
}, },
// SEO SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END // SEO SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
@ -120,7 +134,7 @@ export const themeConfig: ThemeConfig = {
links: [ links: [
{ {
name: 'RSS', name: 'RSS',
url: '/rss.xml', // rss.xml or atom.xml url: '/rss.xml', // rss.xml, atom.xml
}, },
{ {
name: 'GitHub', name: 'GitHub',

View file

@ -1,39 +1,27 @@
--- ---
import themeConfig from '@/config' import themeConfig from '@/config'
import { isPostPage } from '@/utils/path'
import { ClientRouter } from 'astro:transitions' import { ClientRouter } from 'astro:transitions'
interface Props { interface Props {
postTitle?: string postTitle?: string
postDescription?: string postDescription?: string
postSlug?: string
} }
const { postTitle, postDescription } = Astro.props const { postTitle, postDescription, postSlug } = Astro.props
const { title, subtitle, description, author, url, favicon } = themeConfig.site const { title, subtitle, description, author, url, favicon } = themeConfig.site
const { mode, light: { background: lightMode }, dark: { background: darkMode } } = themeConfig.color const { mode, light: { background: lightMode }, dark: { background: darkMode } } = themeConfig.color
const { locale, moreLocale } = themeConfig.global const { locale, moreLocale } = themeConfig.global
const { verification = {}, twitterID = '', googleAnalyticsID = '', umamiAnalyticsID = '' } = themeConfig.seo ?? {} // TODO: Change openGraph image fallback url
const { verification = {}, twitterID = '', googleAnalyticsID = '', umamiAnalyticsID = '', openGraph = 'https://placehold.co/1200x630' } = themeConfig.seo ?? {}
const { google = '', bing = '', yandex = '', baidu = '' } = verification const { google = '', bing = '', yandex = '', baidu = '' } = verification
const { commentURL = '', imageHostURL = '', customGoogleAnalyticsJS = '', customUmamiAnalyticsJS = '' } = themeConfig.preload const { commentURL = '', imageHostURL = '', customGoogleAnalyticsJS = '', customUmamiAnalyticsJS = '' } = themeConfig.preload
const initMetaTheme = mode === 'dark' ? darkMode : lightMode const initMetaTheme = mode === 'dark' ? darkMode : lightMode
const pageTitle = postTitle ? `${postTitle} | ${title}` : `${title} - ${subtitle}` const pageTitle = postTitle ? `${postTitle} | ${title}` : `${title} - ${subtitle}`
const pageDescription = postDescription || description const pageDescription = postDescription || description
const pageImage = postSlug ? `${url}/og/${postSlug}.png` : `${openGraph}`
// Determine the OG image for the page
// Check if the current page is a post
const currentPath = Astro.url.pathname
const isPost = isPostPage(currentPath)
// Extract slug from URL as the post identifier
const pathParts = currentPath.split('/').filter(part => part !== '')
const slug = pathParts.length > 0 ? pathParts[pathParts.length - 1] : ''
// Prioritize auto-generated OG image, otherwise use fallback OG image
const pageImage = isPost && slug
? `${url}/og/${slug}.png`
: (favicon.startsWith('http') ? favicon : `${url}${favicon}`)
--- ---
<head> <head>
<!-- Basic info --> <!-- Basic info -->

View file

@ -18,9 +18,10 @@ import '@/styles/heti.css'
interface Props { interface Props {
postTitle?: string postTitle?: string
postDescription?: string postDescription?: string
postSlug?: string
} }
const { postTitle, postDescription } = Astro.props const { postTitle, postDescription, postSlug } = Astro.props
const { isHome, isPost } = getPagePath(Astro.url.pathname) const { isHome, isPost } = getPagePath(Astro.url.pathname)
const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'font-sans' const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'font-sans'
@ -34,7 +35,7 @@ const footerMarginClass = isPost && themeConfig.comment?.waline?.serverURL
class={fontStyle} class={fontStyle}
data-overlayscrollbars-initialize data-overlayscrollbars-initialize
> >
<Head {postTitle} {postDescription} /> <Head {postTitle} {postDescription} {postSlug} />
<body data-overlayscrollbars-initialize> <body data-overlayscrollbars-initialize>
<!-- --> <!-- -->
<div <div

View file

@ -7,7 +7,7 @@ const blogEntries = await getCollection('posts')
// Convert to page data objects // Convert to page data objects
const pages = Object.fromEntries( const pages = Object.fromEntries(
blogEntries.map(({ slug, data }) => [ blogEntries.map(({ slug, data }) => [
data.abbrlink || slug, // Prioritize using abbrlink instead of slug slug,
{ {
title: data.title, title: data.title,
description: data.description || '', description: data.description || '',

View file

@ -26,6 +26,7 @@ const { Content, remarkPluginFrontmatter } = await post.render()
<Layout <Layout
postTitle={post.data.title} postTitle={post.data.title}
postDescription={description} postDescription={description}
postSlug={post.slug}
> >
<article class="heti mb-12.6"> <article class="heti mb-12.6">
<h1 class="post-title"> <h1 class="post-title">

View file

@ -60,6 +60,9 @@ export interface ThemeConfig {
feedID?: string feedID?: string
userID?: string userID?: string
} }
openGraph?: {
url?: string
}
} }
footer: { footer: {