-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.js
More file actions
116 lines (102 loc) · 3.09 KB
/
meta.js
File metadata and controls
116 lines (102 loc) · 3.09 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
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
/**
* @copyright 2023 Chris Zuber <admin@kernvalley.us>
*/
export function meta({ name, itemprop, property, content, parent = document.head, replace = true }) {
const created = document.createElement('meta');
let existing;
if (typeof name === 'string') {
existing = parent.querySelector(`meta[name="${CSS.escape(name)}"]`);
created.name = name;
} else if (typeof itemprop === 'string') {
existing = parent.querySelector(`meta[itemprop="${CSS.escape(itemprop)}"]`);
created.setAttribute('itemprop', itemprop);
} else if (typeof property === 'string') {
existing = parent.querySelector(`meta[property="${CSS.escape(property)}"]`);
created.setAttribute('property', property);
}
if (Array.isArray(content)) {
created.content = content.join(', ');
} else if (typeof content === 'string' || typeof content === 'number') {
created.content = content;
} else if (content instanceof URL) {
created.content = content.href;
} else if (typeof content === 'object') {
created.content = Object.entries(content).map(([k, v]) => `${k}=${v}`).join(',');
}
if (! (parent instanceof Element)) {
return created;
} else if (existing instanceof HTMLMetaElement && replace === true) {
existing.replaceWith(created);
return created;
} else {
parent.append(created);
return created;
}
}
export function description(content) {
return [
meta({ name: 'description', content }),
meta({ itemprop: 'description', content }),
meta({ property: 'og:description', content }),
meta({ name: 'twitter:description', content }),
];
}
export function keywords(content) {
return [
meta({ name: 'keywords', content }),
meta({ itemprop: 'keywords', content }),
meta({ property: 'og:keywords', content }),
];
}
export function referrer(content) {
return meta({ name: 'referrer', content });
}
export function robots(content) {
return meta({ name: 'robots', content });
}
export function thumbnail(content) {
return [
meta({ itemprop: 'thumbnailUrl', content }),
meta({ property: 'og:image', content}),
meta({ name: 'twitter:image', content }),
];
}
export function title(content) {
document.title = content;
return [
meta({ itemprop: 'headline', content }),
meta({ prooperty: 'og:title', content }),
];
}
export function getDescription(base = document.head) {
const found = base.querySelector([
'meta[name="description"][content]',
'meta[itemprop="description"][content]',
'meta[property="og:description"][content]',
'meta[name="twitter:description"][content]',
].join(','));
if (found instanceof HTMLMetaElement) {
return found.content;
} else {
return null;
}
}
export function getKeywords(base = document.head) {
const found = base.querySelector([
'meta[name="keywords"][content]',
'meta[itemprop="keywords"][content]',
'meta[property="og:keywords"][content]',
].join(','));
if (found instanceof HTMLMetaElement) {
return found.content.split(',').map(str => str.trim());
} else {
return [];
}
}
export function getPageInfo() {
return {
title: document.title,
description: getDescription(),
keywords: getKeywords(),
};
}