feat: set `og:type` to `article` for posts (#183)

* New prop setOGTypeArticle sets og:type to "article" for posts, and "website" for every other page.

* Fixed error where I declared setOGTypeArticle twice.
This commit is contained in:
David V. Kimball 2024-09-16 22:35:58 -07:00 committed by GitHub
parent 44fb628737
commit 7a022cc190
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 4 deletions

View File

@ -22,9 +22,10 @@ interface Props {
banner?: string banner?: string
description?: string description?: string
lang?: string lang?: string
setOGTypeArticle?: boolean
} }
let { title, banner, description, lang } = Astro.props let { title, banner, description, lang, setOGTypeArticle } = Astro.props
// apply a class to the body element to decide the height of the banner, only used for initial page load // apply a class to the body element to decide the height of the banner, only used for initial page load
// Swup can update the body for each page visit, but it's after the page transition, causing a delay for banner height change // Swup can update the body for each page visit, but it's after the page transition, causing a delay for banner height change
@ -82,6 +83,11 @@ const bannerOffset = bannerOffsetByPosition[siteConfig.banner.position || 'cente
<meta property="og:url" content={Astro.url}> <meta property="og:url" content={Astro.url}>
<meta property="og:title" content={pageTitle}> <meta property="og:title" content={pageTitle}>
<meta property="og:description" content={description || pageTitle}> <meta property="og:description" content={description || pageTitle}>
{setOGTypeArticle ? (
<meta property="og:type" content="article" />
) : (
<meta property="og:type" content="website" />
)}
<meta name="twitter:card" content="summary_large_image"> <meta name="twitter:card" content="summary_large_image">
<meta property="twitter:url" content={Astro.url}> <meta property="twitter:url" content={Astro.url}>

View File

@ -12,15 +12,16 @@ interface Props {
banner?: string banner?: string
description?: string description?: string
lang?: string lang?: string
setOGTypeArticle?: boolean;
} }
const { title, banner, description, lang } = Astro.props const { title, banner, description, lang, setOGTypeArticle } = Astro.props
const hasBannerCredit = const hasBannerCredit =
siteConfig.banner.enable && siteConfig.banner.credit.enable siteConfig.banner.enable && siteConfig.banner.credit.enable
const hasBannerLink = !!siteConfig.banner.credit.url const hasBannerLink = !!siteConfig.banner.credit.url
--- ---
<Layout title={title} banner={banner} description={description} lang={lang}> <Layout title={title} banner={banner} description={description} lang={lang} setOGTypeArticle={setOGTypeArticle}>
<slot slot="head" name="head"></slot> <slot slot="head" name="head"></slot>
<div id="top-row" class="z-50 pointer-events-none relative transition-all duration-700 max-w-[var(--page-width)] px-0 md:px-4 mx-auto" class:list={[""]}> <div id="top-row" class="z-50 pointer-events-none relative transition-all duration-700 max-w-[var(--page-width)] px-0 md:px-4 mx-auto" class:list={[""]}>
<div id="navbar-wrapper" class="pointer-events-auto sticky top-0 transition-all"> <div id="navbar-wrapper" class="pointer-events-auto sticky top-0 transition-all">

View File

@ -45,7 +45,7 @@ const jsonLd = {
// TODO include cover image here // TODO include cover image here
} }
--- ---
<MainGridLayout banner={entry.data.image} title={entry.data.title} description={entry.data.description} lang={entry.data.lang}> <MainGridLayout banner={entry.data.image} title={entry.data.title} description={entry.data.description} lang={entry.data.lang} setOGTypeArticle={true}>
<script is:inline slot="head" type="application/ld+json" set:html={JSON.stringify(jsonLd)}></script> <script is:inline slot="head" type="application/ld+json" set:html={JSON.stringify(jsonLd)}></script>
<div class="flex w-full rounded-[var(--radius-large)] overflow-hidden relative mb-4"> <div class="flex w-full rounded-[var(--radius-large)] overflow-hidden relative mb-4">
<div id="post-container" class:list={["card-base z-10 px-6 md:px-9 pt-6 pb-4 relative w-full ", <div id="post-container" class:list={["card-base z-10 px-6 md:px-9 pt-6 pb-4 relative w-full ",