diff --git a/website/src/components/Head.astro b/website/src/components/Head.astro index 73dc360d..9c4eeede 100644 --- a/website/src/components/Head.astro +++ b/website/src/components/Head.astro @@ -1,9 +1,49 @@ --- import Default from '@astrojs/starlight/components/Head.astro'; +const { head, entry } = Astro.locals.starlightRoute; const basePath = import.meta.env.BASE_URL; + +const getMetaContent = (key: string, attribute: 'name' | 'property' = 'name') => + head.find((tag) => tag.tag === 'meta' && tag.attrs?.[attribute] === key)?.attrs?.content; + +const getLinkHref = (rel: string) => + head.find((tag) => tag.tag === 'link' && tag.attrs?.rel === rel)?.attrs?.href; + +const twitterTitle = entry.data.title; +const description = + entry.data.description ?? + getMetaContent('description') ?? + getMetaContent('og:description', 'property'); +const canonicalUrl = + getLinkHref('canonical') ?? getMetaContent('og:url', 'property'); +const socialImageUrl = + getMetaContent('twitter:image') ?? getMetaContent('og:image', 'property'); +const socialImageAlt = + getMetaContent('twitter:image:alt') ?? + getMetaContent('og:image:alt', 'property') ?? + description; +const socialImageType = socialImageUrl?.endsWith('.png') + ? 'image/png' + : socialImageUrl?.endsWith('.jpg') || socialImageUrl?.endsWith('.jpeg') + ? 'image/jpeg' + : socialImageUrl?.endsWith('.webp') + ? 'image/webp' + : undefined; + +const twitterDomain = + canonicalUrl && URL.canParse(canonicalUrl) + ? new URL(canonicalUrl).hostname + : undefined; --- +{twitterTitle && } +{description && } +{canonicalUrl && } +{twitterDomain && } +{socialImageUrl && } +{socialImageType && } +{socialImageAlt && }