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

extract generators to their own modules

This commit is contained in:
Declan de Wet
2016-11-02 22:59:13 +02:00
parent a7a987e858
commit a3c17f1c14
3 changed files with 49 additions and 18 deletions
+29
View File
@@ -0,0 +1,29 @@
import { VUE_META_ATTRIBUTE } from './constants'
/**
* Generates tag attributes for use on the server.
*
* @param {('bodyAttrs'|'htmlAttrs')} type - the type of attributes to generate
* @param {Object} data - the attributes to generate
* @return {Object} - the attribute generator
*/
export default function attrsGenerator (type, data) {
return {
text () {
let attributeStr = ''
let watchedAttrs = []
for (let attr in data) {
if (data.hasOwnProperty(attr)) {
watchedAttrs.push(attr)
attributeStr += `${
typeof data[attr] !== 'undefined'
? `${attr}="${data[attr]}"`
: attr
} `
}
}
attributeStr += `${VUE_META_ATTRIBUTE}="${watchedAttrs.join(',')}"`
return attributeStr.trim()
}
}
}
+4 -18
View File
@@ -1,4 +1,5 @@
import { VUE_META_ATTRIBUTE } from './constants'
import titleGenerator from './titleGenerator'
import attrsGenerator from './attrsGenerator'
/**
* Converts a meta info property to one that can be stringified on the server
@@ -10,24 +11,9 @@ import { VUE_META_ATTRIBUTE } from './constants'
export default function generateServerInjector (type, data) {
switch (type) {
case 'title':
return {
text: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}</${type}>`
}
return titleGenerator(type, data)
case 'htmlAttrs':
case 'bodyAttrs':
return {
text () {
let attributeStr = ''
let watchedAttrs = []
for (let attr in data) {
if (data.hasOwnProperty(attr)) {
watchedAttrs.push(attr)
attributeStr += `${typeof data[attr] !== 'undefined' ? `${attr}="${data[attr]}"` : attr} `
}
}
attributeStr += `${VUE_META_ATTRIBUTE}="${watchedAttrs.join(',')}"`
return attributeStr.trim()
}
}
return attrsGenerator(type, data)
}
}
+16
View File
@@ -0,0 +1,16 @@
import { VUE_META_ATTRIBUTE } from './constants'
/**
* Generates title output for the server
*
* @param {'title'} type - the string "title"
* @param {String} data - the title text
* @return {Object} - the title generator
*/
export default function titleGenerator (type, data) {
return {
text () {
return `<${type} ${VUE_META_ATTRIBUTE}="true">${data}</${type}>`
}
}
}