mirror of
https://github.com/github/awesome-copilot.git
synced 2026-03-18 23:25:13 +00:00
Supplement Starlight's default head output with Twitter title, description, URL, and domain tags plus extra image metadata so social crawlers have a more complete card payload. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
65 lines
2.3 KiB
Plaintext
65 lines
2.3 KiB
Plaintext
---
|
|
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;
|
|
---
|
|
|
|
<Default><slot /></Default>
|
|
{twitterTitle && <meta name="twitter:title" content={twitterTitle} />}
|
|
{description && <meta name="twitter:description" content={description} />}
|
|
{canonicalUrl && <meta property="twitter:url" content={canonicalUrl} />}
|
|
{twitterDomain && <meta property="twitter:domain" content={twitterDomain} />}
|
|
{socialImageUrl && <meta property="og:image:secure_url" content={socialImageUrl} />}
|
|
{socialImageType && <meta property="og:image:type" content={socialImageType} />}
|
|
{socialImageAlt && <meta name="twitter:image:alt" content={socialImageAlt} />}
|
|
<script is:inline define:vars={{ basePath }}>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
document.body.dataset.basePath = basePath;
|
|
});
|
|
</script>
|
|
{import.meta.env.PROD && (
|
|
<>
|
|
<meta
|
|
name="ha-url"
|
|
content="https://collector.githubapp.com/awesome-copilot-web/collect"
|
|
/>
|
|
<script
|
|
async
|
|
defer
|
|
src="https://analytics.githubassets.com/hydro-marketing.min.js"
|
|
></script>
|
|
</>
|
|
)}
|