|
1 |
| -import unionBy from 'lodash/unionBy' |
2 |
| - |
3 |
| -// @see https://ogp.me/ |
4 |
| -const META_TYPE_ARRAY = [ |
5 |
| - 'article:author', |
6 |
| - 'article:tag', |
7 |
| - 'book:author', |
8 |
| - 'book:tag', |
9 |
| - 'music:album', |
10 |
| - 'music:musician', |
11 |
| - 'og:locale:alternate', |
12 |
| - 'video:actor', |
13 |
| - 'video:director', |
14 |
| - 'video:tag', |
15 |
| - 'video:writer' |
16 |
| -] |
| 1 | +import transform from 'lodash/transform' |
17 | 2 |
|
18 | 3 | export default {
|
19 | 4 | // created will be called on both client and ssr
|
@@ -49,11 +34,18 @@ export default {
|
49 | 34 | },
|
50 | 35 |
|
51 | 36 | getMergedMetaTags () {
|
52 |
| - const pageMeta = this.$page.frontmatter.meta || [] |
53 |
| - // pageMetaTags have higher priority than siteMetaTags |
54 |
| - // description needs special attention as it has too many entries |
55 |
| - return unionBy([{ name: 'description', content: this.$description }], |
56 |
| - pageMeta, this.siteMeta, metaIdentifier) |
| 37 | + const exists = {} |
| 38 | + return transform([ |
| 39 | + this.$page.frontmatter.meta || [], // page meta |
| 40 | + [{ name: 'description', content: this.$description }], // meta description |
| 41 | + this.siteMeta // site meta |
| 42 | + ], (merged, meta) => { |
| 43 | + const filtered = meta.filter(tag => !exists[metaIdentifier(tag)]) |
| 44 | + merged.push(...filtered) |
| 45 | + filtered.forEach(tag => { |
| 46 | + exists[metaIdentifier(tag)] = 1 |
| 47 | + }) |
| 48 | + }, []) |
57 | 49 | }
|
58 | 50 | },
|
59 | 51 |
|
@@ -101,11 +93,7 @@ function updateMetaTags (newMetaTags, currentMetaTags) {
|
101 | 93 | */
|
102 | 94 | function metaIdentifier (tag) {
|
103 | 95 | for (const item of ['name', 'property', 'itemprop']) {
|
104 |
| - if (tag.hasOwnProperty(item)) { |
105 |
| - const key = tag[item] |
106 |
| - const value = tag.hasOwnProperty('content') && ~META_TYPE_ARRAY.indexOf(key) ? tag.content : '' |
107 |
| - return key + item + value |
108 |
| - } |
| 96 | + if (tag.hasOwnProperty(item)) return tag[item] + item |
109 | 97 | }
|
110 | 98 | return JSON.stringify(tag)
|
111 | 99 | }
|
|
0 commit comments