|
| 1 | +<!-- TODO: remove this once https://github.com/nuxtlabs/docus/pull/1174 gets merged --> |
| 2 | +<script setup lang="ts"> |
| 3 | +import { kebabCase } from 'scule' |
| 4 | +import type { ContentNavigationItem, Collections, DocsEnCollectionItem } from '@nuxt/content' |
| 5 | +import { findPageHeadline } from '@nuxt/content/utils' |
| 6 | +
|
| 7 | +const addPrerenderPath = (path: string) => { |
| 8 | + const event = useRequestEvent() |
| 9 | + if (event) { |
| 10 | + event.node.res.setHeader( |
| 11 | + 'x-nitro-prerender', |
| 12 | + [ |
| 13 | + event.node.res.getHeader('x-nitro-prerender'), |
| 14 | + path, |
| 15 | + ].filter(Boolean).join(','), |
| 16 | + ) |
| 17 | + } |
| 18 | +} |
| 19 | +
|
| 20 | +definePageMeta({ |
| 21 | + layout: 'docs', |
| 22 | +}) |
| 23 | +
|
| 24 | +const route = useRoute() |
| 25 | +const { locale, isEnabled, t } = useDocusI18n() |
| 26 | +const appConfig = useAppConfig() |
| 27 | +const navigation = inject<Ref<ContentNavigationItem[]>>('navigation') |
| 28 | +
|
| 29 | +const collectionName = computed(() => isEnabled.value ? `docs_${locale.value}` : 'docs') |
| 30 | +
|
| 31 | +const [{ data: page }, { data: surround }] = await Promise.all([ |
| 32 | + useAsyncData(kebabCase(route.path), () => queryCollection(collectionName.value as keyof Collections).path(route.path).first() as Promise<DocsEnCollectionItem>), |
| 33 | + useAsyncData(`${kebabCase(route.path)}-surround`, () => { |
| 34 | + return queryCollectionItemSurroundings(collectionName.value as keyof Collections, route.path, { |
| 35 | + fields: ['description'], |
| 36 | + }) |
| 37 | + }), |
| 38 | +]) |
| 39 | +
|
| 40 | +if (!page.value) { |
| 41 | + throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true }) |
| 42 | +} |
| 43 | +
|
| 44 | +if (page.value.layout) { |
| 45 | + setPageLayout(page.value.layout as never) |
| 46 | +} |
| 47 | +
|
| 48 | +// Add the page path to the prerender list |
| 49 | +addPrerenderPath(`/raw${route.path}.md`) |
| 50 | +
|
| 51 | +const title = page.value.seo?.title || page.value.title |
| 52 | +const description = page.value.seo?.description || page.value.description |
| 53 | +
|
| 54 | +useSeoMeta({ |
| 55 | + title, |
| 56 | + ogTitle: title, |
| 57 | + description, |
| 58 | + ogDescription: description, |
| 59 | +}) |
| 60 | +
|
| 61 | +const headline = computed(() => findPageHeadline(navigation?.value, page.value?.path)) |
| 62 | +defineOgImageComponent('Docs', { |
| 63 | + headline: headline.value, |
| 64 | +}) |
| 65 | +
|
| 66 | +const editLink = computed(() => { |
| 67 | + if (!appConfig.github) { |
| 68 | + return |
| 69 | + } |
| 70 | +
|
| 71 | + return [ |
| 72 | + appConfig.github.url, |
| 73 | + 'edit', |
| 74 | + appConfig.github.branch, |
| 75 | + appConfig.github.rootDir, |
| 76 | + 'content', |
| 77 | + `${page.value?.stem}.${page.value?.extension}`, |
| 78 | + ].filter(Boolean).join('/') |
| 79 | +}) |
| 80 | +</script> |
| 81 | + |
| 82 | +<template> |
| 83 | + <UPage v-if="page"> |
| 84 | + <UPageHeader |
| 85 | + :title="page.title" |
| 86 | + :description="page.description" |
| 87 | + :headline="headline" |
| 88 | + :ui="{ |
| 89 | + wrapper: 'flex-row items-center flex-wrap justify-between', |
| 90 | + }" |
| 91 | + > |
| 92 | + <template #links> |
| 93 | + <UButton |
| 94 | + v-for="(link, index) in page.links" |
| 95 | + :key="index" |
| 96 | + size="sm" |
| 97 | + v-bind="link" |
| 98 | + /> |
| 99 | + |
| 100 | + <DocsPageHeaderLinks /> |
| 101 | + </template> |
| 102 | + </UPageHeader> |
| 103 | + |
| 104 | + <UPageBody> |
| 105 | + <ContentRenderer |
| 106 | + v-if="page" |
| 107 | + :value="page" |
| 108 | + /> |
| 109 | + |
| 110 | + <USeparator> |
| 111 | + <div |
| 112 | + v-if="editLink" |
| 113 | + class="flex items-center gap-2 text-sm text-muted" |
| 114 | + > |
| 115 | + <UButton |
| 116 | + variant="link" |
| 117 | + color="neutral" |
| 118 | + :to="editLink" |
| 119 | + target="_blank" |
| 120 | + icon="i-lucide-pen" |
| 121 | + :ui="{ leadingIcon: 'size-4' }" |
| 122 | + > |
| 123 | + {{ t('docs.edit') }} |
| 124 | + </UButton> |
| 125 | + <span>{{ t('common.or') }}</span> |
| 126 | + <UButton |
| 127 | + variant="link" |
| 128 | + color="neutral" |
| 129 | + :to="`${appConfig.github.url}/issues/new/choose`" |
| 130 | + target="_blank" |
| 131 | + icon="i-lucide-alert-circle" |
| 132 | + :ui="{ leadingIcon: 'size-4' }" |
| 133 | + > |
| 134 | + {{ t('docs.report') }} |
| 135 | + </UButton> |
| 136 | + </div> |
| 137 | + </USeparator> |
| 138 | + <UContentSurround :surround="surround" /> |
| 139 | + </UPageBody> |
| 140 | + |
| 141 | + <template |
| 142 | + v-if="page?.body?.toc?.links?.length" |
| 143 | + #right |
| 144 | + > |
| 145 | + <UContentToc |
| 146 | + highlight |
| 147 | + :title="appConfig.toc?.title || t('docs.toc')" |
| 148 | + :links="page.body?.toc?.links" |
| 149 | + > |
| 150 | + <template #bottom> |
| 151 | + <DocsAsideRightBottom /> |
| 152 | + </template> |
| 153 | + </UContentToc> |
| 154 | + </template> |
| 155 | + </UPage> |
| 156 | +</template> |
0 commit comments