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

feat: better template support

This commit is contained in:
Alexander Lichter
2018-08-19 17:26:32 +01:00
parent 959f1a7774
commit e64c6076b6
6 changed files with 4652 additions and 3956 deletions
+15 -6
View File
@@ -20,7 +20,7 @@ const escapeHTML = (str) => typeof window === 'undefined'
.replace(/'/g, '\u0027')
export default function _getMetaInfo (options = {}) {
const { keyName, tagIDKeyName, metaTemplateKeyName } = options
const { keyName, tagIDKeyName, metaTemplateKeyName, contentKeyName } = options
/**
* Returns the correct meta info for the given component
* (child components will overwrite parent meta info)
@@ -53,6 +53,7 @@ export default function _getMetaInfo (options = {}) {
option: keyName,
deep: true,
metaTemplateKeyName,
contentKeyName,
arrayMerge (target, source) {
// we concat the arrays without merging objects contained in,
// but we check for a `vmid` property on each object in the array
@@ -66,10 +67,17 @@ export default function _getMetaInfo (options = {}) {
for (let sourceIndex in source) {
const sourceItem = source[sourceIndex]
if (targetItem[tagIDKeyName] && targetItem[tagIDKeyName] === sourceItem[tagIDKeyName]) {
const targetTemplate = targetItem[metaTemplateKeyName]
const sourceTemplate = sourceItem[metaTemplateKeyName]
if (targetTemplate && !sourceTemplate) {
sourceItem[contentKeyName] = applyTemplate(component)(targetTemplate)(sourceItem[contentKeyName])
delete sourceItem[metaTemplateKeyName]
}
shared = true
break
}
}
if (!shared) {
destination.push(targetItem)
}
@@ -79,6 +87,8 @@ export default function _getMetaInfo (options = {}) {
}
})
// Remove all "template" tags from meta
// backup the title chunk in case user wants access to it
if (info.title) {
info.titleChunk = info.title
@@ -86,11 +96,7 @@ export default function _getMetaInfo (options = {}) {
// replace title with populated template
if (info.titleTemplate) {
if (typeof info.titleTemplate === 'function') {
info.title = info.titleTemplate.call(component, info.titleChunk)
} else {
info.title = info.titleTemplate.replace(/%s/g, info.titleChunk)
}
info.title = applyTemplate(component)(info.titleTemplate)(info.titleChunk)
}
// convert base tag to an array so it can be handled the same way
@@ -140,3 +146,6 @@ export default function _getMetaInfo (options = {}) {
return info
}
}
const applyTemplate = component => template => chunk =>
typeof template === 'function' ? template.call(component, chunk) : template.replace(/%s/g, chunk)