/
setupUpdateHead.ts
129 lines (114 loc) · 2.94 KB
/
setupUpdateHead.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { isPlainObject, isString } from '@vuepress/shared'
import type { HeadConfig, VuepressSSRContext } from '@vuepress/shared'
import { onMounted, provide, ref, useSSRContext, watch } from 'vue'
import {
updateHeadSymbol,
usePageHead,
usePageLang,
} from './composables/index.js'
import type { UpdateHead } from './composables/index.js'
/**
* Auto update head and provide as global util
*/
export const setupUpdateHead = (): void => {
const head = usePageHead()
const lang = usePageLang()
// ssr-only, extract page meta info to ssrContext
if (__VUEPRESS_SSR__) {
const ssrContext: VuepressSSRContext | undefined = useSSRContext()
if (ssrContext) {
ssrContext.head = head.value
ssrContext.lang = lang.value
}
return
}
const headTags = ref<HTMLElement[]>([])
// load current head tags from DOM
const loadHead = (): void => {
head.value.forEach((item) => {
const tag = queryHeadTag(item)
if (tag) {
headTags.value.push(tag)
}
})
}
// update html lang attribute and head tags to DOM
const updateHead: UpdateHead = () => {
document.documentElement.lang = lang.value
headTags.value.forEach((item) => {
if (item.parentNode === document.head) {
document.head.removeChild(item)
}
})
headTags.value.splice(0, headTags.value.length)
head.value.forEach((item) => {
const tag = createHeadTag(item)
if (tag !== null) {
document.head.appendChild(tag)
headTags.value.push(tag)
}
})
}
provide(updateHeadSymbol, updateHead)
onMounted(() => {
loadHead()
updateHead()
watch(
() => head.value,
() => updateHead()
)
})
}
/**
* Query the matched head tag of head config
*/
export const queryHeadTag = ([
tagName,
attrs,
content = '',
]: HeadConfig): HTMLElement | null => {
const attrsSelector = Object.entries(attrs)
.map(([key, value]) => {
if (isString(value)) {
return `[${key}=${JSON.stringify(value)}]`
}
if (value === true) {
return `[${key}]`
}
return ''
})
.join('')
const selector = `head > ${tagName}${attrsSelector}`
const tags = Array.from(document.querySelectorAll<HTMLElement>(selector))
const matchedTag = tags.find((item) => item.innerText === content)
return matchedTag || null
}
/**
* Create head tag from head config
*/
export const createHeadTag = ([
tagName,
attrs,
content,
]: HeadConfig): HTMLElement | null => {
if (!isString(tagName)) {
return null
}
// create element
const tag = document.createElement(tagName)
// set attributes
if (isPlainObject(attrs)) {
Object.entries(attrs).forEach(([key, value]) => {
if (isString(value)) {
tag.setAttribute(key, value)
} else if (value === true) {
tag.setAttribute(key, '')
}
})
}
// set content
if (isString(content)) {
tag.appendChild(document.createTextNode(content))
}
return tag
}