UNPKG

file-cms

Version:

File based Content Management System, easy to use with content stored in native file system

214 lines (213 loc) 7.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSeoTags = void 0; var tslib_1 = require("tslib"); var getDefaultSeoSettings = function () { return ({ title: { key: "title", default: "" }, description: { key: "description", default: "" }, image: { key: "image", default: "" }, imageWidth: { key: "imageWidth", default: "" }, imageHeight: { key: "imageHeight", default: "" }, locale: { key: "locale", default: "en" }, siteName: { key: "siteName", default: "" }, seoType: { key: "seoType", default: "website" }, twitterCard: { key: "twitterCard", default: "summary_large_image" }, twitterSite: { key: "twitterSite", default: "" } }); }; var generateSeoTags = function (content, seo) { var seoSettings = getDefaultSeoSettings(); if (typeof seo === "object") { for (var _i = 0, _a = Object.keys(seo); _i < _a.length; _i++) { var key = _a[_i]; var providedKey = (typeof seo[key] == "string" ? { key: seo[key] } : seo[key]); seoSettings[key] = tslib_1.__assign(tslib_1.__assign({}, seoSettings[key]), providedKey); } } var _seoMetaTags = []; _seoMetaTags.push.apply(_seoMetaTags, generateTitleSeoTags(content, seoSettings.title)); _seoMetaTags.push.apply(_seoMetaTags, generateDescriptionSeoTags(content, seoSettings.description)); _seoMetaTags.push.apply(_seoMetaTags, generateImageSeoTags(content, seoSettings.image, seoSettings.imageWidth, seoSettings.imageHeight)); _seoMetaTags.push.apply(_seoMetaTags, generateLocaleSeoTags(content, seoSettings.locale)); _seoMetaTags.push.apply(_seoMetaTags, generateSiteNameSeoTags(content, seoSettings.siteName)); _seoMetaTags.push.apply(_seoMetaTags, generateSeoTypeSeoTags(content, seoSettings.seoType)); _seoMetaTags.push.apply(_seoMetaTags, generateTwitterCardSeoTags(content, seoSettings.twitterCard)); _seoMetaTags.push.apply(_seoMetaTags, generateTwitterSiteSeoTags(content, seoSettings.twitterSite)); return _seoMetaTags; }; exports.generateSeoTags = generateSeoTags; var getSeoValueFromContent = function (content, seoSettings) { return content[seoSettings.key] !== undefined ? content[seoSettings.key] : seoSettings.default; }; var generateTitleSeoTags = function (content, titleSeoSettings) { var _seoMetaTags = []; var title = getSeoValueFromContent(content, titleSeoSettings); if (title) { _seoMetaTags.push({ attributes: {}, content: title, tag: "title" }); _seoMetaTags.push({ attributes: { property: "og:title", content: title }, content: null, tag: "meta" }); _seoMetaTags.push({ attributes: { name: "twitter:title", content: title }, content: null, tag: "meta" }); } return _seoMetaTags; }; var generateDescriptionSeoTags = function (content, descriptionSeoSettings) { var _seoMetaTags = []; var description = getSeoValueFromContent(content, descriptionSeoSettings); if (description) { _seoMetaTags.push({ attributes: { name: "description", content: description }, content: null, tag: "meta" }); _seoMetaTags.push({ attributes: { property: "og:description", content: description }, content: null, tag: "meta" }); _seoMetaTags.push({ attributes: { name: "twitter:description", content: description }, content: null, tag: "meta" }); } return _seoMetaTags; }; var generateImageSeoTags = function (content, imageSeoSettings, imageWidthSeoSettings, imageHeightSeoSettings) { var _seoMetaTags = []; var image = getSeoValueFromContent(content, imageSeoSettings); if (image) { _seoMetaTags.push({ attributes: { property: "og:image", content: image }, content: null, tag: "meta" }); var imageWidth = getSeoValueFromContent(content, imageWidthSeoSettings); if (imageWidth) { _seoMetaTags.push({ attributes: { property: "og:image:width", content: imageWidth }, content: null, tag: "meta" }); } var imageHeight = getSeoValueFromContent(content, imageHeightSeoSettings); if (imageHeight) { _seoMetaTags.push({ attributes: { property: "og:image:height", content: imageHeight }, content: null, tag: "meta" }); } } return _seoMetaTags; }; var generateLocaleSeoTags = function (content, localeSeoSettings) { var _seoMetaTags = []; var locale = getSeoValueFromContent(content, localeSeoSettings); if (locale) { _seoMetaTags.push({ attributes: { property: "og:locale", content: locale }, content: null, tag: "meta" }); } return _seoMetaTags; }; var generateSiteNameSeoTags = function (content, siteNameSeoSettings) { var _seoMetaTags = []; var siteName = getSeoValueFromContent(content, siteNameSeoSettings); if (siteName) { _seoMetaTags.push({ attributes: { property: "og:site_name", content: siteName }, content: null, tag: "meta" }); } return _seoMetaTags; }; var generateSeoTypeSeoTags = function (content, seoTypeSeoSettings) { var _seoMetaTags = []; var seoType = getSeoValueFromContent(content, seoTypeSeoSettings); if (seoType) { _seoMetaTags.push({ attributes: { property: "og:type", content: seoType }, content: null, tag: "meta" }); } return _seoMetaTags; }; var generateTwitterCardSeoTags = function (content, twitterCardSeoSettings) { var _seoMetaTags = []; var twitterCard = getSeoValueFromContent(content, twitterCardSeoSettings); if (twitterCard) { _seoMetaTags.push({ attributes: { name: "twitter:card", content: twitterCard }, content: null, tag: "meta" }); } return _seoMetaTags; }; var generateTwitterSiteSeoTags = function (content, twitterSiteSeoSettings) { var _seoMetaTags = []; var twitterSite = getSeoValueFromContent(content, twitterSiteSeoSettings); if (twitterSite) { _seoMetaTags.push({ attributes: { name: "twitter:site", content: twitterSite }, content: null, tag: "meta" }); } return _seoMetaTags; };