Skip to content

Commit d9c8a61

Browse files
committed
fix: change meta tags merge function #2614
1 parent 5fcbd88 commit d9c8a61

File tree

2 files changed

+119
-6
lines changed

2 files changed

+119
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import updateMeta from '../../root-mixins/updateMeta'
2+
3+
describe('getMergedMetaTags', () => {
4+
function metaFaker (pageMeta = null, siteMeta = []) {
5+
return {
6+
$page: {
7+
frontmatter: { meta: pageMeta }
8+
},
9+
$description: '$description',
10+
siteMeta: siteMeta
11+
}
12+
}
13+
14+
test('empty', () => {
15+
const me = metaFaker()
16+
const sut = updateMeta.methods.getMergedMetaTags.bind(me)
17+
const actual = sut()
18+
expect(actual).toEqual([
19+
{ name: 'description', content: '$description' }
20+
])
21+
})
22+
23+
test('description in pageMeta, $description and siteMeta should use pageMeta', () => {
24+
const me = metaFaker([
25+
{ name: 'description', content: 'pageMeta' }
26+
], [
27+
{ name: 'description', content: 'siteMeta' }
28+
])
29+
const sut = updateMeta.methods.getMergedMetaTags.bind(me)
30+
const actual = sut()
31+
expect(actual).toEqual([
32+
{ name: 'description', content: 'pageMeta' }
33+
])
34+
})
35+
36+
test('description in $description and siteMeta should use $description', () => {
37+
const me = metaFaker(null, [
38+
{ name: 'description', content: 'siteMeta' }
39+
])
40+
const sut = updateMeta.methods.getMergedMetaTags.bind(me)
41+
const actual = sut()
42+
expect(actual).toEqual([
43+
{ name: 'description', content: '$description' }
44+
])
45+
})
46+
47+
test('allow multiple article:tag', () => {
48+
const me = metaFaker([
49+
{ property: 'article:tag', content: 'tag1' },
50+
{ property: 'article:tag', content: 'tag2' },
51+
{ property: 'article:tag', content: 'tag3' }
52+
], [])
53+
const sut = updateMeta.methods.getMergedMetaTags.bind(me)
54+
const actual = sut()
55+
expect(actual).toEqual([
56+
{ property: 'article:tag', content: 'tag1' },
57+
{ property: 'article:tag', content: 'tag2' },
58+
{ property: 'article:tag', content: 'tag3' },
59+
{ name: 'description', content: '$description' }
60+
])
61+
})
62+
63+
test('pageMeta order should not be changed', () => {
64+
const me = metaFaker([
65+
{ property: 'og:image', content: 'https://example.com/rock.jpg' },
66+
{ property: 'og:image:width', content: '300' },
67+
{ property: 'og:image:height', content: '300' },
68+
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
69+
{ property: 'og:image', content: 'https://example.com/rock3.jpg' },
70+
{ property: 'og:image:height', content: '1000' }
71+
], [])
72+
const sut = updateMeta.methods.getMergedMetaTags.bind(me)
73+
const actual = sut()
74+
expect(actual).toEqual([
75+
{ property: 'og:image', content: 'https://example.com/rock.jpg' },
76+
{ property: 'og:image:width', content: '300' },
77+
{ property: 'og:image:height', content: '300' },
78+
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
79+
{ property: 'og:image', content: 'https://example.com/rock3.jpg' },
80+
{ property: 'og:image:height', content: '1000' },
81+
{ name: 'description', content: '$description' }
82+
])
83+
})
84+
85+
test('siteMeta with same meta identifier of pageMeta should be ignore', () => {
86+
const me = metaFaker([
87+
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
88+
{ property: 'og:image:width', content: '300' },
89+
{ property: 'og:image:height', content: '300' }
90+
], [
91+
{ property: 'og:image', content: 'https://example.com/rock1.jpg' },
92+
{ property: 'og:image:width', content: '100' },
93+
{ property: 'og:image:height', content: '100' },
94+
{ property: 'og:site_name', content: 'siteMeta' }
95+
])
96+
const sut = updateMeta.methods.getMergedMetaTags.bind(me)
97+
const actual = sut()
98+
expect(actual).toEqual([
99+
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
100+
{ property: 'og:image:width', content: '300' },
101+
{ property: 'og:image:height', content: '300' },
102+
{ name: 'description', content: '$description' },
103+
{ property: 'og:site_name', content: 'siteMeta' }
104+
])
105+
})
106+
})

packages/@vuepress/core/lib/client/root-mixins/updateMeta.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unionBy from 'lodash/unionBy'
1+
import transform from 'lodash/transform'
22

33
export default {
44
// created will be called on both client and ssr
@@ -34,11 +34,18 @@ export default {
3434
},
3535

3636
getMergedMetaTags () {
37-
const pageMeta = this.$page.frontmatter.meta || []
38-
// pageMetaTags have higher priority than siteMetaTags
39-
// description needs special attention as it has too many entries
40-
return unionBy([{ name: 'description', content: this.$description }],
41-
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+
}, [])
4249
}
4350
},
4451

0 commit comments

Comments
 (0)