aboutsummaryrefslogtreecommitdiff
path: root/node_modules/vuepress/lib/app/dataMixin.js
blob: fd003d926f1f9da66c175f48dbbd6f83d76dfd6e (plain)
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
import Vue from 'vue'
import { findPageForPath } from './util'

export default function dataMixin (siteData) {
  prepare(siteData)
  const store = new Vue({
    data: { siteData }
  })

  if (module.hot) {
    module.hot.accept('./.temp/siteData', () => {
      prepare(siteData)
      store.siteData = siteData
    })
  }

  return {
    computed: {
      $site () {
        return store.siteData
      },
      $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 || {}
      },
      $siteTitle () {
        return this.$localeConfig.title || this.$site.title || ''
      },
      $title () {
        const page = this.$page
        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'
      },
      $description () {
        // #565 hoist description from meta
        if (this.$page.frontmatter.meta) {
          const descriptionMeta = this.$page.frontmatter.meta.filter(item => item.name === 'description')[0]
          if (descriptionMeta) return descriptionMeta.content
        }
        return this.$page.frontmatter.description || this.$localeConfig.description || this.$site.description || ''
      },
      $lang () {
        return this.$page.frontmatter.lang || this.$localeConfig.lang || 'en-US'
      },
      $localePath () {
        return this.$localeConfig.path || '/'
      },
      $themeLocaleConfig () {
        return (this.$site.themeConfig.locales || {})[this.$localePath] || {}
      },
      $page () {
        return findPageForPath(
          this.$site.pages,
          this.$route.path
        )
      }
    }
  }
}

function prepare (siteData) {
  siteData.pages.forEach(page => {
    if (!page.frontmatter) {
      page.frontmatter = {}
    }
  })
  if (siteData.locales) {
    Object.keys(siteData.locales).forEach(path => {
      siteData.locales[path].path = path
    })
  }
  Object.freeze(siteData)
}