2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-15 01:02:25 +03:00

feat: append tag into body

This commit is contained in:
Clark Du
2017-11-02 20:53:25 +08:00
parent 1813a8e2d0
commit 8602d5060c
3 changed files with 15 additions and 6 deletions
+3 -1
View File
@@ -42,7 +42,9 @@ export default function _updateClientMetaInfo (options = {}) {
break
// catch-all update tags
default:
const { oldTags, newTags } = updateTags(options)(key, newInfo[key], document.getElementsByTagName('head')[0])
const headTag = document.getElementsByTagName('head')[0]
const bodyTag = document.getElementsByTagName('body')[0]
const { oldTags, newTags } = updateTags(options)(key, newInfo[key], headTag, bodyTag)
if (newTags.length) {
addedTags[key] = newTags
removedTags[key] = oldTags
+10 -4
View File
@@ -5,15 +5,15 @@ export default function _updateTags (options = {}) {
const { attribute } = options
/**
* Updates meta tags inside <head> on the client. Borrowed from `react-helmet`:
* Updates meta tags inside <head> and <body> on the client. Borrowed from `react-helmet`:
* https://github.com/nfl/react-helmet/blob/004d448f8de5f823d10f838b02317521180f34da/src/Helmet.js#L195-L245
*
* @param {('meta'|'base'|'link'|'style'|'script'|'noscript')} type - 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} - a representation of what tags changed
*/
return function updateTags (type, tags, headTag) {
const nodes = headTag.querySelectorAll(`${type}[${attribute}]`)
return function updateTags (type, tags, headTag, bodyTag) {
const nodes = document.querySelectorAll(`${type}[${attribute}]`)
const oldTags = toArray(nodes)
const newTags = []
let indexToDelete
@@ -72,7 +72,13 @@ export default function _updateTags (options = {}) {
}
oldTags.forEach((tag) => tag.parentNode.removeChild(tag))
newTags.forEach((tag) => headTag.appendChild(tag))
newTags.forEach((tag) => {
if (tag.inBody === true) {
bodyTag.appendChild(tag)
} else {
headTag.appendChild(tag)
}
})
return { oldTags, newTags }
}
+2 -1
View File
@@ -10,9 +10,10 @@ export default function _tagGenerator (options = {}) {
*/
return function tagGenerator (type, tags) {
return {
text () {
text ({inBody = false} = {}) {
// build a string containing all tags of this type
return tags.reduce((tagsStr, tag) => {
if (!!tag.inBody !== inBody) return tagsStr
// build a string containing all attributes of this tag
const attrs = Object.keys(tag).reduce((attrsStr, attr) => {
switch (attr) {