feat: add `$withBase` global app function

pull/161/head
Kia King Ishii 4 years ago
parent 776d8014a2
commit 15e18df01e

@ -1,4 +1,5 @@
import { App } from 'vue' import { App } from 'vue'
import { joinPath } from './utils'
import { SiteDataRef } from './composables/siteData' import { SiteDataRef } from './composables/siteData'
import { PageDataRef } from './composables/PageData' import { PageDataRef } from './composables/PageData'
import { Content } from './components/Content' import { Content } from './components/Content'
@ -31,7 +32,7 @@ export function mixinGlobalComputed(
$page: { $page: {
get() { get() {
return page return page.value
} }
}, },
@ -51,6 +52,12 @@ export function mixinGlobalComputed(
get() { get() {
return page.value.description || siteByRoute.value.description return page.value.description || siteByRoute.value.description
} }
},
$withBase: {
value(path: string) {
return joinPath(site.value.base, path)
}
} }
}) })
} }

@ -1,5 +1,12 @@
export const inBrowser = typeof window !== 'undefined' export const inBrowser = typeof window !== 'undefined'
/**
* Join two paths by resolving the slash collision.
*/
export function joinPath(base: string, path: string): string {
return `${base}${path}`.replace(/\/+/g, '/')
}
/** /**
* Converts a url path to the corresponding js chunk filename. * Converts a url path to the corresponding js chunk filename.
*/ */

@ -2,7 +2,7 @@
<header class="hero"> <header class="hero">
<img <img
v-if="data.heroImage" v-if="data.heroImage"
:src="heroImageSrc" :src="$withBase(heroImageSrc)"
:alt="data.heroAlt || 'hero'" :alt="data.heroAlt || 'hero'"
/> />
@ -39,7 +39,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed } from 'vue'
import { useRoute, useSiteData } from 'vitepress' import { useRoute, useSiteData } from 'vitepress'
import { withBase } from '../utils'
const route = useRoute() const route = useRoute()
const siteData = useSiteData() const siteData = useSiteData()
@ -48,7 +47,7 @@ const actionLink = computed(() => ({
link: data.value.actionLink, link: data.value.actionLink,
text: data.value.actionText text: data.value.actionText
})) }))
const heroImageSrc = computed(() => withBase(data.value.heroImage)) const heroImageSrc = computed(() => data.value.heroImage)
const siteTitle = computed(() => siteData.value.title) const siteTitle = computed(() => siteData.value.title)
const siteDescription = computed(() => siteData.value.description) const siteDescription = computed(() => siteData.value.description)
</script> </script>

@ -16,8 +16,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, defineProps } from 'vue' import { computed, defineProps } from 'vue'
import { useRoute } from 'vitepress' import { useRoute } from 'vitepress'
import { withBase, isExternal } from '../utils' import { isExternal } from '../utils'
import type { DefaultTheme } from '../config' import type { DefaultTheme } from '../config'
import { useUrl } from '../composables/url'
import OutboundLink from './icons/OutboundLink.vue' import OutboundLink from './icons/OutboundLink.vue'
const { item } = defineProps<{ const { item } = defineProps<{
@ -35,6 +36,7 @@ const normalizePath = (path: string): string => {
return path return path
} }
const { withBase } = useUrl()
const route = useRoute() const route = useRoute()
const classes = computed(() => ({ const classes = computed(() => ({

@ -7,17 +7,13 @@
<img <img
v-if="$themeConfig.logo" v-if="$themeConfig.logo"
class="logo" class="logo"
:src="withBase($themeConfig.logo)" :src="$withBase($themeConfig.logo)"
alt="Logo" alt="Logo"
/> />
{{ $site.title }} {{ $site.title }}
</a> </a>
</template> </template>
<script setup lang="ts">
import { withBase } from '../utils'
</script>
<style scoped> <style scoped>
.nav-bar-title { .nav-bar-title {
font-size: 1.3rem; font-size: 1.3rem;

@ -2,13 +2,13 @@
<div v-if="hasLinks" class="next-and-prev-link"> <div v-if="hasLinks" class="next-and-prev-link">
<div class="container"> <div class="container">
<div class="prev"> <div class="prev">
<a v-if="prev" class="link" :href="withBase(prev.link)"> <a v-if="prev" class="link" :href="$withBase(prev.link)">
<ArrowLeft class="icon icon-prev" /> <ArrowLeft class="icon icon-prev" />
<span class="text">{{ prev.text }}</span> <span class="text">{{ prev.text }}</span>
</a> </a>
</div> </div>
<div class="next"> <div class="next">
<a v-if="next" class="link" :href="withBase(next.link)"> <a v-if="next" class="link" :href="$withBase(next.link)">
<span class="text">{{ next.text }}</span> <span class="text">{{ next.text }}</span>
<ArrowRight class="icon icon-next" /> <ArrowRight class="icon icon-next" />
</a> </a>
@ -18,7 +18,6 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { withBase } from '../utils'
import { useNextAndPrevLinks } from '../composables/nextAndPrevLinks' import { useNextAndPrevLinks } from '../composables/nextAndPrevLinks'
import ArrowLeft from './icons/ArrowLeft.vue' import ArrowLeft from './icons/ArrowLeft.vue'
import ArrowRight from './icons/ArrowRight.vue' import ArrowRight from './icons/ArrowRight.vue'

@ -0,0 +1,14 @@
import { useSiteData } from 'vitepress'
import { joinPath } from '/@app/utils'
export function useUrl() {
const site = useSiteData()
function withBase(path: string): string {
return joinPath(site.value.base, path)
}
return {
withBase
}
}

@ -1,4 +1,4 @@
import { useSiteData, Route } from 'vitepress' import { Route } from 'vitepress'
export const hashRE = /#.*$/ export const hashRE = /#.*$/
export const extRE = /(index)?\.(md|html)$/ export const extRE = /(index)?\.(md|html)$/
@ -13,10 +13,6 @@ export function isArray(value: any): value is any[] {
return Array.isArray(value) return Array.isArray(value)
} }
export function withBase(path: string) {
return (useSiteData().value.base + path).replace(/\/+/g, '/')
}
export function isExternal(path: string): boolean { export function isExternal(path: string): boolean {
return outboundRE.test(path) return outboundRE.test(path)
} }

Loading…
Cancel
Save