ClientComputedMixin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Generated by "@vuepress/internal-transform-modules"
  3. */
  4. 'use strict'
  5. /**
  6. * Get page data via path (permalink).
  7. *
  8. * @param {array} pages
  9. * @param {string} path
  10. * @returns {object}
  11. */
  12. function findPageForPath (pages, path) {
  13. for (let i = 0; i < pages.length; i++) {
  14. const page = pages[i]
  15. if (page.path.toLowerCase() === path.toLowerCase()) {
  16. return page
  17. }
  18. }
  19. return {
  20. path: '',
  21. frontmatter: {}
  22. }
  23. }
  24. /**
  25. * Expose a function to get ClientComputedMixin constructor.
  26. * Note that this file will run in both server and client side.
  27. *
  28. * @param {object} siteData
  29. * @returns {ClientComputedMixin}
  30. */
  31. export default siteData => {
  32. return class ClientComputedMixin {
  33. setPage (page) {
  34. this.__page = page
  35. }
  36. get $site () {
  37. return siteData
  38. }
  39. get $themeConfig () {
  40. return this.$site.themeConfig
  41. }
  42. get $frontmatter () {
  43. return this.$page.frontmatter
  44. }
  45. get $localeConfig () {
  46. const { locales = {}} = this.$site
  47. let targetLang
  48. let defaultLang
  49. for (const path in locales) {
  50. if (path === '/') {
  51. defaultLang = locales[path]
  52. } else if (this.$page.path.indexOf(path) === 0) {
  53. targetLang = locales[path]
  54. }
  55. }
  56. return targetLang || defaultLang || {}
  57. }
  58. get $siteTitle () {
  59. return this.$localeConfig.title || this.$site.title || ''
  60. }
  61. get $canonicalUrl () {
  62. const { canonicalUrl } = this.$page.frontmatter
  63. if (typeof canonicalUrl === 'string') {
  64. return canonicalUrl
  65. }
  66. return false
  67. }
  68. get $title () {
  69. const page = this.$page
  70. const { metaTitle } = this.$page.frontmatter
  71. if (typeof metaTitle === 'string') {
  72. return metaTitle
  73. }
  74. const siteTitle = this.$siteTitle
  75. const selfTitle = page.frontmatter.home ? null : (
  76. page.frontmatter.title // explicit title
  77. || page.title // inferred title
  78. )
  79. return siteTitle
  80. ? selfTitle
  81. ? (selfTitle + ' | ' + siteTitle)
  82. : siteTitle
  83. : selfTitle || 'VuePress'
  84. }
  85. get $description () {
  86. // #565 hoist description from meta
  87. const description = getMetaDescription(this.$page.frontmatter.meta)
  88. if (description) {
  89. return description
  90. }
  91. return this.$page.frontmatter.description || this.$localeConfig.description || this.$site.description || ''
  92. }
  93. get $lang () {
  94. return this.$page.frontmatter.lang || this.$localeConfig.lang || 'en-US'
  95. }
  96. get $localePath () {
  97. return this.$localeConfig.path || '/'
  98. }
  99. get $themeLocaleConfig () {
  100. return (this.$site.themeConfig.locales || {})[this.$localePath] || {}
  101. }
  102. get $page () {
  103. if (this.__page) {
  104. return this.__page
  105. }
  106. return findPageForPath(
  107. this.$site.pages,
  108. this.$route.path
  109. )
  110. }
  111. }
  112. }
  113. function getMetaDescription (meta) {
  114. if (meta) {
  115. const descriptionMeta = meta.filter(item => item.name === 'description')[0]
  116. if (descriptionMeta) return descriptionMeta.content
  117. }
  118. }