Skip to content
This repository was archived by the owner on Oct 1, 2018. It is now read-only.

Commit 8c9fef3

Browse files
committed
feat(search): Add title and meta description to pages
1 parent 802953f commit 8c9fef3

File tree

8 files changed

+79
-21
lines changed

8 files changed

+79
-21
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SeoService } from './services/seo.service';
12
import { BrowserModule } from '@angular/platform-browser';
23
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
34
import { NgModule } from '@angular/core';
@@ -17,7 +18,7 @@ import { AppRoutingModule } from './app-routing.module';
1718
MatSidenavModule,
1819
AppRoutingModule
1920
],
20-
providers: [],
21+
providers: [SeoService],
2122
bootstrap: [AppComponent]
2223
})
2324
export class AppModule {}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { Component, OnInit } from "@angular/core";
1+
import { Component, OnInit } from '@angular/core';
2+
import { SeoService } from '../services/seo.service';
23

34
@Component({
4-
selector: "app-companies",
5-
templateUrl: "./companies.component.html",
6-
styleUrls: ["./companies.component.scss"]
5+
selector: 'app-companies',
6+
templateUrl: './companies.component.html',
7+
styleUrls: ['./companies.component.scss']
78
})
89
export class CompaniesComponent implements OnInit {
9-
constructor() {}
10+
constructor(private _seo: SeoService) {}
1011

11-
ngOnInit() {}
12+
ngOnInit() {
13+
this._seo.setHeaders(['Companies'], this._seo.companiesDescription);
14+
}
1215
}

src/app/operators/components/operator/operator.component.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
InjectionToken
88
} from '@angular/core';
99
import { Router, ActivatedRoute } from '@angular/router';
10+
import { SeoService } from '../../../services/seo.service';
1011
import { OperatorDoc } from '../../../../operator-docs/operator.model';
1112
import 'rxjs/add/operator/pluck';
1213

@@ -26,7 +27,8 @@ export class OperatorComponent implements OnInit {
2627
constructor(
2728
private _router: Router,
2829
private _activatedRoute: ActivatedRoute,
29-
@Inject(OPERATOR_TOKEN) public operators: OperatorDoc[]
30+
@Inject(OPERATOR_TOKEN) public operators: OperatorDoc[],
31+
private _seo: SeoService
3032
) {}
3133

3234
ngOnInit() {
@@ -35,15 +37,15 @@ export class OperatorComponent implements OnInit {
3537
this.operators.filter(
3638
(operator: OperatorDoc) => operator.name === name
3739
)[0] || this.notfound();
40+
this._seo.setHeaders(
41+
[this.operator.name, this.operator.operatorType],
42+
this.operator.shortDescription
43+
? this.operator.shortDescription.description
44+
: ''
45+
);
3846
});
3947
}
4048

41-
notfound() {
42-
console.log('not found');
43-
this._router.navigate(['/operators']);
44-
return {};
45-
}
46-
4749
get operatorName() {
4850
return this.operator.name;
4951
}
@@ -104,4 +106,9 @@ export class OperatorComponent implements OnInit {
104106
get additionalResources() {
105107
return this.operator.additionalResources || [];
106108
}
109+
110+
private notfound() {
111+
this._router.navigate(['/operators']);
112+
return {};
113+
}
107114
}

src/app/operators/operators.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { BreakpointObserver } from '@angular/cdk/layout';
1818
import { Subscription } from 'rxjs/Subscription';
1919
import { Observable } from 'rxjs/Observable';
2020
import { OperatorDoc } from '../../operator-docs/operator.model';
21+
import { SeoService } from '../services/seo.service';
2122

2223
const OPERATOR_MENU_GAP_LARGE = 64;
2324
const OPERATOR_MENU_GAP_SMALL = 54;
@@ -64,7 +65,8 @@ export class OperatorsComponent implements OnInit, AfterViewInit {
6465
private _breakpointObserver: BreakpointObserver,
6566
private _router: Router,
6667
private _activatedRoute: ActivatedRoute,
67-
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[]
68+
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[],
69+
private _seo: SeoService
6870
) {}
6971

7072
ngOnInit() {
@@ -73,6 +75,7 @@ export class OperatorsComponent implements OnInit, AfterViewInit {
7375
this._subscription = this._activatedRoute.fragment.subscribe(name =>
7476
this.scrollToOperator(name)
7577
);
78+
this._seo.setHeaders(['Operators'], this._seo.operatorsDescription);
7679
}
7780

7881
ngAfterViewInit() {

src/app/rxjs/rxjs.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { SeoService } from '../services/seo.service';
23

34
@Component({
45
selector: 'app-rxjs',
56
templateUrl: './rxjs.component.html',
67
styleUrls: ['./rxjs.component.scss']
78
})
89
export class RxjsComponent implements OnInit {
9-
10-
constructor() { }
10+
constructor(private _seo: SeoService) {}
1111

1212
ngOnInit() {
13+
this._seo.setHeaders([], this._seo.homeDescription);
1314
}
14-
1515
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { SeoService } from './seo.service';
4+
5+
describe('SeoService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [SeoService]
9+
});
10+
});
11+
12+
it(
13+
'should be created',
14+
inject([SeoService], (service: SeoService) => {
15+
expect(service).toBeTruthy();
16+
})
17+
);
18+
});

src/app/services/seo.service.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Injectable } from '@angular/core';
2+
import { Title, Meta } from '@angular/platform-browser';
3+
4+
@Injectable()
5+
export class SeoService {
6+
// This part is happended at the end of head>title
7+
private siteTitle = 'RxJS Documentation';
8+
9+
// head>meta>description for common pages
10+
public homeDescription = 'The complete RxJS documentation...';
11+
public operatorsDescription = 'All the RxJS operators...';
12+
public companiesDescription = 'Companies that use RxJS...';
13+
public teamDescription = 'People behind the RxJS Documentation project...';
14+
15+
constructor(private _title: Title, private _meta: Meta) {}
16+
17+
public setHeaders(titleParts: string[], description: string) {
18+
this._title.setTitle([...titleParts, this.siteTitle].join(' \u2022 '));
19+
if (description && description.length) {
20+
this._meta.updateTag({
21+
content: description,
22+
name: 'description'
23+
});
24+
}
25+
}
26+
}

src/app/team/team.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { SeoService } from '../services/seo.service';
23

34
@Component({
45
selector: 'app-team',
56
templateUrl: './team.component.html',
67
styleUrls: ['./team.component.scss']
78
})
89
export class TeamComponent implements OnInit {
9-
10-
constructor() { }
10+
constructor(private _seo: SeoService) {}
1111

1212
ngOnInit() {
13+
this._seo.setHeaders(['The Team'], this._seo.teamDescription);
1314
}
14-
1515
}

0 commit comments

Comments
 (0)