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.
460 lines (449 loc) • 14.5 kB
JavaScript
import { __assign } from 'tslib';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import { BEFORE_APP_SERIALIZED } from '@angular/platform-server';
import { Inject, Injectable, Optional, PLATFORM_ID, NgModule, defineInjectable, inject } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
/**
* @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 && 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: Injectable }
];
/** @nocollapse */
JsonLdService.ctorParameters = function () { return [
{ type: Object, decorators: [{ type: Optional }, { type: Inject, args: [PLATFORM_ID,] }] },
{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [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: 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: NgModule, args: [{
providers: [
JsonLdService, {
provide: BEFORE_APP_SERIALIZED,
useFactory: serializeJsonLdFactory,
deps: [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: Injectable, args: [{
providedIn: 'root',
},] }
];
/** @nocollapse */
SeoSocialShareService.ctorParameters = function () { return [
{ type: Meta },
{ type: Title }
]; };
/** @nocollapse */ SeoSocialShareService.ngInjectableDef = defineInjectable({ factory: function SeoSocialShareService_Factory() { return new SeoSocialShareService(inject(Meta), inject(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
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { JsonLdService, ServerJsonLdModule, BrowserJsonLdModule, SeoSocialShareService, serializeJsonLdFactory as ɵa };
//# sourceMappingURL=ngx-seo.js.map