ngx-seo
Version:
This library was generated with [Nx](https://nx.dev).
315 lines (309 loc) • 12.8 kB
JavaScript
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Meta, Title } from '@angular/platform-browser';
class JsonLdService {
constructor() {
this.document = inject(DOCUMENT);
}
setData(data) {
this.jsonLd = data;
this.inject();
}
getObject(type, rawData, context = 'http://schema.org') {
let object = {
'@type': type,
};
if (context) {
object = {
'@context': context,
...object,
};
}
if (rawData) {
object = {
...object,
...rawData,
};
}
return object;
}
inject() {
let ldJsonScriptTag = this.document.head.querySelector(`script[type='application/ld+json']`);
if (ldJsonScriptTag) {
ldJsonScriptTag.textContent = JSON.stringify(this.jsonLd);
}
else {
ldJsonScriptTag = this.document.createElement('script');
ldJsonScriptTag.setAttribute('type', 'application/ld+json');
ldJsonScriptTag.textContent = JSON.stringify(this.jsonLd);
this.document.head.appendChild(ldJsonScriptTag);
}
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: JsonLdService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: JsonLdService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: JsonLdService, decorators: [{
type: Injectable
}] });
var NgxSeoMetaTagAttr;
(function (NgxSeoMetaTagAttr) {
NgxSeoMetaTagAttr["name"] = "name";
NgxSeoMetaTagAttr["property"] = "property";
})(NgxSeoMetaTagAttr || (NgxSeoMetaTagAttr = {}));
class SeoSocialShareService {
constructor() {
this.metaService = inject(Meta);
this.titleService = inject(Title);
this.document = inject(DOCUMENT);
}
setData(data) {
this.setSection(data.section);
this.setKeywords(data.keywords);
this.setTitle(data.title);
this.setType(data.type);
this.setDescription(data.description);
this.setImage(data.image, data.imageAuxData);
this.setUrl(data.url);
this.setPublished(data.published);
this.setModified(data.modified);
this.setAuthor(data.author);
}
setKeywords(keywords) {
if (keywords) {
this.metaService.updateTag({ name: 'keywords', content: keywords });
}
else {
this.metaService.removeTag(`name='keywords'`);
}
}
setSection(section) {
if (section) {
this.metaService.updateTag({ name: 'article:section', content: section });
}
else {
this.metaService.removeTag(`name='article:section'`);
}
}
setTitle(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 });
this.metaService.updateTag({ itemprop: 'name', content: title }, `itemprop='name'`);
}
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'`);
this.metaService.removeTag(`itemprop='name'`);
}
}
setType(type) {
if (type && type.length) {
this.metaService.updateTag({ property: 'og:type', content: type });
}
else {
this.metaService.removeTag(`property='og:type'`);
}
}
setDescription(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 });
this.metaService.updateTag({ itemprop: 'description', content: description }, `itemprop='description'`);
}
else {
this.metaService.removeTag(`name='twitter:description'`);
this.metaService.removeTag(`property='og:description'`);
this.metaService.removeTag(`name='description'`);
this.metaService.removeTag(`itemprop='description'`);
}
}
setImage(image, auxData) {
if (image && image.length) {
this.metaService.updateTag({ name: 'twitter:image', content: image });
this.metaService.updateTag({ itemprop: 'image', content: image }, `itemprop='image'`);
this.metaService.updateTag({ property: 'og:image', content: image });
if (auxData && auxData.height) {
this.metaService.updateTag({ property: 'og:image:height', content: auxData.height.toString() });
}
else {
this.metaService.removeTag(`property='og:image:height'`);
}
if (auxData && auxData.width) {
this.metaService.updateTag({ property: 'og:image:width', content: auxData.width.toString() });
}
else {
this.metaService.removeTag(`property='og:image:width'`);
}
if (auxData && auxData.alt) {
this.metaService.updateTag({ property: 'og:image:alt', content: auxData.alt });
this.metaService.updateTag({ property: 'twitter:image:alt', content: auxData.alt });
}
else {
this.metaService.removeTag(`property='og:image:alt'`);
this.metaService.removeTag(`property='twitter:image:alt'`);
}
if (auxData && auxData.mimeType) {
this.metaService.updateTag({ property: 'og:image:type', content: auxData.mimeType });
}
else {
this.metaService.removeTag(`property='og:image:type'`);
}
if (auxData && auxData.secureUrl) {
this.metaService.updateTag({ property: 'og:image:secure_url', content: auxData.secureUrl });
}
else {
this.metaService.removeTag(`property='og:image:secure_url'`);
}
}
else {
this.metaService.removeTag(`name='twitter:image'`);
this.metaService.removeTag(`property='twitter:image:alt'`);
this.metaService.removeTag(`property='og:image'`);
this.metaService.removeTag(`property='og:image:height'`);
this.metaService.removeTag(`property='og:image:secure_url'`);
this.metaService.removeTag(`property='og:image:type'`);
this.metaService.removeTag(`property='og:image:alt'`);
this.metaService.removeTag(`property='og:image:width'`);
this.metaService.removeTag(`itemprop='image'`);
}
}
setUrl(url) {
if (url && url.length) {
this.metaService.updateTag({ property: 'og:url', content: url });
}
else {
this.metaService.removeTag(`property='og:url'`);
}
this.setCanonicalUrl(url);
}
setPublished(publishedDateString) {
if (publishedDateString) {
const 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'`);
}
}
setModified(modifiedDateString) {
if (modifiedDateString) {
const 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'`);
}
}
setAuthor(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'`);
}
}
setTwitterSiteCreator(site) {
if (site) {
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'`);
}
}
setTwitterCard(card) {
if (card) {
this.metaService.updateTag({ name: 'twitter:card', content: card });
}
else {
this.metaService.removeTag(`name='twitter:card'`);
}
}
setFbAppId(appId) {
if (appId) {
this.metaService.updateTag({ property: 'fb:app_id', content: appId });
}
else {
this.metaService.removeTag(`property='fb:app_id'`);
}
}
setMetaTag(metaTag) {
if (metaTag.value) {
const metaTagObject = {
[metaTag.attr]: metaTag.attrValue,
content: metaTag.value,
};
this.metaService.updateTag(metaTagObject);
}
else {
const selector = `${metaTag.attr}='${metaTag.attrValue}'`;
this.metaService.removeTag(selector);
}
}
setMetaTags(metaTags) {
for (const metaTag of metaTags) {
this.setMetaTag(metaTag);
}
}
setLanguageAlternativeUrl(lang, url) {
// first remove potential previous url
const selector = `link[rel='alternate'][hreflang='${lang}']`;
const languageAlternativeElement = this.document.head.querySelector(selector);
if (languageAlternativeElement) {
this.document.head.removeChild(languageAlternativeElement);
}
if (url && url.length) {
const link = this.document.createElement('link');
link.setAttribute('rel', 'alternate');
link.setAttribute('hreflang', lang);
link.setAttribute('href', url);
this.document.head.appendChild(link);
}
}
setCanonicalUrl(url) {
// first remove potential previous url
const selector = `link[rel='canonical']`;
const canonicalElement = this.document.head.querySelector(selector);
if (canonicalElement) {
this.document.head.removeChild(canonicalElement);
}
if (url && url.length) {
const link = this.document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', url);
this.document.head.appendChild(link);
}
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: SeoSocialShareService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: SeoSocialShareService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.7", ngImport: i0, type: SeoSocialShareService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}] });
/*
* Public API Surface of ngx-seo
*/
/**
* Generated bundle index. Do not edit.
*/
export { JsonLdService, NgxSeoMetaTagAttr, SeoSocialShareService };
//# sourceMappingURL=ngx-seo.mjs.map