mirror of https://github.com/vuejs/vitepress
parent
c4002a8d2e
commit
96c30a6d83
@ -1,49 +1,9 @@
|
||||
import { h, shallowRef, watchEffect, inject, nextTick } from 'vue'
|
||||
import { RouteSymbol } from '../composables/router'
|
||||
import Theme from '/@theme/index'
|
||||
|
||||
const NotFound = Theme.NotFound || (() => '404 Not Found')
|
||||
import { h } from 'vue'
|
||||
import { useRoute } from '../composables/router'
|
||||
|
||||
export const Content = {
|
||||
setup() {
|
||||
const comp = shallowRef()
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
const route = inject(RouteSymbol, {
|
||||
path: '/',
|
||||
scrollPosition: 0
|
||||
})
|
||||
|
||||
watchEffect(() => {
|
||||
const pendingPath = route.path
|
||||
let pagePath = pendingPath.replace(/\.html$/, '')
|
||||
if (pagePath.endsWith('/')) {
|
||||
pagePath += 'index'
|
||||
}
|
||||
|
||||
// awlays force re-fetch content in dev
|
||||
import(`${pagePath}.md?t=${Date.now()}`)
|
||||
.then(async (m) => {
|
||||
if (route.path === pendingPath) {
|
||||
comp.value = m.default
|
||||
await nextTick()
|
||||
window.scrollTo({
|
||||
left: 0,
|
||||
top: route.scrollPosition,
|
||||
behavior: 'auto'
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (route.path === pendingPath) {
|
||||
comp.value = NotFound
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
// TODO SSR
|
||||
}
|
||||
|
||||
return () => (comp.value ? h(comp.value) : null)
|
||||
const route = useRoute()
|
||||
return () => (route.component ? h(route.component) : null)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue