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:
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}>`
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user