UNPKG

cloudhubui

Version:

Various components to use in react projects

173 lines (150 loc) 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactHelmet = _interopRequireDefault(require("react-helmet")); var _useLocation2 = _interopRequireDefault(require("../customhooks/useLocation")); var _SeoContext = _interopRequireDefault(require("./SeoContext")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var PageSeo = function PageSeo(_ref) { var title = _ref.title, description = _ref.description, image = _ref.image, ogType = _ref.ogType, published = _ref.published, updated = _ref.updated, tags = _ref.tags; var _React$useContext = _react.default.useContext(_SeoContext.default), homeUrl = _React$useContext.homeUrl, siteName = _React$useContext.siteName, twitterHandle = _React$useContext.twitterHandle, fbId = _React$useContext.fbId, siteDescription = _React$useContext.description, siteImage = _React$useContext.image, defaultTitle = _React$useContext.title, keywords = _React$useContext.tags; var keywordsArray = [].concat(_toConsumableArray(tags || []), _toConsumableArray(keywords || [])); var _useLocation = (0, _useLocation2.default)(), location = _useLocation.location; var imageUrl; if (image) { var hasSlash = "".concat(homeUrl).slice("".concat(homeUrl).length - 1) === '/' || "".concat(image).slice(0, 1) === '/'; imageUrl = "".concat(image).includes('http') ? image : "".concat(homeUrl).concat(hasSlash ? '' : '/').concat(image); } else { var _hasSlash = "".concat(homeUrl).slice("".concat(homeUrl).length - 1) === '/' || "".concat(siteImage).slice(0, 1) === '/'; imageUrl = "".concat(siteImage).includes('http') ? siteImage : "".concat(homeUrl).concat(_hasSlash ? '' : '/').concat(siteImage); } var getMetaTags = function getMetaTags() { var metaTags = [{ itemprop: 'name', content: title || defaultTitle }, { itemprop: 'description', content: description || siteDescription }, { itemprop: 'image', content: "".concat(imageUrl) }, { name: 'description', property: 'description', content: description || siteDescription }, { name: 'keywords', property: 'keywords', content: keywordsArray.join(', ') }, { name: 'twitter:card', property: 'twitter:card', content: 'summary_large_image' }, { name: 'twitter:site', property: 'twitter:site', content: "".concat(twitterHandle) }, { name: 'twitter:title', property: 'twitter:title', content: "".concat(siteName, ": ").concat(title || defaultTitle) }, { name: 'twitter:description', property: 'twitter:description', content: description || siteDescription }, { name: 'twitter:creator', property: 'twitter:creator', content: twitterHandle }, { name: 'twitter:image:src', property: 'twitter:image:src', content: "".concat(imageUrl) }, { name: 'og:title', property: 'og:title', content: "".concat(siteName, ": ").concat(title || defaultTitle) }, { name: 'og:type', property: 'og:type', content: ogType }, { name: 'og:url', property: 'og:url', content: "".concat(homeUrl).concat(location.pathname) }, { name: 'og:image', property: 'og:image', content: "".concat(imageUrl) }, { name: 'og:description', property: 'og:description', content: description || siteDescription }, { name: 'og:site_name', property: 'og:site_name', content: siteName }, { name: 'fb:app_id', property: 'fb:app_id', content: fbId }]; return metaTags; }; return /*#__PURE__*/_react.default.createElement(_reactHelmet.default, { htmlAttributes: { lang: 'en' }, title: "".concat(siteName, ": ").concat(title || defaultTitle), link: [{ rel: 'canonical', href: "".concat(homeUrl).concat(location.pathname) }], meta: getMetaTags() }); }; PageSeo.defaultProps = { title: '', description: '', image: '', ogType: 'article', published: '', updated: '', tags: [] }; PageSeo.propTypes = { title: _propTypes.default.string, description: _propTypes.default.string, image: _propTypes.default.string, ogType: _propTypes.default.string, published: _propTypes.default.string, updated: _propTypes.default.string, tags: _propTypes.default.array }; var _default = PageSeo; exports.default = _default;