ngx-seo
Version:
`ngx-seo` is a library to help generate and inject proper JSON-LD objects and Meta tags that allow for social sharing into server-side generated Angular applications using Universal.
495 lines (480 loc) • 18.8 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common'), require('@angular/platform-server'), require('@angular/core'), require('@angular/platform-browser')) :
typeof define === 'function' && define.amd ? define('ngx-seo', ['exports', '@angular/common', '@angular/platform-server', '@angular/core', '@angular/platform-browser'], factory) :
(factory((global['ngx-seo'] = {}),global.ng.common,global.ng['platform-server'],global.ng.core,global.ng.platformBrowser));
}(this, (function (exports,common,platformServer,i0,i1) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var JsonLdService = /** @class */ (function () {
function JsonLdService(platformId, doc) {
this.platformId = platformId;
this.doc = doc;
this.jsonLd = {};
}
/**
* @param {?} type
* @param {?} rawData
* @return {?}
*/
JsonLdService.prototype.setData = /**
* @param {?} type
* @param {?} rawData
* @return {?}
*/
function (type, rawData) {
this.jsonLd = this.getObject(type, rawData);
this.injectBrowser();
};
/**
* @param {?} type
* @param {?=} rawData
* @return {?}
*/
JsonLdService.prototype.getObject = /**
* @param {?} type
* @param {?=} rawData
* @return {?}
*/
function (type, rawData) {
/** @type {?} */
var object = {
'@context': 'http://schema.org',
'@type': type,
};
if (rawData) {
object = __assign({}, object, rawData);
}
return object;
};
/**
* @return {?}
*/
JsonLdService.prototype.toJson = /**
* @return {?}
*/
function () {
return JSON.stringify(this.jsonLd);
};
/**
* @private
* @return {?}
*/
JsonLdService.prototype.injectBrowser = /**
* @private
* @return {?}
*/
function () {
if (this.platformId && common.isPlatformBrowser(this.platformId)) {
/** @type {?} */
var ldJsonScriptTag = this.doc.head.querySelector("script[type='application/ld+json']");
if (ldJsonScriptTag) {
ldJsonScriptTag.textContent = this.toJson();
}
else {
ldJsonScriptTag = this.doc.createElement('script');
ldJsonScriptTag.setAttribute('type', 'application/ld+json');
ldJsonScriptTag.textContent = this.toJson();
this.doc.head.appendChild(ldJsonScriptTag);
}
}
};
JsonLdService.decorators = [
{ type: i0.Injectable }
];
/** @nocollapse */
JsonLdService.ctorParameters = function () {
return [
{ type: Object, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [i0.PLATFORM_ID,] }] },
{ type: undefined, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [common.DOCUMENT,] }] }
];
};
return JsonLdService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var BrowserJsonLdModule = /** @class */ (function () {
function BrowserJsonLdModule() {
}
BrowserJsonLdModule.decorators = [
{ type: i0.NgModule, args: [{
providers: [
JsonLdService,
],
},] }
];
return BrowserJsonLdModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @param {?} doc
* @param {?} jsonLdService
* @return {?}
*/
function serializeJsonLdFactory(doc, jsonLdService) {
/** @type {?} */
var serializeAndInject = ( /**
* @return {?}
*/function () {
/** @type {?} */
var script = doc.createElement('script');
script.setAttribute('type', 'application/ld+json');
script.textContent = jsonLdService.toJson();
doc.head.appendChild(script);
});
return serializeAndInject;
}
var ServerJsonLdModule = /** @class */ (function () {
function ServerJsonLdModule() {
}
ServerJsonLdModule.decorators = [
{ type: i0.NgModule, args: [{
providers: [
JsonLdService, {
provide: platformServer.BEFORE_APP_SERIALIZED,
useFactory: serializeJsonLdFactory,
deps: [common.DOCUMENT, JsonLdService],
multi: true,
},
],
},] }
];
return ServerJsonLdModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var SeoSocialShareService = /** @class */ (function () {
function SeoSocialShareService(metaService, titleService) {
this.metaService = metaService;
this.titleService = titleService;
}
/**
* @param {?} data
* @return {?}
*/
SeoSocialShareService.prototype.setData = /**
* @param {?} data
* @return {?}
*/
function (data) {
this.setTitle(data.title);
this.setMetaDescription(data.description);
this.setUrl(data.url);
this.setImage(data.image);
this.setPublished(data.published);
this.setModified(data.modified);
this.setAuthor(data.author);
this.setSection(data.section);
this.setType(data.type);
};
/**
* @param {?=} newSection
* @return {?}
*/
SeoSocialShareService.prototype.setSection = /**
* @param {?=} newSection
* @return {?}
*/
function (newSection) {
if (newSection && newSection.length) {
this.metaService.updateTag({ name: 'article:section', content: newSection });
}
else {
this.metaService.removeTag("name='article:section'");
}
};
/**
* @param {?=} site
* @return {?}
*/
SeoSocialShareService.prototype.setTwitterSiteCreator = /**
* @param {?=} site
* @return {?}
*/
function (site) {
if (site && site.length) {
this.metaService.updateTag({ name: 'twitter:site', content: site });
this.metaService.updateTag({ name: 'twitter:creator', content: site });
}
else {
this.metaService.removeTag("name='twitter:site'");
this.metaService.removeTag("name='twitter:creator'");
}
};
/**
* @param {?=} card
* @return {?}
*/
SeoSocialShareService.prototype.setTwitterCard = /**
* @param {?=} card
* @return {?}
*/
function (card) {
if (card && card.length) {
this.metaService.updateTag({ name: 'twitter:card', content: card });
}
else {
this.metaService.removeTag("name='twitter:card'");
}
};
/**
* @param {?=} appId
* @return {?}
*/
SeoSocialShareService.prototype.setFbAppId = /**
* @param {?=} appId
* @return {?}
*/
function (appId) {
if (appId && appId.length) {
this.metaService.updateTag({ property: 'fb:app_id', content: appId });
}
else {
this.metaService.removeTag("property='fb:app_id'");
}
};
/**
* @private
* @param {?=} title
* @return {?}
*/
SeoSocialShareService.prototype.setTitle = /**
* @private
* @param {?=} title
* @return {?}
*/
function (title) {
if (title === void 0) {
title = '';
}
this.titleService.setTitle(title);
if (title && title.length) {
this.metaService.updateTag({ name: 'twitter:title', content: title });
this.metaService.updateTag({ name: 'twitter:image:alt', content: title });
this.metaService.updateTag({ property: 'og:image:alt', content: title });
this.metaService.updateTag({ property: 'og:title', content: title });
this.metaService.updateTag({ name: 'title', content: title });
}
else {
this.metaService.removeTag("name='twitter:title'");
this.metaService.removeTag("name='twitter:image:alt'");
this.metaService.removeTag("property='og:image:alt'");
this.metaService.removeTag("property='og:title'");
this.metaService.removeTag("name='title'");
}
};
/**
* @private
* @param {?=} type
* @return {?}
*/
SeoSocialShareService.prototype.setType = /**
* @private
* @param {?=} type
* @return {?}
*/
function (type) {
if (type && type.length) {
this.metaService.updateTag({ property: 'og:type', content: type });
}
else {
this.metaService.removeTag("property='og:type'");
}
};
/**
* @private
* @param {?=} description
* @return {?}
*/
SeoSocialShareService.prototype.setMetaDescription = /**
* @private
* @param {?=} description
* @return {?}
*/
function (description) {
if (description && description.length) {
this.metaService.updateTag({ name: 'twitter:description', content: description });
this.metaService.updateTag({ property: 'og:description', content: description });
this.metaService.updateTag({ name: 'description', content: description });
}
else {
this.metaService.removeTag("name='twitter:description'");
this.metaService.removeTag("property='og:description'");
this.metaService.removeTag("name='description'");
}
};
/**
* @private
* @param {?=} image
* @return {?}
*/
SeoSocialShareService.prototype.setImage = /**
* @private
* @param {?=} image
* @return {?}
*/
function (image) {
if (image && image.length) {
this.metaService.updateTag({ name: 'twitter:image', content: image });
this.metaService.updateTag({ property: 'og:image', content: image });
this.metaService.updateTag({ property: 'og:image:height', content: '630' });
}
else {
this.metaService.removeTag("name='twitter:image'");
this.metaService.removeTag("property='og:image'");
this.metaService.removeTag("property='og:image:height'");
}
};
/**
* @private
* @param {?=} url
* @return {?}
*/
SeoSocialShareService.prototype.setUrl = /**
* @private
* @param {?=} url
* @return {?}
*/
function (url) {
if (url && url.length) {
this.metaService.updateTag({ property: 'og:url', content: url });
}
else {
this.metaService.removeTag("property='og:url'");
}
};
/**
* @private
* @param {?=} publishedDateString
* @return {?}
*/
SeoSocialShareService.prototype.setPublished = /**
* @private
* @param {?=} publishedDateString
* @return {?}
*/
function (publishedDateString) {
if (publishedDateString) {
/** @type {?} */
var publishedDate = new Date(publishedDateString);
this.metaService.updateTag({ name: 'article:published_time', content: publishedDate.toISOString() });
this.metaService.updateTag({ name: 'published_date', content: publishedDate.toISOString() });
}
else {
this.metaService.removeTag("name='article:published_time'");
this.metaService.removeTag("name='publication_date'");
}
};
/**
* @private
* @param {?=} modifiedDateString
* @return {?}
*/
SeoSocialShareService.prototype.setModified = /**
* @private
* @param {?=} modifiedDateString
* @return {?}
*/
function (modifiedDateString) {
if (modifiedDateString) {
/** @type {?} */
var modifiedDate = new Date(modifiedDateString);
this.metaService.updateTag({ name: 'article:modified_time', content: modifiedDate.toISOString() });
this.metaService.updateTag({ name: 'og:updated_time', content: modifiedDate.toISOString() });
}
else {
this.metaService.removeTag("name='article:modified_time'");
this.metaService.removeTag("name='og:updated_time'");
}
};
/**
* @private
* @param {?=} author
* @return {?}
*/
SeoSocialShareService.prototype.setAuthor = /**
* @private
* @param {?=} author
* @return {?}
*/
function (author) {
if (author && author.length) {
this.metaService.updateTag({ name: 'article:author', content: author });
this.metaService.updateTag({ name: 'author', content: author });
}
else {
this.metaService.removeTag("name='article:author'");
this.metaService.removeTag("name='author'");
}
};
SeoSocialShareService.decorators = [
{ type: i0.Injectable, args: [{
providedIn: 'root',
},] }
];
/** @nocollapse */
SeoSocialShareService.ctorParameters = function () {
return [
{ type: i1.Meta },
{ type: i1.Title }
];
};
/** @nocollapse */ SeoSocialShareService.ngInjectableDef = i0.defineInjectable({ factory: function SeoSocialShareService_Factory() { return new SeoSocialShareService(i0.inject(i1.Meta), i0.inject(i1.Title)); }, token: SeoSocialShareService, providedIn: "root" });
return SeoSocialShareService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
exports.JsonLdService = JsonLdService;
exports.ServerJsonLdModule = ServerJsonLdModule;
exports.BrowserJsonLdModule = BrowserJsonLdModule;
exports.SeoSocialShareService = SeoSocialShareService;
exports.ɵa = serializeJsonLdFactory;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=ngx-seo.umd.js.map