-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ClientComputedMixin.js
135 lines (115 loc) Β· 3 KB
/
ClientComputedMixin.js
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
130
131
132
133
134
135
'use strict'
/**
* Get page data via path (permalink).
*
* @param {array} pages
* @param {string} path
* @returns {object}
*/
function findPageForPath (pages, path) {
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path.toLowerCase() === path.toLowerCase()) {
return page
}
}
return {
path: '',
frontmatter: {}
}
}
/**
* Expose a function to get ClientComputedMixin constructor.
* Note that this file will run in both server and client side.
*
* @param {object} siteData
* @returns {ClientComputedMixin}
*/
module.exports = siteData => {
return class ClientComputedMixin {
setPage (page) {
this.__page = page
}
get $site () {
return siteData
}
get $themeConfig () {
return this.$site.themeConfig
}
get $frontmatter () {
return this.$page.frontmatter
}
get $localeConfig () {
const { locales = {}} = this.$site
let targetLang
let defaultLang
for (const path in locales) {
if (path === '/') {
defaultLang = locales[path]
} else if (this.$page.path.indexOf(path) === 0) {
targetLang = locales[path]
}
}
return targetLang || defaultLang || {}
}
get $siteTitle () {
return this.$localeConfig.title || this.$site.title || ''
}
get $canonicalUrl () {
const { canonicalUrl } = this.$page.frontmatter
if (typeof canonicalUrl === 'string') {
return canonicalUrl
}
return false
}
get $title () {
const page = this.$page
const { metaTitle } = this.$page.frontmatter
if (typeof metaTitle === 'string') {
return metaTitle
}
const siteTitle = this.$siteTitle
const selfTitle = page.frontmatter.home ? null : (
page.frontmatter.title // explicit title
|| page.title // inferred title
)
return siteTitle
? selfTitle
? (selfTitle + ' | ' + siteTitle)
: siteTitle
: selfTitle || 'VuePress'
}
get $description () {
// #565 hoist description from meta
const description = getMetaDescription(this.$page.frontmatter.meta)
if (description) {
return description
}
return this.$page.frontmatter.description || this.$localeConfig.description || this.$site.description || ''
}
get $lang () {
return this.$page.frontmatter.lang || this.$localeConfig.lang || 'en-US'
}
get $localePath () {
return this.$localeConfig.path || '/'
}
get $themeLocaleConfig () {
return (this.$site.themeConfig.locales || {})[this.$localePath] || {}
}
get $page () {
if (this.__page) {
return this.__page
}
return findPageForPath(
this.$site.pages,
this.$route.path
)
}
}
}
function getMetaDescription (meta) {
if (meta) {
const descriptionMeta = meta.filter(item => item.name === 'description')[0]
if (descriptionMeta) return descriptionMeta.content
}
}