UNPKG

gatsby-theme-advanced

Version:
49 lines (48 loc) 2.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importDefault(require("react")); // This function acts as a type guard to prevent undefined content from being added const addTag = (tagList, property, content) => { tagList.push(<meta property={property} content={content}/>); }; const createArticleTagList = (postData, userData) => { const metaTags = []; addTag(metaTags, "article:published_time", postData.datePublished.toISOString()); addTag(metaTags, "article:modified_time", postData.dateModified.toISOString()); addTag(metaTags, "article:author", "http://examples.opengraphprotocol.us/profile.html"); if (userData) { addTag(metaTags, "profile:first_name", userData.firstName); addTag(metaTags, "profile:last_name", userData.lastName); addTag(metaTags, "profile:username", userData.id); } addTag(metaTags, "article:section", postData.category); postData.tags.forEach((tag) => { addTag(metaTags, "article:tag", tag); }); return metaTags; }; const OpenGraphTags = ({ seoData, websiteData, userData, postData, }) => { const { isArticle, type, title, imageUrl, imageAlt, url, description } = seoData; const siteName = websiteData.name; if (!imageUrl || !imageAlt) return []; const metaTags = []; addTag(metaTags, "og:title", title); addTag(metaTags, "og:type", type); addTag(metaTags, "og:url", url); addTag(metaTags, "og:image", imageUrl); addTag(metaTags, "og:image:alt", imageAlt); addTag(metaTags, "og:site_name", siteName); if (description) addTag(metaTags, "og:description", description); if (websiteData.fbAppId) addTag(metaTags, "fb:app_id", websiteData.fbAppId); if (isArticle && postData) metaTags.push(...createArticleTagList(postData, userData)); // Add unique keys and return return metaTags.map((tag) => (Object.assign(Object.assign({}, tag), { key: `${tag.props.property}-${tag.props.content}` }))); }; exports.default = OpenGraphTags;