rasengan
Version:
The modern React Framework
84 lines (83 loc) • 3.82 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/**
* This function generates metadata useful for pages to show images when sharing on social media
* @param {Metadata[]} metadatas
*/
export const generateMetadata = (metadatas) => {
const metadataElements = [];
metadatas.forEach((metadata) => {
const { openGraph, twitter, links, metaTags } = metadata;
// Handling openGraph
if (openGraph) {
const { title, description, url, image, width, height, type } = openGraph;
if (title) {
metadataElements.push(_jsx("meta", { property: "og:title", content: title, "data-rg": "true" }, "og:title"));
}
if (description) {
metadataElements.push(_jsx("meta", { property: "og:description", content: description, "data-rg": "true" }, "og:description"));
}
if (url) {
metadataElements.push(_jsx("meta", { property: "og:url", content: url, "data-rg": "true" }, "og:url"));
}
if (image) {
metadataElements.push(_jsx("meta", { property: "og:image", content: image, "data-rg": "true" }, "og:image"));
}
if (width) {
metadataElements.push(_jsx("meta", { property: "og:image:width", content: width, "data-rg": "true" }, "og:image:width"));
}
if (height) {
metadataElements.push(_jsx("meta", { property: "og:image:height", content: height, "data-rg": "true" }, "og:image:height"));
}
metadataElements.push(_jsx("meta", { property: "og:type", content: type || 'website', "data-rg": "true" }, "og:type"));
}
// Handling twitter
if (twitter) {
const { card, site, creator, image, title, description } = twitter;
metadataElements.push(_jsx("meta", { name: "twitter:card", content: card || 'summary_large_image', "data-rg": "true" }, "twitter:card"));
if (site) {
metadataElements.push(_jsx("meta", { name: "twitter:site", content: site, "data-rg": "true" }, "twitter:site"));
}
if (creator) {
metadataElements.push(_jsx("meta", { name: "twitter:creator", content: creator, "data-rg": "true" }, "twitter:creator"));
}
if (image) {
metadataElements.push(_jsx("meta", { name: "twitter:image", content: image, "data-rg": "true" }, "twitter:image"));
}
if (title) {
metadataElements.push(_jsx("meta", { name: "twitter:title", content: title, "data-rg": "true" }, "twitter:title"));
}
if (description) {
metadataElements.push(_jsx("meta", { name: "twitter:description", content: description, "data-rg": "true" }, "twitter:description"));
}
}
// Handling links
if (links) {
metadataElements.push(...generateLinks(links));
}
// Handling metadata tags
if (metaTags) {
metadataElements.push(...generateMetaTags(metaTags));
}
});
return metadataElements;
};
/**
* This function generates links for metadata
*/
const generateLinks = (links) => {
return links.map((link) => {
const { rel, sizes, type, href } = link;
return (_jsx("link", { rel: rel, sizes: sizes || '32x32', type: type || 'image/png', href: href, "data-rg": "true" }, rel));
});
};
/**
* This function generates meta tags for metadata
* @param metaTags
* @returns
*/
const generateMetaTags = (metaTags) => {
return metaTags.map((metaTag) => {
const { content, name, property } = metaTag;
return (_jsx("meta", { property: property ?? name, content: content, "data-rg": "true" }, property ?? name));
});
};