Skip to content

Commit f278ddc

Browse files
committed
feat: infer content path from route path in CommonContent
1 parent ebfbb72 commit f278ddc

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

assets/index.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $common-margin: 24px;
3636
margin-bottom: $common-margin;
3737

3838
h2 {
39-
margin-bottom: $common-margin;
39+
margin-block: $common-margin;
4040
}
4141

4242
h2,

components/CommonContent.vue

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
<script lang="ts" setup>
2-
const props = defineProps<{ category: string }>();
2+
const props = defineProps<{ path?: string }>();
33
44
const route = useRoute();
55
const { locale } = useI18n();
66
7-
const { data: page } = await useAsyncData(
8-
computed(() => `${locale.value}:${props.category}:${route.params.slug}`),
9-
() => {
10-
return queryCollection(locale.value)
11-
.path(`/${props.category}/${route.params.slug}`)
12-
.first();
13-
}
7+
const contentPath = computed(() => {
8+
if (props.path) return props.path;
9+
10+
// For `prefix_except_default`
11+
const prefix = `/${locale.value}`;
12+
return route.path.startsWith(prefix)
13+
? route.path.substring(prefix.length) || '/'
14+
: route.path;
15+
});
16+
17+
const { data: page, error } = await useAsyncData(
18+
computed(() => `${locale.value}:${contentPath.value}`),
19+
() => queryCollection(locale.value).path(contentPath.value).first()
1420
);
1521
useHead({ title: page.value?.title });
22+
23+
if (error.value || !page.value) {
24+
throw createError({
25+
statusCode: 404,
26+
statusMessage: 'Page Not Found',
27+
fatal: true
28+
});
29+
}
1630
</script>
1731

1832
<template>
1933
<article v-if="page">
2034
<category-second
21-
v-if="page?.body.value[0][0] !== 'h2'"
22-
:id="page?.title"
23-
:title="page?.title"
24-
:right-text="page?.date.substring(0, 10)"
35+
v-if="page.body.value[0][0] !== 'h2'"
36+
:id="page.title"
37+
:title="page.title"
38+
:right-text="page.date?.substring(0, 10)"
2539
:title-url="`${route.path}#${page.title}`" />
2640
<ContentRenderer :value="page" class="heti" />
2741
</article>

nuxt.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export default defineNuxtConfig({
3232
'@nuxt/icon'
3333
],
3434

35+
router: {
36+
options: {
37+
scrollBehaviorType: 'smooth'
38+
}
39+
},
40+
3541
icon: {
3642
mode: 'css',
3743
cssLayer: 'base'

pages/aoscc/[slug].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
2-
<common-content category="aoscc" />
2+
<common-content />
33
</template>

pages/crowdsourcing/[slug].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
2-
<common-content category="crowdsourcing" />
2+
<common-content />
33
</template>

pages/news/[slug].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
2-
<common-content category="news" />
2+
<common-content />
33
</template>

0 commit comments

Comments
 (0)