feat: Journal list, category pages, pagination, tokens and pl/en strings
- index and category pages paginate with ?page=N and 404 out of range - category content renders above the list through the html gate - related posts fall back to the latest posts - --journal-* tokens and neutral base styles - pl/en strings registered only for locales the app enables
This commit is contained in:
@@ -1,21 +1,49 @@
|
||||
<script setup lang="ts">
|
||||
// journal-category: posts in one category; an unknown category is a 404.
|
||||
// journal-category: one category's header and rich content above its posts.
|
||||
// The slug resolves against GET /categories?include_empty=1, so an empty
|
||||
// category answers 200 with its content; an unknown slug is a real 404 (this
|
||||
// route may be a root-level catch-all in the app).
|
||||
import { gateJournalHtml } from '../utils/htmlGate.ts'
|
||||
import { pageOutOfRange, parsePageParam } from '../utils/pagination.ts'
|
||||
import { toListPost } from '../utils/view.ts'
|
||||
import type { JournalModuleOptions } from '../utils/types.ts'
|
||||
|
||||
definePageMeta({ key: (route) => route.fullPath })
|
||||
|
||||
const route = useRoute()
|
||||
const api = useJournalApi()
|
||||
const cfg = useRuntimeConfig().public.journal as JournalModuleOptions
|
||||
const slug = String(route.params.category ?? '')
|
||||
const { data } = await useAsyncData(`journal-category-${slug}`, async () => {
|
||||
const category = await api.findCategory(slug)
|
||||
if (!category) return null
|
||||
return { category, page: await api.listPosts({ category: category.slug }) }
|
||||
|
||||
const page = parsePageParam(route.query.page)
|
||||
if (page === null) throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
|
||||
|
||||
const { data, error } = await useAsyncData(`journal-category-${slug}-${page}`, async () => {
|
||||
const found = await api.findCategory(slug)
|
||||
if (!found) return null
|
||||
const posts = await api.listPosts({ category: found.slug, page, perPage: cfg.perPage.category, sort: cfg.sort })
|
||||
// Only this category reaches the payload, with its content already gated.
|
||||
const gated = gateJournalHtml(found.content)
|
||||
const { children: _children, ...category } = found
|
||||
return {
|
||||
category: { ...category, content: gated.html || null },
|
||||
contentBlocked: !gated.safe,
|
||||
posts: { ...posts, data: posts.data.map(toListPost) },
|
||||
}
|
||||
})
|
||||
if (!data.value) throw createError({ statusCode: 404, fatal: true })
|
||||
if (error.value) {
|
||||
throw createError({ statusCode: (error.value as { statusCode?: number }).statusCode ?? 500, fatal: true })
|
||||
}
|
||||
if (!data.value) throw createError({ statusCode: 404, statusMessage: 'Category not found', fatal: true })
|
||||
if (pageOutOfRange(page, data.value.posts.meta.last_page)) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section v-if="data" class="journal-category">
|
||||
<h1>{{ data.category.name }}</h1>
|
||||
<ul>
|
||||
<li v-for="post in data.page.data" :key="post.id">{{ post.title }}</li>
|
||||
</ul>
|
||||
<JournalCategoryHeader :category="data.category" :page="page" :blocked="data.contentBlocked" />
|
||||
<JournalPostList :posts="data.posts.data" :empty="$t('journal.category.empty')" />
|
||||
<JournalPagination :current="data.posts.meta.current_page" :last="data.posts.meta.last_page" />
|
||||
</section>
|
||||
</template>
|
||||
|
||||
@@ -1,13 +1,37 @@
|
||||
<script setup lang="ts">
|
||||
// journal-index: the latest posts.
|
||||
// journal-index: the post list, paginated with ?page=N. A non-numeric page
|
||||
// or a page past the last one is a real 404.
|
||||
import { pageOutOfRange, parsePageParam } from '../utils/pagination.ts'
|
||||
import { toListPost } from '../utils/view.ts'
|
||||
import type { JournalModuleOptions } from '../utils/types.ts'
|
||||
|
||||
// Re-run setup (and its 404 checks) on every query change.
|
||||
definePageMeta({ key: (route) => route.fullPath })
|
||||
|
||||
const route = useRoute()
|
||||
const api = useJournalApi()
|
||||
const { data: page } = await useAsyncData('journal-index', () => api.listPosts({ perPage: 9 }))
|
||||
const cfg = useRuntimeConfig().public.journal as JournalModuleOptions
|
||||
|
||||
const page = parsePageParam(route.query.page)
|
||||
if (page === null) throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
|
||||
|
||||
const { data, error } = await useAsyncData(
|
||||
`journal-index-${page}`,
|
||||
() => api.listPosts({ page, perPage: cfg.perPage.index, sort: cfg.sort }),
|
||||
{ transform: (res) => ({ ...res, data: res.data.map(toListPost) }) },
|
||||
)
|
||||
if (error.value || !data.value) {
|
||||
throw createError({ statusCode: (error.value as { statusCode?: number } | undefined)?.statusCode ?? 500, fatal: true })
|
||||
}
|
||||
if (pageOutOfRange(page, data.value.meta.last_page)) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="journal-index">
|
||||
<ul v-if="page">
|
||||
<li v-for="post in page.data" :key="post.id">{{ post.title }}</li>
|
||||
</ul>
|
||||
<section v-if="data" class="journal-index">
|
||||
<JournalIndexHeader :page="page" />
|
||||
<JournalPostList :posts="data.data" />
|
||||
<JournalPagination :current="data.meta.current_page" :last="data.meta.last_page" />
|
||||
</section>
|
||||
</template>
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
<script setup lang="ts">
|
||||
// journal-post: one published post by slug. An unknown, draft or future post
|
||||
// is a real 404. The transform gates the body before it reaches the payload.
|
||||
import { toPostView } from '../utils/view.ts'
|
||||
// Related posts fall back to the latest posts when the API has none (it
|
||||
// returns none for a post without a category).
|
||||
import { toListPost, toPostView } from '../utils/view.ts'
|
||||
import type { JournalModuleOptions } from '../utils/types.ts'
|
||||
|
||||
const route = useRoute()
|
||||
const api = useJournalApi()
|
||||
const links = useJournalLinks()
|
||||
const cfg = useRuntimeConfig().public.journal as JournalModuleOptions
|
||||
const slug = computed(() => String(route.params.slug ?? ''))
|
||||
|
||||
const { data: post, error } = await useAsyncData(
|
||||
() => `journal-post-${slug.value}`,
|
||||
() => api.getPost(slug.value),
|
||||
async () => {
|
||||
const found = await api.getPost(slug.value)
|
||||
if (!found) return null
|
||||
const count = Math.max(0, cfg.relatedCount)
|
||||
let related = found.related_posts ?? []
|
||||
if (related.length === 0 && count > 0) {
|
||||
const latest = await api.listPosts({ perPage: count + 1, sort: cfg.sort })
|
||||
related = latest.data.filter((p) => p.slug !== found.slug)
|
||||
}
|
||||
return { ...found, related_posts: related.slice(0, count).map(toListPost) }
|
||||
},
|
||||
{ transform: (p) => (p ? toPostView(p) : null) },
|
||||
)
|
||||
|
||||
@@ -23,7 +38,30 @@ if (!post.value) {
|
||||
|
||||
<template>
|
||||
<article v-if="post" class="journal-post">
|
||||
<h1 class="journal-post__title">{{ post.title }}</h1>
|
||||
<header class="journal-post__header">
|
||||
<h1 class="journal-post__title">{{ post.title }}</h1>
|
||||
<JournalPostMeta :post="post" />
|
||||
</header>
|
||||
<JournalFeaturedImage :src="post.featured_image_url" :alt="post.title" eager />
|
||||
<JournalPostBody :html="post.content_html" :blocked="post.content_blocked" />
|
||||
<nav v-if="post.previous_post || post.next_post" class="journal-post__siblings">
|
||||
<NuxtLink
|
||||
v-if="post.previous_post && links.postLink(post.previous_post.slug)"
|
||||
:to="links.postLink(post.previous_post.slug)!"
|
||||
rel="prev"
|
||||
class="journal-post__previous"
|
||||
>
|
||||
<span>{{ $t('journal.post.previous') }}</span> {{ post.previous_post.title }}
|
||||
</NuxtLink>
|
||||
<NuxtLink
|
||||
v-if="post.next_post && links.postLink(post.next_post.slug)"
|
||||
:to="links.postLink(post.next_post.slug)!"
|
||||
rel="next"
|
||||
class="journal-post__next"
|
||||
>
|
||||
<span>{{ $t('journal.post.next') }}</span> {{ post.next_post.title }}
|
||||
</NuxtLink>
|
||||
</nav>
|
||||
<JournalRelatedPosts :posts="post.related_posts" />
|
||||
</article>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user