-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
102 lines (84 loc) · 2.97 KB
/
types.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
export interface TitleMetaLinkTag {
/** the tag for the meta information */
tag: string;
/** the inner content of the meta tag */
content?: string | null | undefined;
/** the HTML attributes to attach to the meta tag */
attributes?: Record<string, string> | null | undefined;
}
export interface SeoTitleTag {
tag: 'title';
content: string | null;
attributes?: null;
}
export interface RegularMetaAttributes {
name: string;
content: string;
}
export interface OgMetaAttributes {
property: string;
content: string;
}
export interface SeoMetaTag {
tag: 'meta';
content?: null;
attributes: RegularMetaAttributes | OgMetaAttributes;
}
export interface FaviconAttributes {
sizes: string;
type: string;
rel: string;
href: string;
}
export interface AppleTouchIconAttributes {
sizes: string;
rel: 'apple-touch-icon';
href: string;
}
export interface SeoLinkTag {
tag: 'link';
content?: null;
attributes: FaviconAttributes | AppleTouchIconAttributes;
}
export type SeoTag = SeoTitleTag | SeoMetaTag;
export type FaviconTag = SeoMetaTag | SeoLinkTag;
export type SeoOrFaviconTag = SeoTag | FaviconTag;
export const isSeoTitleTag = (tag: any): tag is SeoTitleTag =>
'tag' in tag && tag.tag === 'title' && !tag.attributes;
export const isSeoTag = (tag: any): tag is SeoTag => isSeoTitleTag(tag) || isSeoMetaTag(tag);
export const isFaviconAttributes = (tag: any): tag is FaviconAttributes =>
'sizes' in tag &&
typeof tag.sizes === 'string' &&
'type' in tag &&
typeof tag.type === 'string' &&
'rel' in tag &&
typeof tag.rel === 'string' &&
'href' in tag &&
typeof tag.href === 'string';
export const isAppleTouchIconAttributes = (tag: any): tag is AppleTouchIconAttributes =>
'sizes' in tag &&
typeof tag.sizes === 'string' &&
'rel' in tag &&
tag.rel === 'apple-touch-icon' &&
'href' in tag &&
typeof tag.href === 'string';
export const isSeoLinkTag = (tag: any): tag is SeoLinkTag =>
'tag' in tag &&
tag.tag === 'link' &&
!tag.content &&
(isFaviconAttributes(tag.attributes) || isAppleTouchIconAttributes(tag.attributes));
export const isFaviconTag = (tag: any): tag is FaviconTag => isSeoMetaTag(tag) || isSeoLinkTag(tag);
export const isSeoOrFaviconTag = (
seoOrFaviconTag: TitleMetaLinkTag | SeoOrFaviconTag,
): seoOrFaviconTag is SeoOrFaviconTag => isSeoTag(seoOrFaviconTag) || isFaviconTag(seoOrFaviconTag);
export const isRegularMetaAttributes = (
attributes: RegularMetaAttributes | OgMetaAttributes,
): attributes is RegularMetaAttributes => 'name' in attributes && 'content' in attributes;
export const isOgMetaAttributes = (
attributes: RegularMetaAttributes | OgMetaAttributes,
): attributes is OgMetaAttributes => 'property' in attributes && 'content' in attributes;
export const isSeoMetaTag = (seoMetaTag: SeoOrFaviconTag): seoMetaTag is SeoMetaTag =>
'tag' in seoMetaTag &&
seoMetaTag.tag === 'meta' &&
!seoMetaTag.content &&
(isRegularMetaAttributes(seoMetaTag.attributes) || isOgMetaAttributes(seoMetaTag.attributes));