Head.astro 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ---
  2. import { Base64 } from "js-base64";
  3. import Default from '@astrojs/starlight/components/Head.astro'
  4. import config from '../../config.mjs'
  5. const base = import.meta.env.BASE_URL.replace(/^\//, "").replace(/\/$/, "")
  6. const slug = Astro.url.pathname.replace(/^\//, "").replace(/\/$/, "");
  7. const {
  8. entry: {
  9. data: { title , description },
  10. },
  11. } = Astro.locals.starlightRoute;
  12. const isDocs = base === "" ? true : slug === base || slug.startsWith(`${base}/`)
  13. const t = Astro.locals.t as (key: string) => string
  14. const titleSuffix = t("app.head.titleSuffix")
  15. const shareSlug = base === "" ? "s" : `${base}/s`
  16. const isShare = slug === shareSlug || slug.startsWith(`${shareSlug}/`)
  17. const isHome = slug === "" || slug === base
  18. let encodedTitle = '';
  19. let ogImage = `${config.url}/social-share.png`;
  20. let truncatedDesc = '';
  21. if (isDocs) {
  22. // Truncate to fit S3's max key size
  23. encodedTitle = encodeURIComponent(
  24. Base64.encode(
  25. // Convert to ASCII
  26. encodeURIComponent(
  27. // Truncate to fit S3's max key size
  28. title.substring(0, 700)
  29. )
  30. )
  31. );
  32. if (description) {
  33. truncatedDesc = encodeURIComponent(description.substring(0, 400))
  34. }
  35. ogImage = `${config.socialCard}/opencode-docs/${encodedTitle}.png?desc=${truncatedDesc}`;
  36. }
  37. ---
  38. { isHome && (
  39. <title>{title} | {titleSuffix}</title>
  40. )}
  41. <Default {...Astro.props}><slot /></Default>
  42. { !isShare && (
  43. <meta property="og:image" content={ogImage} />
  44. <meta property="twitter:image" content={ogImage} />
  45. )}