|
| 1 | +import React from "react"; |
| 2 | +import Helmet from "react-helmet"; |
| 3 | +import { graphql, useStaticQuery } from "gatsby"; |
| 4 | +import url from "url"; |
| 5 | + |
| 6 | +const ContactMeta = ({ location }) => { |
| 7 | + const data = useStaticQuery(graphql` |
| 8 | + query { |
| 9 | + site { |
| 10 | + siteMetadata { |
| 11 | + siteUrl |
| 12 | + siteTitle |
| 13 | + metadata { |
| 14 | + title |
| 15 | + description |
| 16 | + } |
| 17 | + twitterCard { |
| 18 | + title |
| 19 | + description |
| 20 | + imageUrl |
| 21 | + username |
| 22 | + } |
| 23 | + facebookCard { |
| 24 | + title |
| 25 | + description |
| 26 | + imageUrl |
| 27 | + appId |
| 28 | + } |
| 29 | + siteDescription |
| 30 | + language |
| 31 | + logoUrl |
| 32 | + iconUrl |
| 33 | + coverUrl |
| 34 | + alternateLogoUrl |
| 35 | + shareImageWidth |
| 36 | + shareImageHeight |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + `); |
| 41 | + |
| 42 | + const config = data.site.siteMetadata; |
| 43 | + |
| 44 | + const baseUrl = data.site.siteMetadata.siteUrl; |
| 45 | + const siteTitle = `Contact | ${data.site.siteMetadata.siteTitle}`; |
| 46 | + |
| 47 | + const canonical = url.resolve(baseUrl, location.pathname); |
| 48 | + |
| 49 | + const description = config.metadata.description || config.siteDescription; |
| 50 | + |
| 51 | + const publisherLogo = url.resolve( |
| 52 | + config.siteUrl, |
| 53 | + config.logoUrl || config.alternateLogoUrl |
| 54 | + ); |
| 55 | + let shareImage = |
| 56 | + config.coverUrl || |
| 57 | + config.facebookCard.imageUrl || |
| 58 | + config.twitterCard.imageUrl; |
| 59 | + |
| 60 | + shareImage = shareImage ? url.resolve(config.siteUrl, shareImage) : null; |
| 61 | + |
| 62 | + const facebookImageUrl = config.facebookCard.imageUrl |
| 63 | + ? url.resolve(config.siteUrl, config.facebookCard.imageUrl) |
| 64 | + : null; |
| 65 | + |
| 66 | + const twitterImageUrl = config.twitterCard.imageUrl |
| 67 | + ? url.resolve(config.siteUrl, config.twitterCard.imageUrl) |
| 68 | + : null; |
| 69 | + |
| 70 | + const jsonLd = { |
| 71 | + "@context": `https://schema.org/`, |
| 72 | + "@type": "WebPage", |
| 73 | + url: canonical, |
| 74 | + headline: 'Contact | ' + config.metadata.title || config.siteTitle, |
| 75 | + image: shareImage |
| 76 | + ? { |
| 77 | + "@type": `ImageObject`, |
| 78 | + url: shareImage, |
| 79 | + width: config.shareImageWidth, |
| 80 | + height: config.shareImageHeight, |
| 81 | + } |
| 82 | + : undefined, |
| 83 | + publisher: { |
| 84 | + "@type": `Organization`, |
| 85 | + name: siteTitle, |
| 86 | + logo: publisherLogo |
| 87 | + ? { |
| 88 | + "@type": `ImageObject`, |
| 89 | + url: publisherLogo, |
| 90 | + width: 60, |
| 91 | + height: 60, |
| 92 | + } |
| 93 | + : undefined, |
| 94 | + }, |
| 95 | + mainEntityOfPage: { |
| 96 | + "@type": `WebPage`, |
| 97 | + "@id": config.siteUrl, |
| 98 | + }, |
| 99 | + description, |
| 100 | + }; |
| 101 | + |
| 102 | + return ( |
| 103 | + <> |
| 104 | + <Helmet htmlAttributes={{ lang: config.language }}> |
| 105 | + <title>Contact | {config.metadata.title || config.siteTitle}</title> |
| 106 | + <meta |
| 107 | + name="description" |
| 108 | + content={config.metadata.description || config.siteDescription} |
| 109 | + /> |
| 110 | + <link rel="canonical" href={canonical} /> |
| 111 | + <meta property="og:site_name" content={config.siteTitle} /> |
| 112 | + <meta property="og:type" content="article" /> |
| 113 | + <meta |
| 114 | + property="og:title" |
| 115 | + content={ `Contact | ` + |
| 116 | + config.facebookCard.title || |
| 117 | + config.metadata.title || |
| 118 | + config.siteTitle |
| 119 | + } |
| 120 | + /> |
| 121 | + <meta |
| 122 | + property="og:description" |
| 123 | + content={ |
| 124 | + config.facebookCard.description || |
| 125 | + config.metadata.description || |
| 126 | + config.siteDescription |
| 127 | + } |
| 128 | + /> |
| 129 | + <meta property="og:url" content={canonical} /> |
| 130 | + {config.facebookCard.imageUrl !== "" && ( |
| 131 | + <meta property="og:image" content={facebookImageUrl} /> |
| 132 | + )} |
| 133 | + {config.facebookCard.appId !== "" && ( |
| 134 | + <meta property="fb:app_id" content={config.facebookCard.appId} /> |
| 135 | + )} |
| 136 | + <meta |
| 137 | + name="twitter:title" |
| 138 | + content={ `Contact | ` + |
| 139 | + config.twitterCard.title || |
| 140 | + config.metadata.title || |
| 141 | + config.siteTitle |
| 142 | + } |
| 143 | + /> |
| 144 | + <meta |
| 145 | + name="twitter:description" |
| 146 | + content={ |
| 147 | + config.twitterCard.description || |
| 148 | + config.metadata.description || |
| 149 | + config.siteDescription |
| 150 | + } |
| 151 | + /> |
| 152 | + <meta name="twitter:url" content={canonical} /> |
| 153 | + {config.twitterCard.username && ( |
| 154 | + <meta name="twitter:site" content={config.twitterCard.username} /> |
| 155 | + )} |
| 156 | + {config.twitterCard.username && ( |
| 157 | + <meta name="twitter:creator" content={config.twitterCard.username} /> |
| 158 | + )} |
| 159 | + {config.twitterCard.imageUrl && ( |
| 160 | + <meta name="twitter:image" content={twitterImageUrl} /> |
| 161 | + )} |
| 162 | + {config.twitterCard.imageUrl && ( |
| 163 | + <meta name="twitter:card" content="summary_large_image" /> |
| 164 | + )} |
| 165 | + <script type="application/ld+json"> |
| 166 | + {JSON.stringify(jsonLd, undefined, 4)} |
| 167 | + </script> |
| 168 | + </Helmet> |
| 169 | + </> |
| 170 | + ); |
| 171 | +}; |
| 172 | + |
| 173 | +export default ContactMeta; |
0 commit comments