docs: improve multi-source handling (#682)

This commit is contained in:
Benjamin Canac
2023-09-15 14:37:53 +02:00
committed by GitHub
parent 81463cd21d
commit 8ec23c042d
10 changed files with 128 additions and 201 deletions

View File

@@ -10,7 +10,7 @@
<Footer />
<ClientOnly>
<LazyUDocsSearch ref="searchRef" :files="files" :navigation="navigation" />
<LazyUDocsSearch ref="searchRef" :files="files" :navigation="navigation" :groups="groups" />
</ClientOnly>
<UNotifications>
@@ -28,29 +28,32 @@
<script setup lang="ts">
import { withoutTrailingSlash } from 'ufo'
import { debounce } from 'perfect-debounce'
import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
const searchRef = ref()
const route = useRoute()
const colorMode = useColorMode()
const { prefix, removePrefixFromNavigation, removePrefixFromFiles } = useContentSource()
const { branch, branches } = useContentSource()
const { data: nav } = await useAsyncData('navigation', () => fetchContentNavigation())
const { data: search } = useLazyFetch('/api/search.json', { default: () => [], server: false })
const { data: files } = useLazyFetch<ParsedContent[]>('/api/search.json', { default: () => [], server: false })
// Computed
const navigation = computed(() => {
const navigation = nav.value.find(link => link._path === prefix.value)?.children || []
const main = nav.value.filter(item => item._path !== '/dev')
const dev = nav.value.find(item => item._path === '/dev')?.children
return prefix.value === '/main' ? removePrefixFromNavigation(navigation) : navigation
return branch.value?.name === 'dev' ? dev : main
})
const files = computed(() => {
const files = search.value.filter(file => file._path.startsWith(prefix.value))
const groups = computed(() => {
if (route.path === '/') {
return []
}
return prefix.value === '/main' ? removePrefixFromFiles(files) : files
return [{ key: 'branch', label: 'Branch', commands: branches.value }]
})
const color = computed(() => colorMode.value === 'dark' ? '#18181b' : 'white')