|
|
|
@ -1,11 +1,20 @@
|
|
|
|
|
import { watchEffect } from 'vue'
|
|
|
|
|
import { useSiteData } from './siteData'
|
|
|
|
|
import { siteDataRef } from './siteData'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {import('./pageData').PageDataRef} 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[]}
|
|
|
|
@ -24,14 +33,8 @@ export function useUpdateHead(pageData) {
|
|
|
|
|
tags.forEach((el) => document.head.removeChild(el))
|
|
|
|
|
tags.length = 0
|
|
|
|
|
if (newTags && newTags.length) {
|
|
|
|
|
newTags.forEach(([tag, attrs, innerHTML]) => {
|
|
|
|
|
const el = document.createElement(tag)
|
|
|
|
|
for (const key in attrs) {
|
|
|
|
|
el.setAttribute(key, attrs[key])
|
|
|
|
|
}
|
|
|
|
|
if (innerHTML) {
|
|
|
|
|
el.innerHTML = innerHTML
|
|
|
|
|
}
|
|
|
|
|
newTags.forEach((headConfig) => {
|
|
|
|
|
const el = createHeadElement(headConfig)
|
|
|
|
|
document.head.appendChild(el)
|
|
|
|
|
tags.push(el)
|
|
|
|
|
})
|
|
|
|
@ -39,7 +42,8 @@ export function useUpdateHead(pageData) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
|
updateHeadTags(siteHeadTags, siteData.value.head)
|
|
|
|
|
updateTitleAndDescription()
|
|
|
|
|
updateHeadTags(siteHeadTags, siteDataRef.value.head)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|