Skip to content

fix($core): Change meta merge function #2614 #2615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import updateMeta from '../../root-mixins/updateMeta'

describe('getMergedMetaTags', () => {
function metaFaker (pageMeta = null, siteMeta = []) {
return {
$page: {
frontmatter: { meta: pageMeta }
},
$description: '$description',
siteMeta: siteMeta
}
}

test('empty', () => {
const me = metaFaker()
const actual = updateMeta.methods.getMergedMetaTags.call(me)
expect(actual).toEqual([
{ name: 'description', content: '$description' }
])
})

test('description in pageMeta, $description and siteMeta should use pageMeta', () => {
const me = metaFaker([
{ name: 'description', content: 'pageMeta' }
], [
{ name: 'description', content: 'siteMeta' }
])
const actual = updateMeta.methods.getMergedMetaTags.call(me)
expect(actual).toEqual([
{ name: 'description', content: 'pageMeta' }
])
})

test('description in $description and siteMeta should use $description', () => {
const me = metaFaker(null, [
{ name: 'description', content: 'siteMeta' }
])
const actual = updateMeta.methods.getMergedMetaTags.call(me)
expect(actual).toEqual([
{ name: 'description', content: '$description' }
])
})

test('allow multiple article:tag', () => {
const me = metaFaker([
{ property: 'article:tag', content: 'tag1' },
{ property: 'article:tag', content: 'tag2' },
{ property: 'article:tag', content: 'tag3' }
], [])
const actual = updateMeta.methods.getMergedMetaTags.call(me)
expect(actual).toEqual([
{ property: 'article:tag', content: 'tag1' },
{ property: 'article:tag', content: 'tag2' },
{ property: 'article:tag', content: 'tag3' },
{ name: 'description', content: '$description' }
])
})

test('pageMeta order should not be changed', () => {
const me = metaFaker([
{ property: 'og:image', content: 'https://example.com/rock.jpg' },
{ property: 'og:image:width', content: '300' },
{ property: 'og:image:height', content: '300' },
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
{ property: 'og:image', content: 'https://example.com/rock3.jpg' },
{ property: 'og:image:height', content: '1000' }
], [])
const actual = updateMeta.methods.getMergedMetaTags.call(me)
expect(actual).toEqual([
{ property: 'og:image', content: 'https://example.com/rock.jpg' },
{ property: 'og:image:width', content: '300' },
{ property: 'og:image:height', content: '300' },
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
{ property: 'og:image', content: 'https://example.com/rock3.jpg' },
{ property: 'og:image:height', content: '1000' },
{ name: 'description', content: '$description' }
])
})

test('siteMeta with same meta identifier of pageMeta should be ignore', () => {
const me = metaFaker([
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
{ property: 'og:image:width', content: '300' },
{ property: 'og:image:height', content: '300' }
], [
{ property: 'og:image', content: 'https://example.com/rock1.jpg' },
{ property: 'og:image:width', content: '100' },
{ property: 'og:image:height', content: '100' },
{ property: 'og:site_name', content: 'siteMeta' }
])
const actual = updateMeta.methods.getMergedMetaTags.call(me)
expect(actual).toEqual([
{ property: 'og:image', content: 'https://example.com/rock2.jpg' },
{ property: 'og:image:width', content: '300' },
{ property: 'og:image:height', content: '300' },
{ name: 'description', content: '$description' },
{ property: 'og:site_name', content: 'siteMeta' }
])
})
})
19 changes: 13 additions & 6 deletions packages/@vuepress/core/lib/client/root-mixins/updateMeta.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import unionBy from 'lodash/unionBy'
import transform from 'lodash/transform'

export default {
// created will be called on both client and ssr
Expand Down Expand Up @@ -34,11 +34,18 @@ export default {
},

getMergedMetaTags () {
const pageMeta = this.$page.frontmatter.meta || []
// pageMetaTags have higher priority than siteMetaTags
// description needs special attention as it has too many entries
return unionBy([{ name: 'description', content: this.$description }],
pageMeta, this.siteMeta, metaIdentifier)
const exists = {}
return transform([
this.$page.frontmatter.meta || [], // page meta
[{ name: 'description', content: this.$description }], // meta description
this.siteMeta // site meta
], (merged, meta) => {
const filtered = meta.filter(tag => !exists[metaIdentifier(tag)])
merged.push(...filtered)
filtered.forEach(tag => {
exists[metaIdentifier(tag)] = 1
})
}, [])
}
},

Expand Down