|
|
@ -1,11 +1,20 @@
|
|
|
|
import { watchEffect } from 'vue'
|
|
|
|
import { watchEffect } from 'vue'
|
|
|
|
import { useSiteData } from './siteData'
|
|
|
|
import { siteDataRef } from './siteData'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {import('./pageData').PageDataRef} pageData
|
|
|
|
* @param {import('./pageData').PageDataRef} pageData
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function useUpdateHead(pageData) {
|
|
|
|
export function useUpdateHead(pageData) {
|
|
|
|
const siteData = useSiteData()
|
|
|
|
const descriptionTag = createHeadElement(['meta', {
|
|
|
|
|
|
|
|
name: 'description',
|
|
|
|
|
|
|
|
content: siteDataRef.value.description
|
|
|
|
|
|
|
|
}])
|
|
|
|
|
|
|
|
document.head.appendChild(descriptionTag)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const updateTitleAndDescription = () => {
|
|
|
|
|
|
|
|
document.title = siteDataRef.value.title
|
|
|
|
|
|
|
|
descriptionTag.setAttribute('content', siteDataRef.value.description)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @type {HTMLElement[]}
|
|
|
|
* @type {HTMLElement[]}
|
|
|
@ -24,14 +33,8 @@ export function useUpdateHead(pageData) {
|
|
|
|
tags.forEach((el) => document.head.removeChild(el))
|
|
|
|
tags.forEach((el) => document.head.removeChild(el))
|
|
|
|
tags.length = 0
|
|
|
|
tags.length = 0
|
|
|
|
if (newTags && newTags.length) {
|
|
|
|
if (newTags && newTags.length) {
|
|
|
|
newTags.forEach(([tag, attrs, innerHTML]) => {
|
|
|
|
newTags.forEach((headConfig) => {
|
|
|
|
const el = document.createElement(tag)
|
|
|
|
const el = createHeadElement(headConfig)
|
|
|
|
for (const key in attrs) {
|
|
|
|
|
|
|
|
el.setAttribute(key, attrs[key])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (innerHTML) {
|
|
|
|
|
|
|
|
el.innerHTML = innerHTML
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
document.head.appendChild(el)
|
|
|
|
document.head.appendChild(el)
|
|
|
|
tags.push(el)
|
|
|
|
tags.push(el)
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -39,7 +42,8 @@ export function useUpdateHead(pageData) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
watchEffect(() => {
|
|
|
|
updateHeadTags(siteHeadTags, siteData.value.head)
|
|
|
|
updateTitleAndDescription()
|
|
|
|
|
|
|
|
updateHeadTags(siteHeadTags, siteDataRef.value.head)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
watchEffect(() => {
|
|
|
@ -49,3 +53,17 @@ export function useUpdateHead(pageData) {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param {import('src').HeadConfig} item
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function createHeadElement([tag, attrs, innerHTML]) {
|
|
|
|
|
|
|
|
const el = document.createElement(tag)
|
|
|
|
|
|
|
|
for (const key in attrs) {
|
|
|
|
|
|
|
|
el.setAttribute(key, attrs[key])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (innerHTML) {
|
|
|
|
|
|
|
|
el.innerHTML = innerHTML
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return el
|
|
|
|
|
|
|
|
}
|
|
|
|