2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-12 00:22:24 +03:00

feat: child can indicate its content should be ignored (resolves: #204)

This commit is contained in:
pimlie
2019-03-08 14:41:11 +01:00
committed by Alexander Lichter
parent 4dafffea4e
commit 22e456cbe2
2 changed files with 77 additions and 2 deletions
+23 -2
View File
@@ -1,5 +1,6 @@
import deepmerge from 'deepmerge'
import applyTemplate from './applyTemplate'
import { metaInfoAttributeKeys } from './constants'
export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, contentKeyName }, target, source) {
// we concat the arrays without merging objects contained in,
@@ -15,9 +16,11 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
}
const sourceIndex = source.findIndex(item => item[tagIDKeyName] === targetItem[tagIDKeyName])
const sourceItem = source[sourceIndex]
// source doesnt contain any duplicate id's
if (sourceIndex === -1) {
// or the source item should be ignored
if (sourceIndex === -1 || sourceItem[contentKeyName] === false || sourceItem.innerHTML === false) {
destination.push(targetItem)
return
}
@@ -29,7 +32,6 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
return
}
const sourceItem = source[sourceIndex]
const sourceTemplate = sourceItem[metaTemplateKeyName]
if (!sourceTemplate) {
@@ -45,6 +47,25 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
}
export function merge(target, source, options = {}) {
// remove properties explicitly set to false so child components can
// optionally _not_ overwrite the parents content
// (for array properties this is checked in arrayMerge)
if (source.title === false) {
delete source.title
}
for (const attrKey in metaInfoAttributeKeys) {
if (!source[attrKey]) {
continue
}
for (const key in source[attrKey]) {
if (source[attrKey][key] === false) {
delete source[attrKey][key]
}
}
}
return deepmerge(target, source, {
arrayMerge: (t, s) => arrayMerge(options, t, s)
})
+54
View File
@@ -617,4 +617,58 @@ describe('getMetaInfo', () => {
__dangerouslyDisableSanitizersByTagID: {}
})
})
test('child can indicate its content should be ignored', () => {
Vue.component('merge-child', {
render: h => h('div'),
metaInfo: {
title: false,
meta: [
{
vmid: 'og:title',
content: false
}
]
}
})
const component = new Vue({
metaInfo: {
title: 'Hello',
meta: [
{
vmid: 'og:title',
property: 'og:title',
content: 'Test title',
template: chunk => `${chunk} - My page`
}
]
},
el: document.createElement('div'),
render: h => h('div', null, [h('merge-child')])
})
expect(getMetaInfo(component)).toEqual({
title: 'Hello',
titleChunk: 'Hello',
titleTemplate: '%s',
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {},
meta: [
{
vmid: 'og:title',
property: 'og:title',
content: 'Test title - My page'
}
],
base: [],
link: [],
style: [],
script: [],
noscript: [],
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: {}
})
})
})