-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta.js
49 lines (45 loc) · 1.24 KB
/
meta.js
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
'use strict'
const dom = require('@nx-js/dom-util')
const secret = {
config: Symbol('meta config')
}
module.exports = function metaFactory (config) {
function meta (elem) {
const parentConfig = dom.findAncestorProp(elem, secret.config)
config = elem[secret.config] = Object.assign({}, parentConfig, config)
if (config.title) {
document.title = config.title
}
if (config.description) {
setMetaTag('description', config.description)
}
if (config.author) {
setMetaTag('author', config.author)
}
if (config.keywords) {
setMetaTag('keywords', config.keywords)
}
if (config.robots) {
setMetaTag('robots', config.robots)
}
if (config.analytics) {
if (typeof ga !== 'function') {
throw new Error('There is no global ga (Google analytics) function.')
}
ga('set', 'page', config.analytics)
ga('send', 'pageview')
}
}
meta.$name = 'meta'
meta.$type = 'component'
return meta
}
function setMetaTag (name, content) {
let tag = document.querySelector(`meta[name="${name}"]`)
if (!tag) {
tag = document.createElement('meta')
tag.setAttribute('name', name)
document.head.appendChild(tag)
}
tag.setAttribute('content', content)
}