All files / src/app/services seo.service.ts

100% Statements 161/161
100% Branches 72/72
100% Functions 21/21
100% Lines 152/152

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 4561x 1x 1x 1x 1x 1x 1x             1x                           160x     160x   160x                             160x 160x 160x 160x 160x 160x 160x 160x 160x 160x             1x 405x 405x 405x 405x 405x   405x 16x   389x     405x   405x 405x 405x 405x             1x 405x   401x   405x 405x 405x 405x 405x   405x   405x   405x   405x 405x     405x 405x 405x 4x 4x           405x         405x                     1x 405x 1x     405x 1x     405x 1x     405x 1x                 405x 405x 301x 45x     104x   405x 27x 11x   69x 69x     27x 11x   43x 43x                   1x 654x               1x 405x 405x 405x   405x 29x 13x   29x 13x   29x 13x   29x 15x     405x 405x 35x                   370x 66x 66x   131x         66x 65x                                                           304x 81x                             223x 65x                             158x 81x                                 405x             1x 405x 21x   384x   405x 405x 349x 7x   349x 328x   349x   405x 342x   63x                 1219x 1219x 91x             1219x 1219x 1219x   3252x   1219x             1x 1624x 1216x 1216x               1x 1x           1x 8x               586x 585x 585x                         1x                   1x 2668x 2664x           4x                 1x 30x           1x 30x   1x  
import { DOCUMENT, isPlatformBrowser, PlatformLocation } from '@angular/common';
import { Inject, Injectable, LOCALE_ID, PLATFORM_ID, Renderer2, RendererFactory2, ViewEncapsulation } from '@angular/core';
import { DomSanitizer, Meta, SafeHtml, Title } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { Observable, Subject } from 'rxjs';
import { environment } from '../../environments/environment';
import { APP_CONFIG, InterfaceAppConfig } from '../app-config';
import { ConfigSeoModel, HtmlLinkElementModel, HttpStatusModel, PageBaseModel } from '../models';
 
/**
 * Seo Service
 */
@Injectable()
export class SeoService {
    /** Renderer2 object */
    renderer: Renderer2;
    /**
     * custom SEO Meta Tags
     * We need to keep them in order to remove them before load tags of next page
     */
    customSEOMetaTags: Array<{
        /** type of meta tag; name or property */
        type: string,
        /** value of name or property, NOT content value */
        value: string}>;
 
    /** config for SEO */
    configSEO: ConfigSeoModel = {};
 
    /** http status */
    private readonly httpStatus$ = new Subject<HttpStatusModel>();
    /** json-LD */
    private readonly jsonLD$ = new Subject<SafeHtml>();
 
    /**
     * constructor of SeoService
     * @param meta: Meta
     * @param titleService: Title
     * @param router: Router
     * @param rendererFactory: RendererFactory2
     * @param platformLocation: PlatformLocation
     * @param sanitizer: DomSanitizer
     * @param platformId: PLATFORM_ID
     * @param appConfig: APP_CONFIG
     * @param locale: LOCALE_ID
     * @param doc: DOCUMENT
     */
    constructor(private readonly meta: Meta,
                private readonly titleService: Title,
                private readonly router: Router,
                private readonly rendererFactory: RendererFactory2,
                private readonly platformLocation: PlatformLocation,
                private readonly sanitizer: DomSanitizer,
                @Inject(PLATFORM_ID) private readonly platformId: string,
                @Inject(APP_CONFIG) private readonly appConfig: InterfaceAppConfig,
                @Inject(LOCALE_ID) private readonly locale: string,
                @Inject(DOCUMENT) public doc) {
    }
 
    /**
     * Set Html Tags
     * @param page: current page
     */
    setSEOData(page: PageBaseModel): void {
        const tempPage = {...page};
        this.titleService.setTitle(tempPage.title);
        this.meta.updateTag({itemprop: 'name', content: tempPage.title}, "itemprop='name'");
        this.meta.updateTag({name: 'description', content: tempPage.description}, "name='description'");
        this.meta.updateTag({itemprop: 'description', content: tempPage.description}, "itemprop='description'");
 
        if (tempPage.image) {
            this.meta.updateTag({itemprop: 'image', content: tempPage.image.src}, "itemprop='image'");
        } else {
            this.meta.removeTag("itemprop='image'");
        }
 
        this.meta.updateTag({name: 'apple-mobile-web-app-title', content: tempPage.title}, "name='apple-mobile-web-app-title'");
 
        this.setLocalAndUrlHtmlTags(tempPage);
        this.setProjectSpecifiedHtmlTags(tempPage);
        this.setCustomSEOHtmlTags(tempPage);
        this.setJsonLD(tempPage);
    }
 
    /**
     * Set Locale and Url Html Tags
     * @param tempPage: current page
     */
    setLocalAndUrlHtmlTags(tempPage: PageBaseModel): void {
        if (!tempPage.hasOwnProperty('locales')) {
            // tslint:disable-next-line:no-string-literal
            tempPage['locales'] = [];
        }
        const protocol = environment.protocol;
        const host = environment.host;
        const cultureCode = this.locale;
        const languageCode = this.locale.substr(0, 2);
        const slug = this.platformLocation.pathname;
 
        tempPage.canonicalUrl = `${protocol}${host}${slug}`;
 
        this.updateLink({rel: 'canonical', href: tempPage.canonicalUrl}, "link[rel='canonical']");
 
        this.meta.updateTag({httpEquiv: 'Content-Language', content: languageCode}, "httpEquiv='Content-Language'");
 
        this.meta.updateTag({property: 'og:url', content: tempPage.canonicalUrl}, "property='og:url'");
        this.meta.updateTag({property: 'og:locale', content: cultureCode.replace('-', '_')},
            "property='og:locale'");
 
        this.meta.removeTag("property='og:locale:alternate'");
        this.removeLink("link[rel='alternate']");
        for (const langAlternate of tempPage.locales) {
            this.meta.updateTag({property: 'og:locale:alternate', content: langAlternate.cultureCode.replace('-', '_')});
            this.updateLink({
                rel: 'alternate',
                href: `${protocol}${host}/${langAlternate.slug}`,
                hreflang: langAlternate.cultureCode
            });
        }
        this.updateLink({
            rel: 'alternate',
            href: tempPage.canonicalUrl,
            hreflang: cultureCode
        });
        this.updateLink({
            rel: 'alternate',
            href: tempPage.canonicalUrl,
            hreflang: 'x-default'
        });
    }
 
    /**
     * Set Project Specified Html Tags
     * @param tempPage: current page
     */
    setProjectSpecifiedHtmlTags(tempPage: PageBaseModel): void {
        if (environment.defaultData['twitter:site']) {
            this.meta.updateTag({name: 'twitter:site', content: environment.defaultData['twitter:site']},
                "name='twitter:site'");
        }
        if (environment.defaultData['twitter:creator']) {
            this.meta.updateTag({name: 'twitter:creator', content: environment.defaultData['twitter:creator']},
                "name='twitter:creator'");
        }
        if (environment.defaultData['fb:app_id']) {
            this.meta.updateTag({property: 'fb:app_id', content: environment.defaultData['fb:app_id']},
                "property='fb:app_id'");
        }
        if (environment.defaultData['fb:admins']) {
            this.meta.updateTag({property: 'fb:admins', content: environment.defaultData['fb:admins']},
                "property='fb:admins'");
        }
    }
 
    /**
     * Set Custom SEO Html Tags
     * @param tempPage: current page
     */
    setCustomSEOHtmlTags(tempPage: PageBaseModel): void {
        if (this.customSEOMetaTags) {
            for (const customSEO of this.customSEOMetaTags) {
                this.meta.removeTag(`${customSEO.type}='${customSEO.value}'`);
            }
        } else {
            this.customSEOMetaTags = [];
        }
        if (tempPage.hasOwnProperty('seo')) {
            if (tempPage.seo.hasOwnProperty('names')) {
                Object.keys(tempPage.seo.names)
                    .forEach((prop: string) => {
                        this.meta.updateTag({name: prop, content: tempPage.seo.names[prop]}, `name='${prop}'`);
                        this.customSEOMetaTags.push({type: 'name', value: prop});
                    });
            }
            if (tempPage.seo.hasOwnProperty('properties')) {
                Object.keys(tempPage.seo.properties)
                    .forEach((prop: string) => {
                        this.meta.updateTag({property: prop, content: tempPage.seo.properties[prop]}, `property='${prop}'`);
                        this.customSEOMetaTags.push({type: 'property', value: prop});
                    });
            }
        }
    }
 
    /**
     * Get Plaintext by Html
     * @param htmlContent: html content
     */
    getPlaintextByHtml(htmlContent: string): string {
        return htmlContent ? String(htmlContent)
            .replace(/<[^>]+>/gm, '') : '';
    }
 
    /**
     * Get JSON-LDs by content of current page
     * @param tempPage: current page
     */
    getJsonLDByContentOfCurrentPage(tempPage: any): Array<any> {
        const jsonLDs = [];
        const images = [];
        const image = tempPage.image ? tempPage.image :
            this.configSEO.defaultImageForSEO ? this.configSEO.defaultImageForSEO : undefined;
        if (image) {
            if (image.src1x1) {
                images.push(image.src1x1);
            }
            if (image.src4x3) {
                images.push(image.src4x3);
            }
            if (image.src16x9) {
                images.push(image.src16x9);
            }
            if (image.src) {
                images.push(image.src);
            }
        }
        const publisher = this.configSEO.defaultPublisher ? this.configSEO.defaultPublisher : undefined;
        if (['/quote', '/alinti', 'guzel-soz'].indexOf(tempPage.routePath) > -1 && tempPage.hasOwnProperty('whoSaidThat')) {
            jsonLDs.push({
                '@type': 'Quotation',
                creator: {
                    '@type': 'Person',
                    name: this.getPlaintextByHtml(tempPage.whoSaidThat)
                },
                text: this.getPlaintextByHtml(tempPage.content),
                image: images,
                mainEntityOfPage: tempPage.canonicalUrl
            });
        } else if (['/quote', '/alinti', 'guzel-soz'].indexOf(tempPage.routePath) > -1 && tempPage.hasOwnProperty('persons')) {
            const persons = [];
            Object.keys(tempPage.persons)
                .forEach(key => {
                    persons.push({
                        '@type': 'Person',
                        name: tempPage.persons[key]
                    });
                });
            if (persons.length > 1) {
                jsonLDs.push({
                    '@type': 'Conversation',
                    name: this.getPlaintextByHtml(tempPage.title),
                    hasPart: [
                        {
                            '@type': 'Message',
                            sender: persons[0],
                            recipient: persons[1],
                            about: {
                                '@type': 'Thing',
                                name: '...'
                            },
                            datePublished: new Date(tempPage.created.seconds * 1000)
                        },
                        {
                            '@type': 'Message',
                            sender: persons[1],
                            recipient: persons[0],
                            about: {
                                '@type': 'Thing',
                                name: '...'
                            },
                            datePublished: new Date(tempPage.created.seconds * 1000)
                        }
                    ],
                    text: this.getPlaintextByHtml(tempPage.content),
                    image: images,
                    mainEntityOfPage: tempPage.canonicalUrl
                });
            }
        } else if (['/blog', '/gunluk'].indexOf(tempPage.routePath) > -1) {
            jsonLDs.push({
                '@type': 'BlogPosting',
                author: {
                    '@type': 'Person',
                    name: this.getPlaintextByHtml(tempPage.createdBy)
                },
                publisher: {...{'@type': 'Organization'}, ...publisher},
                dateCreated: new Date(tempPage.created.seconds * 1000),
                dateModified: new Date(tempPage.changed.seconds * 1000),
                datePublished: new Date(tempPage.created.seconds * 1000),
                headline: this.getPlaintextByHtml(tempPage.title),
                description: tempPage.description,
                image: images,
                mainEntityOfPage: tempPage.canonicalUrl
            });
        } else if (['/article', '/makale'].indexOf(tempPage.routePath) > -1) {
            jsonLDs.push({
                '@type': 'Article',
                author: {
                    '@type': 'Person',
                    name: this.getPlaintextByHtml(tempPage.createdBy)
                },
                publisher: {...{'@type': 'Organization'}, ...publisher},
                dateCreated: new Date(tempPage.created.seconds * 1000),
                dateModified: new Date(tempPage.changed.seconds * 1000),
                datePublished: new Date(tempPage.created.seconds * 1000),
                headline: this.getPlaintextByHtml(tempPage.title),
                description: tempPage.description,
                image: images,
                mainEntityOfPage: tempPage.canonicalUrl
            });
        } else if (['/joke', '/eglence', '/fikra', '/saka', '/espri', '/soguk-espri'].indexOf(tempPage.routePath) > -1) {
            jsonLDs.push({
                '@type': 'SocialMediaPosting',
                author: {
                    '@type': 'Person',
                    name: this.getPlaintextByHtml(tempPage.createdBy)
                },
                publisher: {...{'@type': 'Organization'}, ...publisher},
                dateCreated: new Date(tempPage.created.seconds * 1000),
                dateModified: new Date(tempPage.changed.seconds * 1000),
                datePublished: new Date(tempPage.created.seconds * 1000),
                headline: this.getPlaintextByHtml(tempPage.title),
                description: tempPage.description,
                image: images,
                mainEntityOfPage: tempPage.canonicalUrl
            });
        }
 
        return jsonLDs;
    }
 
    /**
     * Set JSON-LD
     * @param tempPage: current page
     */
    setJsonLD(tempPage: PageBaseModel): void {
        if (tempPage.hasOwnProperty('jsonLDs')) {
            tempPage.jsonLDs.push(...this.getJsonLDByContentOfCurrentPage(tempPage));
        } else {
            tempPage.jsonLDs = this.getJsonLDByContentOfCurrentPage(tempPage);
        }
        let jsonLDList = '';
        for (const jsonLD of tempPage.jsonLDs) {
            if (jsonLDList !== '') {
                jsonLDList += ',';
            }
            if (!jsonLD.hasOwnProperty('@context')) {
                jsonLD['@context'] = 'http://schema.org/';
            }
            jsonLDList += JSON.stringify(jsonLD, undefined, 0);
        }
        if (jsonLDList) {
            this.jsonLD$.next(this.sanitizer.bypassSecurityTrustHtml(`<script type="application/ld+json">[${jsonLDList}]</script>`));
        } else {
            this.jsonLD$.next(undefined);
        }
    }
 
    /**
     * add or update link to head of document
     * @param linkObject: tags of link
     * @param attrSelector: selector to remove old link elements
     */
    updateLink(linkObject: HtmlLinkElementModel, attrSelector?: string): void {
        if (this.renderer === undefined) {
            this.renderer = this.rendererFactory.createRenderer(this.doc, {
                id: '-1',
                encapsulation: ViewEncapsulation.None,
                styles: [],
                data: {}
            });
        }
        this.removeLink(attrSelector);
        const link = this.renderer.createElement('link');
        Object.keys(linkObject)
            .forEach((prop: string) => {
                this.renderer.setAttribute(link, prop, linkObject[prop]);
            });
        this.renderer.appendChild(this.doc.head, link);
    }
 
    /**
     * add or remove link from head of document
     * @param attrSelector: selector to remove old link elements
     */
    removeLink(attrSelector: string): void {
        if (attrSelector) {
            for (const oldLink of this.doc.querySelectorAll(attrSelector)) {
                this.renderer.removeChild(this.doc.head, oldLink);
            }
        }
    }
 
    /**
     * get title of current page
     */
    getTitle(): string {
        return this.titleService.getTitle();
    }
 
    /**
     * get meta object of current page
     */
    getMeta(): Meta {
        return this.meta;
    }
 
    /**
     * http 301 redirection
     * @param destinationURL: destination URL
     * @param isExternal: is this url external?
     */
    http301(destinationURL: string, isExternal = false): void {
        const destURL = destinationURL.replace(/[\/]+/g, '/');
        if (isPlatformBrowser(this.platformId)) {
            // istanbul ignore next
            if (isExternal && !this.appConfig.isUnitTest) {
                // this.router.navigate can't work because it is out of base url
                window.location.href = destURL;
            } else {
                this.router.navigate([destURL])
                    .catch(// istanbul ignore next
                        reason => {
                        console.error(reason);
                    });
            }
        } else {
            this.httpStatus$.next({
                code: 301,
                htmlContent: `--http-redirect-301--${destURL}--end-of-http-redirect-301--`
            });
        }
    }
 
    /**
     * http 404 not found
     */
    http404(): void {
        if (isPlatformBrowser(this.platformId)) {
            this.router.navigate([this.router.url, 'http-404'])
                .catch(// istanbul ignore next
                    reason => {
                    console.error(reason);
                });
        } else {
            this.httpStatus$.next({
                code: 404
            });
        }
    }
 
    /**
     * get http status
     */
    getHttpStatus(): Observable<HttpStatusModel> {
        return this.httpStatus$.asObservable();
    }
 
    /**
     * get json-LD
     */
    getJsonLD(): Observable<SafeHtml> {
        return this.jsonLD$.asObservable();
    }
}