mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-09 05:42:25 +03:00
support custom options
This commit is contained in:
@@ -2,21 +2,23 @@ import titleGenerator from './generators/titleGenerator'
|
||||
import attrsGenerator from './generators/attrsGenerator'
|
||||
import tagGenerator from './generators/tagGenerator'
|
||||
|
||||
/**
|
||||
* Converts a meta info property to one that can be stringified on the server
|
||||
*
|
||||
* @param {String} type - the type of data to convert
|
||||
* @param {(String|Object|Array<Object>)} data - the data value
|
||||
* @return {Object} - the new injector
|
||||
*/
|
||||
export default function generateServerInjector (type, data) {
|
||||
switch (type) {
|
||||
case 'title':
|
||||
return titleGenerator(type, data)
|
||||
case 'htmlAttrs':
|
||||
case 'bodyAttrs':
|
||||
return attrsGenerator(type, data)
|
||||
default:
|
||||
return tagGenerator(type, data)
|
||||
export default function _generateServerInjector (options) {
|
||||
/**
|
||||
* Converts a meta info property to one that can be stringified on the server
|
||||
*
|
||||
* @param {String} type - the type of data to convert
|
||||
* @param {(String|Object|Array<Object>)} data - the data value
|
||||
* @return {Object} - the new injector
|
||||
*/
|
||||
return function generateServerInjector (type, data) {
|
||||
switch (type) {
|
||||
case 'title':
|
||||
return titleGenerator(options)(type, data)
|
||||
case 'htmlAttrs':
|
||||
case 'bodyAttrs':
|
||||
return attrsGenerator(options)(type, data)
|
||||
default:
|
||||
return tagGenerator(options)(type, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
import { VUE_META_ATTRIBUTE } from '../../shared/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
|
||||
} `
|
||||
export default function _attrsGenerator ({ attribute }) {
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
return 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 += `${attribute}="${watchedAttrs.join(',')}"`
|
||||
return attributeStr.trim()
|
||||
}
|
||||
attributeStr += `${VUE_META_ATTRIBUTE}="${watchedAttrs.join(',')}"`
|
||||
return attributeStr.trim()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
import { VUE_META_ATTRIBUTE } from '../../shared/constants'
|
||||
export default function _tagGenerator ({ attribute }) {
|
||||
/**
|
||||
* Generates meta, base, link, style, script, noscript tags for use on the server
|
||||
*
|
||||
* @param {('meta'|'base'|'link'|'style'|'script'|'noscript')} the name of the tag
|
||||
* @param {(Array<Object>|Object)} tags - an array of tag objects or a single object in case of base
|
||||
* @return {Object} - the tag generator
|
||||
*/
|
||||
return function tagGenerator (type, tags) {
|
||||
return {
|
||||
text () {
|
||||
// build a string containing all tags of this type
|
||||
return tags.reduce((tagsStr, tag) => {
|
||||
// build a string containing all attributes of this tag
|
||||
const attrs = Object.keys(tag).reduce((attrsStr, attr) => {
|
||||
switch (attr) {
|
||||
// these attributes are treated as children on the tag
|
||||
case 'innerHTML':
|
||||
case 'cssText':
|
||||
return attrsStr
|
||||
|
||||
/**
|
||||
* Generates meta, base, link, style, script, noscript tags for use on the server
|
||||
*
|
||||
* @param {('meta'|'base'|'link'|'style'|'script'|'noscript')} the name of the tag
|
||||
* @param {(Array<Object>|Object)} tags - an array of tag objects or a single object in case of base
|
||||
* @return {Object} - the tag generator
|
||||
*/
|
||||
export default function tagGenerator (type, tags) {
|
||||
return {
|
||||
text () {
|
||||
// build a string containing all tags of this type
|
||||
return tags.reduce((tagsStr, tag) => {
|
||||
// build a string containing all attributes of this tag
|
||||
const attrs = Object.keys(tag).reduce((attrsStr, attr) => {
|
||||
switch (attr) {
|
||||
// these attributes are treated as children on the tag
|
||||
case 'innerHTML':
|
||||
case 'cssText':
|
||||
return attrsStr
|
||||
// these form the attribute list for this tag
|
||||
default:
|
||||
return typeof tag[attr] === 'undefined'
|
||||
? `${attrsStr} ${attr}`
|
||||
: `${attrsStr} ${attr}="${tag[attr]}"`
|
||||
}
|
||||
}, '').trim()
|
||||
|
||||
// these form the attribute list for this tag
|
||||
default:
|
||||
return typeof tag[attr] === 'undefined'
|
||||
? `${attrsStr} ${attr}`
|
||||
: `${attrsStr} ${attr}="${tag[attr]}"`
|
||||
}
|
||||
}, '').trim()
|
||||
// grab child content from one of these attributes, if possible
|
||||
const content = tag.innerHTML || tag.cssText || ''
|
||||
|
||||
// grab child content from one of these attributes, if possible
|
||||
const content = tag.innerHTML || tag.cssText || ''
|
||||
// these tag types will have content inserted
|
||||
const closed = ['noscript', 'script', 'style'].indexOf(type) === -1
|
||||
|
||||
// these tag types will have content inserted
|
||||
const closed = ['noscript', 'script', 'style'].indexOf(type) === -1
|
||||
|
||||
// the final string for this specific tag
|
||||
return closed
|
||||
? `${tagsStr}<${type} ${VUE_META_ATTRIBUTE}="true" ${attrs}/>`
|
||||
: `${tagsStr}<${type} ${VUE_META_ATTRIBUTE}="true" ${attrs}>${content}</${type}>`
|
||||
}, '')
|
||||
// the final string for this specific tag
|
||||
return closed
|
||||
? `${tagsStr}<${type} ${attribute}="true" ${attrs}/>`
|
||||
: `${tagsStr}<${type} ${attribute}="true" ${attrs}>${content}</${type}>`
|
||||
}, '')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { VUE_META_ATTRIBUTE } from '../../shared/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}>`
|
||||
export default function _titleGenerator ({ attribute }) {
|
||||
/**
|
||||
* Generates title output for the server
|
||||
*
|
||||
* @param {'title'} type - the string "title"
|
||||
* @param {String} data - the title text
|
||||
* @return {Object} - the title generator
|
||||
*/
|
||||
return function titleGenerator (type, data) {
|
||||
return {
|
||||
text () {
|
||||
return `<${type} ${attribute}="true">${data}</${type}>`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-16
@@ -1,23 +1,25 @@
|
||||
import getMetaInfo from '../shared/getMetaInfo'
|
||||
import generateServerInjector from './generateServerInjector'
|
||||
|
||||
/**
|
||||
* Converts the state of the meta info object such that each item
|
||||
* can be compiled to a tag string on the server
|
||||
*
|
||||
* @this {Object} - Vue instance - ideally the root component
|
||||
* @return {Object} - server meta info with `toString` methods
|
||||
*/
|
||||
export default function inject () {
|
||||
// get meta info with sensible defaults
|
||||
const info = getMetaInfo(this.$root)
|
||||
export default function _inject (options) {
|
||||
/**
|
||||
* Converts the state of the meta info object such that each item
|
||||
* can be compiled to a tag string on the server
|
||||
*
|
||||
* @this {Object} - Vue instance - ideally the root component
|
||||
* @return {Object} - server meta info with `toString` methods
|
||||
*/
|
||||
return function inject () {
|
||||
// get meta info with sensible defaults
|
||||
const info = getMetaInfo(options)(this.$root)
|
||||
|
||||
// generate server injectors
|
||||
for (let key in info) {
|
||||
if (info.hasOwnProperty(key) && key !== 'titleTemplate') {
|
||||
info[key] = generateServerInjector(key, info[key])
|
||||
// generate server injectors
|
||||
for (let key in info) {
|
||||
if (info.hasOwnProperty(key) && key !== 'titleTemplate') {
|
||||
info[key] = generateServerInjector(options)(key, info[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
return info
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user