arc-xp-custom-blocks
Version:
arc libraries for ingala
165 lines (164 loc) • 6.95 kB
JavaScript
import generateResizedImageUrl from "./generateResizedImageUrl.js";
function articlePageMetaDataGenerator(fusionContext = {}, metaValue = () => {
}) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
const { siteProperties = {}, globalContent = {}, arcSite } = fusionContext;
const {
fallbackImageAlt,
fallbackImageAuth,
fallbackImageUrl,
fallbackImageFocalPoint,
metaData = { ogImageWidth: 1200, ogImageHeight: 630 },
resizerTokenVersion = "1",
resizerURL,
websiteDomain,
websiteName,
twitterUsername
} = siteProperties;
const author = (_a = globalContent == null ? void 0 : globalContent.credits) == null ? void 0 : _a.by[0];
const descriptionBasic = (_c = (_b = globalContent == null ? void 0 : globalContent.description) == null ? void 0 : _b.basic) == null ? void 0 : _c.trim();
const firstPublishDate = globalContent == null ? void 0 : globalContent.first_publish_date;
const lastUpdatedDate = globalContent == null ? void 0 : globalContent.last_updated_date;
const promoItems = (_d = globalContent == null ? void 0 : globalContent.promo_items) != null ? _d : {};
const sectionPath = (_f = (_e = globalContent == null ? void 0 : globalContent.taxonomy) == null ? void 0 : _e.primary_section) == null ? void 0 : _f.path;
const seoKeywords = (_i = (_h = (_g = globalContent == null ? void 0 : globalContent.taxonomy) == null ? void 0 : _g.seo_keywords) == null ? void 0 : _h.map((keyword) => keyword.trim()).filter(Boolean)) == null ? void 0 : _i.join(", ");
const tagsString = (_k = (_j = globalContent == null ? void 0 : globalContent.taxonomy) == null ? void 0 : _j.tags) == null ? void 0 : _k.map((tag) => {
var _a2;
return (_a2 = tag == null ? void 0 : tag.text) == null ? void 0 : _a2.trim();
}).filter(Boolean).join(", ");
const subheadlineBasic = (_m = (_l = globalContent == null ? void 0 : globalContent.subheadlines) == null ? void 0 : _l.basic) == null ? void 0 : _m.trim();
const websiteUrl = (_n = globalContent == null ? void 0 : globalContent.websites) == null ? void 0 : _n[arcSite].website_url;
const { ogImageWidth, ogImageHeight, twitterCard } = metaData;
const config = { resizerTokenVersion, resizerURL };
const fallbackPromoItem = {
url: fallbackImageUrl,
auth: { [resizerTokenVersion]: fallbackImageAuth },
focal_point: fallbackImageFocalPoint
};
const promoItemsList = Object.values(promoItems).map((item) => {
var _a2;
return (item == null ? void 0 : item.type) === "gallery" ? (_a2 = item == null ? void 0 : item.promo_items) == null ? void 0 : _a2.basic : (item == null ? void 0 : item.type) === "image" ? item : null;
}).filter(Boolean);
const promoItem = promoItemsList[0] || fallbackPromoItem;
const promoItemAltText = (promoItem == null ? void 0 : promoItem.alt_text) || (promoItem == null ? void 0 : promoItem.caption) || (promoItem == null ? void 0 : promoItem.subtitle);
const articleDescription = ((_o = metaValue("description")) == null ? void 0 : _o.trim()) || descriptionBasic || subheadlineBasic;
const articleKeywords = ((_p = metaValue("keywords")) == null ? void 0 : _p.trim()) || seoKeywords || tagsString;
const articleSectionUrl = sectionPath && websiteDomain && new URL(sectionPath, websiteDomain);
const articleTitle = ((_q = metaValue("title")) == null ? void 0 : _q.trim()) || subheadlineBasic || websiteName;
const imageUrl = generateResizedImageUrl(promoItem, config, ogImageWidth, ogImageHeight);
const imageAltText = promoItemsList[0] ? promoItemAltText : fallbackImageAlt;
const robots = ((_r = metaValue("robots")) == null ? void 0 : _r.trim()) || ((_s = siteProperties == null ? void 0 : siteProperties.robots) == null ? void 0 : _s.trim());
const url = websiteDomain && websiteUrl && `${websiteDomain}${websiteUrl}` || websiteDomain;
const metadata = {};
const metadataProperties = {
"robots": robots && {
name: "robots",
content: robots
},
"og:site_name": websiteName && {
property: "og:site_name",
content: websiteName
},
"og:type": {
property: "og:type",
content: "article"
},
"og:url": url && {
name: "og:url",
content: url
},
"description": articleDescription && {
name: "description",
content: articleDescription
},
"og:description": articleDescription && {
property: "og:description",
content: articleDescription
},
"twitter:description": articleDescription && {
name: "twitter:description",
content: articleDescription
},
"og:title": articleTitle && {
property: "og:title",
content: articleTitle
},
"twitter:title": articleTitle && {
name: "twitter:title",
content: articleTitle
},
"author": (author == null ? void 0 : author.name) && {
name: "author",
content: author == null ? void 0 : author.name
},
"article:author": (author == null ? void 0 : author.name) && {
property: "article:author",
content: author == null ? void 0 : author.name
},
"keywords": articleKeywords && {
name: "keywords",
content: articleKeywords
},
"og:see_also": articleSectionUrl && {
property: "og:see_also",
content: articleSectionUrl == null ? void 0 : articleSectionUrl.href
},
// Date
"og:updated_time": lastUpdatedDate && {
property: "og:updated_time",
content: lastUpdatedDate
},
"article:modified_time": lastUpdatedDate && {
property: "article:modified_time",
content: lastUpdatedDate
},
"article:published_time": firstPublishDate && {
property: "article:published_time",
content: firstPublishDate
},
// Image
"og:image": imageUrl && {
property: "og:image",
content: imageUrl
},
"twitter:image": imageUrl && {
name: "twitter:image",
content: imageUrl
},
"og:image:alt": imageAltText && {
property: "og:image:alt",
content: imageAltText
},
"twitter:image:alt": imageAltText && {
name: "twitter:image:alt",
content: imageAltText
},
"og:image:width": imageUrl && {
property: "og:image:width",
content: ogImageWidth
},
"og:image:height": imageUrl && {
property: "og:image:height",
content: ogImageHeight
},
"twitter:card": twitterCard && {
name: "twitter:card",
content: twitterCard
},
"twitter:site": twitterUsername && {
name: "twitter:site",
content: `@${twitterUsername}`
},
"twitter:creator": (author == null ? void 0 : author.twitter) && {
name: "twitter:creator",
content: `@${author == null ? void 0 : author.twitter}`
}
};
Object.entries(metadataProperties).forEach(([key, value]) => {
value ? metadata[key] = value : null;
});
return metadata;
}
export {
articlePageMetaDataGenerator as default
};