forked from Chalarangelo/30-seconds-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.js
More file actions
60 lines (47 loc) · 1.38 KB
/
page.js
File metadata and controls
60 lines (47 loc) · 1.38 KB
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
import PageSerializer from '#src/serializers/pageSerializer.js';
export default class Page {
static pageTypes = {};
// Registers different page types so they can be dynamically created later
static register(pageType) {
this.pageTypes[pageType.name] = pageType;
}
// Creates a page instance dynamically based on the object type
static from(object, options = {}) {
const pageName = `${object.constructor.name}Page`;
return new this.pageTypes[pageName](object, options);
}
static get home() {
return new this.pageTypes.HomePage();
}
constructor(object, options = {}) {
this.object = object;
this.options = options;
}
get serialize() {
return PageSerializer.serialize(this);
}
get key() {
return Object.values(this.params).flat().join('/');
}
get params() {
throw new Error('Not implemented');
}
get props() {
throw new Error('Not implemented');
}
get schemaData() {
const schemaData = {
'@context': 'https://schema.org',
'@type': this.object.isSnippet ? 'TechArticle' : 'ItemList',
url: this.object.fullUrl,
mainEntityOfPage: { '@type': 'WebPage', '@id': this.object.fullUrl },
};
if (this.additionalSchemaData) {
Object.assign(schemaData, this.additionalSchemaData);
}
return schemaData;
}
get slugSegments() {
return this.object.slug.slice(1).split('/');
}
}