Skip to content

Commit

Permalink
feat: add $withBase global app function
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Nov 26, 2020
1 parent 776d801 commit 15e18df
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/client/app/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App } from 'vue'
import { joinPath } from './utils'
import { SiteDataRef } from './composables/siteData'
import { PageDataRef } from './composables/PageData'
import { Content } from './components/Content'
Expand Down Expand Up @@ -31,7 +32,7 @@ export function mixinGlobalComputed(

$page: {
get() {
return page
return page.value
}
},

Expand All @@ -51,6 +52,12 @@ export function mixinGlobalComputed(
get() {
return page.value.description || siteByRoute.value.description
}
},

$withBase: {
value(path: string) {
return joinPath(site.value.base, path)
}
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions src/client/app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
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.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/client/theme-default/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<header class="hero">
<img
v-if="data.heroImage"
:src="heroImageSrc"
:src="$withBase(heroImageSrc)"
:alt="data.heroAlt || 'hero'"
/>

Expand Down Expand Up @@ -39,7 +39,6 @@
<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useSiteData } from 'vitepress'
import { withBase } from '../utils'
const route = useRoute()
const siteData = useSiteData()
Expand All @@ -48,7 +47,7 @@ const actionLink = computed(() => ({
link: data.value.actionLink,
text: data.value.actionText
}))
const heroImageSrc = computed(() => withBase(data.value.heroImage))
const heroImageSrc = computed(() => data.value.heroImage)
const siteTitle = computed(() => siteData.value.title)
const siteDescription = computed(() => siteData.value.description)
</script>
Expand Down
4 changes: 3 additions & 1 deletion src/client/theme-default/components/NavBarLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<script setup lang="ts">
import { computed, defineProps } from 'vue'
import { useRoute } from 'vitepress'
import { withBase, isExternal } from '../utils'
import { isExternal } from '../utils'
import type { DefaultTheme } from '../config'
import { useUrl } from '../composables/url'
import OutboundLink from './icons/OutboundLink.vue'
const { item } = defineProps<{
Expand All @@ -35,6 +36,7 @@ const normalizePath = (path: string): string => {
return path
}
const { withBase } = useUrl()
const route = useRoute()
const classes = computed(() => ({
Expand Down
6 changes: 1 addition & 5 deletions src/client/theme-default/components/NavBarTitle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
<img
v-if="$themeConfig.logo"
class="logo"
:src="withBase($themeConfig.logo)"
:src="$withBase($themeConfig.logo)"
alt="Logo"
/>
{{ $site.title }}
</a>
</template>

<script setup lang="ts">
import { withBase } from '../utils'
</script>

<style scoped>
.nav-bar-title {
font-size: 1.3rem;
Expand Down
5 changes: 2 additions & 3 deletions src/client/theme-default/components/NextAndPrevLinks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<div v-if="hasLinks" class="next-and-prev-link">
<div class="container">
<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" />
<span class="text">{{ prev.text }}</span>
</a>
</div>
<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>
<ArrowRight class="icon icon-next" />
</a>
Expand All @@ -18,7 +18,6 @@
</template>

<script setup lang="ts">
import { withBase } from '../utils'
import { useNextAndPrevLinks } from '../composables/nextAndPrevLinks'
import ArrowLeft from './icons/ArrowLeft.vue'
import ArrowRight from './icons/ArrowRight.vue'
Expand Down
14 changes: 14 additions & 0 deletions src/client/theme-default/composables/url.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 1 addition & 5 deletions src/client/theme-default/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useSiteData, Route } from 'vitepress'
import { Route } from 'vitepress'

export const hashRE = /#.*$/
export const extRE = /(index)?\.(md|html)$/
Expand All @@ -13,10 +13,6 @@ export function isArray(value: any): value is any[] {
return Array.isArray(value)
}

export function withBase(path: string) {
return (useSiteData().value.base + path).replace(/\/+/g, '/')
}

export function isExternal(path: string): boolean {
return outboundRE.test(path)
}
Expand Down

0 comments on commit 15e18df

Please sign in to comment.