refactor: re-expose $frontmatter global property, make localePath account for base by default

pull/317/head
Evan You 3 years ago
parent 76fa1733ff
commit a81b9e0ff1

@ -3,18 +3,19 @@ import { Route } from './router'
import { PageData, SiteData } from '/@types/shared' import { PageData, SiteData } from '/@types/shared'
import serializedSiteData from '@siteData' import serializedSiteData from '@siteData'
import { resolveSiteDataByRoute } from '../shared/config' import { resolveSiteDataByRoute } from '../shared/config'
import { withBase } from './utils'
export const dataSymbol: InjectionKey<VitePressData> = Symbol() export const dataSymbol: InjectionKey<VitePressData> = Symbol()
export interface VitePressData { export interface VitePressData {
site: Ref<SiteData> site: Ref<SiteData>
theme: Ref<any>
page: Ref<PageData> page: Ref<PageData>
frontmatter: Ref<any> theme: Ref<any>
lang: Ref<string> frontmatter: Ref<PageData['frontmatter']>
localePath: Ref<string>
title: Ref<string> title: Ref<string>
description: Ref<string> description: Ref<string>
lang: Ref<string>
localePath: Ref<string>
} }
// site data is a singleton // site data is a singleton
@ -48,7 +49,7 @@ export function initData(route: Route): VitePressData {
localePath: computed(() => { localePath: computed(() => {
const { locales, lang } = site.value const { locales, lang } = site.value
const path = Object.keys(locales).find((lp) => locales[lp].lang === lang) const path = Object.keys(locales).find((lp) => locales[lp].lang === lang)
return (locales && path) || '/' return withBase((locales && path) || '/')
}), }),
title: computed(() => { title: computed(() => {
return route.data.title return route.data.title

@ -70,6 +70,13 @@ export function createApp() {
: defineAsyncComponent(() => import('./components/Debug.vue')) : defineAsyncComponent(() => import('./components/Debug.vue'))
) )
// expose $frontmatter
Object.defineProperty(app.config.globalProperties, '$frontmatter', {
get() {
return data.frontmatter.value
}
})
if (Theme.enhanceApp) { if (Theme.enhanceApp) {
Theme.enhanceApp({ Theme.enhanceApp({
app, app,

@ -1,4 +1,4 @@
import { App, Ref, ComponentOptions } from 'vue' import { App, Ref, Component } from 'vue'
import { Router } from './router' import { Router } from './router'
import { SiteData } from '../../../types/shared' import { SiteData } from '../../../types/shared'
@ -9,7 +9,7 @@ export interface EnhanceAppContext {
} }
export interface Theme { export interface Theme {
Layout: ComponentOptions Layout: Component
NotFound?: ComponentOptions NotFound?: Component
enhanceApp?: (ctx: EnhanceAppContext) => void enhanceApp?: (ctx: EnhanceAppContext) => void
} }

@ -12,7 +12,7 @@ export { useData } from './app/data'
export { useRouter, useRoute } from './app/router' export { useRouter, useRoute } from './app/router'
// utilities // utilities
export { inBrowser, joinPath, withBase } from './app/utils' export { inBrowser, withBase } from './app/utils'
// components // components
export { Content } from './app/components/Content' export { Content } from './app/components/Content'

@ -27,9 +27,9 @@ const route = useRoute()
const { site, page, theme, frontmatter } = useData() const { site, page, theme, frontmatter } = useData()
// custom layout // custom layout
const isCustomLayout = computed(() => !!route.data.frontmatter.customLayout) const isCustomLayout = computed(() => !!frontmatter.value.customLayout)
// home // home
const enableHome = computed(() => !!route.data.frontmatter.home) const enableHome = computed(() => !!frontmatter.value.home)
// navbar // navbar
const showNavbar = computed(() => { const showNavbar = computed(() => {

@ -6,7 +6,7 @@ const { site, theme, localePath } = useData()
<template> <template>
<a <a
class="nav-bar-title" class="nav-bar-title"
:href="withBase(localePath)" :href="localePath"
:aria-label="`${site.title}, back to home`" :aria-label="`${site.title}, back to home`"
> >
<img <img

Loading…
Cancel
Save