/
MetaManager.ts
198 lines (182 loc) · 5.64 KB
/
MetaManager.ts
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
export type MetaValue = number | boolean | string | null | undefined;
export type MetaAttributes = Record<string, MetaValue>;
export type MetaManagerRecordKeys = 'content' | 'href';
export type MetaManagerRecord<K extends MetaManagerRecordKeys> = {
[key in K]: MetaValue;
} & MetaAttributes;
/**
* The Meta manager is a utility for managing various page attributes related
* to the SEO (search engine optimization) and social network integration.
*
* The Meta manager is used to manage the following:
* - page title, set using the contents of the `<title>` element
* - page links, linking related documents and meta-information, added to the
* using `<link>` elements
* - page meta information:
* - the generic named meta information added to the page via
* `<meta>} elements with the `name` attribute, for
* example the `keywords`.
* - specialized meta information added to the page via `<meta>`
* elements with the `property` attribute, for example the OG meta
* tags (`og:type`, `og:image`, etc.).
*/
export abstract class MetaManager {
/**
* Sets the page title.
*
* @param title The new page title.
*/
setTitle(title: string): this {
return this;
}
/**
* Returns the page title. The method returns an empty string if no page
* title has been set yet.
*
* Note that the page title is cached internally by the meta manager and
* may therefore differ from the current document title if it has been
* modified by a 3rd party code.
*
* @return The current page title.
*/
getTitle(): string {
return '';
}
/**
* Set the specified named meta information property.
*
* @param name Meta information property name, for example
* `keywords`.
* @param content The meta information content.
* @parram attr Additional optional meta attributes.
*/
setMetaName(name: string, content: MetaValue, attr?: MetaAttributes): this {
return this;
}
/**
* Returns the value of the specified named meta information property. The
* method returns an empty string for missing meta information (to make the
* returned value React-friendly).
*
* @param name The name of the named meta information property.
* @return The value of the generic meta information, or an empty string.
*/
getMetaName(name: string): MetaManagerRecord<'content'> {
return { content: '' };
}
/**
* Returns the names of the currently specified named meta information
* properties.
*
* @return The names of the currently specified named meta
* information properties.
*/
getMetaNames(): string[] {
return [];
}
/**
* Return [key, value] pairs of named meta information.
*
* @return [key, value] pairs of named meta information.
*/
getMetaNamesIterator():
| IterableIterator<[string, MetaManagerRecord<'content'>]>
| never[] {
return [];
}
/**
* Sets the specified specialized meta information property.
*
* @param name Name of the specialized meta information property.
* @param property The value of the meta information property.
* @parram attr Additional optional meta attributes.
*/
setMetaProperty(
property: string,
content: MetaValue,
attr?: MetaAttributes
): this {
return this;
}
/**
* Returns the value of the specified specialized meta information
* property. The method returns an empty string for missing meta
* information (to make the returned value React-friendly).
*
* @param name The name of the specialized meta information
* property.
* @return The value of the specified meta information, or an
* empty string.
*/
getMetaProperty(property: string): MetaManagerRecord<'content'> {
return { content: '' };
}
/**
* Returns the names of the currently specified specialized meta
* information properties.
*
* @return The names of the currently specified specialized meta
* information properties.
*/
getMetaProperties(): string[] {
return [];
}
/**
* Return [key, value] pairs of meta information properties.
*
* @return [key, value] pairs of meta information properties.
*/
getMetaPropertiesIterator():
| IterableIterator<[string, MetaManagerRecord<'content'>]>
| never[] {
return [];
}
/**
* Sets the specified specialized link information.
*
* @param relation The relation of the link target to the current
* page.
* @param href The reference to the location of the related
* document, e.g. a URL.
* @parram attr Additional optional link attributes.
*/
setLink(relation: string, href: MetaValue, attr?: MetaAttributes): this {
return this;
}
/**
* Return the reference to the specified related linked document. The
* method returns an empty string for missing meta information (to make the
* returned value React-friendly).
*
* @param relation The relation of the link target to the current
* page.
* @return The reference to the location of the related document,
* e.g. a URL.
*/
getLink(relation: string): MetaManagerRecord<'href'> {
return { href: '' };
}
/**
* Returns the relations of the currently set related documents linked to
* the current page.
*/
getLinks(): string[] {
return [];
}
/**
* Return [key, value] pairs of currently set links.
*
* @return [key, value] pairs of currently set links.
*/
getLinksIterator():
| IterableIterator<[string, MetaManagerRecord<'href'>]>
| never[] {
return [];
}
/**
* Resets the stored meta names, properties and links.
*/
clearMetaAttributes(): void {
return;
}
}