-
Notifications
You must be signed in to change notification settings - Fork 3
/
MetaData.js
96 lines (82 loc) · 2.18 KB
/
MetaData.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
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
import React from 'react'
import PropTypes from 'prop-types'
import Helmet from 'react-helmet'
import defaultMeta from '../../data/meta-data'
import config from '../../data/SiteConfig'
import normalisePath from '../utils/normalise-path'
const urlCannonical = config.pathPrefix === '/' ? '' : config.pathPrefix
const generateTitle = titleParts => {
if (!titleParts.length) {
return defaultMeta.title
}
return titleParts.concat(defaultMeta.titleSuffix).join(' - ')
}
const generateMetaTags = (title, description, image, path) =>
defaultMeta.metaTags.map(item => {
// Override title
if (item.name === 'twitter:title' || item.property === 'og:title') {
return {
...item,
content: title
}
}
// Override description tags
if (
description &&
(['description', 'twitter:description'].includes(item.name) ||
item.property === 'og:description')
) {
return {
...item,
content: description
}
}
// Override image
if (
image &&
(item.name === 'twitter:image' || item.property === 'og:image')
) {
const content = image.match(/(http(s)?):\/\//)
? image
: [config.siteUrl, normalisePath(image)].join(urlCannonical)
return {
...item,
content
}
}
// Override url
if (path && item.property === 'og:url') {
return {
...item,
content: [config.siteUrl, normalisePath(path)].join(urlCannonical)
}
}
return item
})
const MetaData = ({ titleParts = [], description, image, path, children }) => {
const title = generateTitle(titleParts)
const metaTags = generateMetaTags(title, description, image, path).filter(
item => {
if (!path && item.property === 'og:url') {
return false
}
return true
}
)
return (
<Helmet title={title}>
<html lang="en" />
{metaTags.map((props, i) => (
<meta key={i} {...props} />
))}
{children}
</Helmet>
)
}
MetaData.propTypes = {
titleParts: PropTypes.arrayOf(PropTypes.string),
description: PropTypes.string,
image: PropTypes.string,
path: PropTypes.string
}
export default MetaData