|
| 1 | +import { queryContent } from '#imports' |
1 | 2 | import type { BlogArticle, BlogAlternateLanguageLink, BlogAlternateHeader } from '~/types/blog' |
2 | 3 |
|
3 | 4 | export interface BlogOverviewOptions { |
@@ -79,21 +80,32 @@ export function useBlogArticle() { |
79 | 80 | const config = useRuntimeConfig() |
80 | 81 |
|
81 | 82 | // Slug derived from catch-all route param; reactive on client navigation |
82 | | - const slug = computed<string | undefined>(() => { |
| 83 | + const slugSegments = computed<string[]>(() => { |
83 | 84 | const p = (route.params as any)?.slug |
84 | | - if (Array.isArray(p) && p.length) return String(p[p.length - 1]) |
85 | | - if (typeof p === 'string') return p |
86 | | - const parts = (route.path || '').split('/') |
87 | | - return parts.at(3) |
| 85 | + if (Array.isArray(p) && p.length) return p.map(String) |
| 86 | + if (typeof p === 'string' && p.length > 0) return [p] |
| 87 | + |
| 88 | + const parts = (route.path || '').split('/').filter(Boolean) |
| 89 | + const blogIndex = parts.indexOf('blog') |
| 90 | + if (blogIndex !== -1) return parts.slice(blogIndex + 1) |
| 91 | + return [] |
88 | 92 | }) |
89 | 93 |
|
| 94 | + const slug = computed<string | undefined>(() => slugSegments.value.at(-1)) |
| 95 | + const contentPath = computed( |
| 96 | + () => `/${['blog', locale.value, ...slugSegments.value].filter(Boolean).join('/')}` |
| 97 | + ) |
| 98 | + |
90 | 99 | const { data: article } = useAsyncData<BlogArticle | null>( |
91 | 100 | () => `${route.path}-${locale.value}`, |
92 | | - () => { |
93 | | - // @ts-ignore queryCollection is provided by @nuxt/content |
94 | | - return queryCollection('blog_' + (locale?.value || 'de')) |
95 | | - .where('slug', '=', slug.value as any) |
96 | | - .first() |
| 101 | + async () => { |
| 102 | + if (!slug.value) return null |
| 103 | + |
| 104 | + const doc = await queryContent(`blog/${locale.value}`) |
| 105 | + .where('_path', '=', contentPath.value) |
| 106 | + .findOne() |
| 107 | + |
| 108 | + return (doc as BlogArticle | null) || null |
97 | 109 | }, |
98 | 110 | { watch: [locale, slug] } |
99 | 111 | ) |
|
0 commit comments