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 | 1×
1×
1×
1×
161×
161×
286×
215×
215×
215×
215×
38×
13×
38×
13×
38×
13×
38×
15×
215×
215×
7×
208×
11×
11×
21×
11×
10×
197×
44×
153×
48×
105×
34×
215×
215×
21×
194×
215×
215×
165×
7×
165×
144×
165×
215×
158×
57×
31×
| import { Injectable } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { Observable, Subject } from 'rxjs';
import { ConfigSeoModel, PageBaseModel } from '../models';
/**
* JsonLD Service
*/
@Injectable()
export class JsonLDService {
/** json-LD */
private readonly jsonLD$ = new Subject<SafeHtml>();
/**
* constructor of JsonLDService
* @param sanitizer: DomSanitizer
*/
constructor(private readonly sanitizer: DomSanitizer) {
}
/**
* 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
* @param configSEO: current config for seo
*/
getJsonLDByContentOfCurrentPage(tempPage: any, configSEO: ConfigSeoModel): Array<any> {
const jsonLDs = [];
const images = [];
const image = tempPage.image ? tempPage.image :
configSEO.defaultImageForSEO ? 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 = configSEO.defaultPublisher ? 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
* @param configSEO: current config for seo
*/
setJsonLD(tempPage: PageBaseModel, configSEO: ConfigSeoModel): void {
if (tempPage.hasOwnProperty('jsonLDs')) {
tempPage.jsonLDs.push(...this.getJsonLDByContentOfCurrentPage(tempPage, configSEO));
} else {
tempPage.jsonLDs = this.getJsonLDByContentOfCurrentPage(tempPage, configSEO);
}
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);
}
}
/**
* get json-LD
*/
getJsonLD(): Observable<SafeHtml> {
return this.jsonLD$.asObservable();
}
}
|