From 793be8efd86b72aac41127c08bf001acc28ed873 Mon Sep 17 00:00:00 2001 From: Damillora Date: Mon, 2 Dec 2024 21:48:51 +0700 Subject: [PATCH] feat: add preview posts --- src/lib/components/SEO/PostSEO.svelte | 4 ++-- src/lib/content/contentApi.ts | 30 +++++++++++++++++++++------ src/lib/content/directusUtils.ts | 2 ++ src/routes/[tag]/[slug]/+page.svelte | 17 ++++++++++----- src/routes/[tag]/[slug]/+page.ts | 10 +++++++-- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/lib/components/SEO/PostSEO.svelte b/src/lib/components/SEO/PostSEO.svelte index f4a24b5..dfa0e1e 100644 --- a/src/lib/components/SEO/PostSEO.svelte +++ b/src/lib/components/SEO/PostSEO.svelte @@ -62,8 +62,8 @@ property="og:image" content={post.feature_image ?? 'https://blog.nanao.moe/images/default-feature.jpg'} /> - - + + diff --git a/src/lib/content/contentApi.ts b/src/lib/content/contentApi.ts index ed04028..ab2e137 100644 --- a/src/lib/content/contentApi.ts +++ b/src/lib/content/contentApi.ts @@ -1,5 +1,5 @@ -import { createDirectus, readItems, rest } from '@directus/sdk'; +import { createDirectus, readItem, readItems, rest } from '@directus/sdk'; import { DIRECTUS_URL, mapAuthor, mapCategory, mapIndexPosts, mapPost, mapSitemapPosts } from './directusUtils'; const POSTS_PER_PAGE = 10; @@ -10,6 +10,7 @@ const indexPageFields = [ "title", "feature_image.*", "date_published", + "date_updated", "excerpt", "slug", "author.*", @@ -17,9 +18,9 @@ const indexPageFields = [ "author.profile_image.*", ]; const allPageFields = [ - ...indexPageFields, "date_updated" + ...indexPageFields ]; -const postPageFields = [...indexPageFields, "content"]; +const postPageFields = [...indexPageFields, "status", "content"]; const authorPageFields = [ "id", "name", @@ -221,9 +222,14 @@ export const browsePostWithAuthor = async (slug: string, page = 1) => { export const readPost = async (slug: string) => { const filterPost = { - "slug": { - "_eq": slug, - } + "_and": [ + { + "slug": { + "_eq": slug, + } + }, + filterPublished + ] }; try { const post = await directus.request(readItems("posts", { @@ -237,6 +243,18 @@ export const readPost = async (slug: string) => { return null; } } + +export const readPreviewPost = async (id: string) => { + try { + const post = await directus.request(readItem("posts", id, { + fields: postPageFields, + })) + + return mapPost(post); + } catch (e) { + return null; + } +} export const readTag = async (slug: string) => { const filterTag = { "slug": { diff --git a/src/lib/content/directusUtils.ts b/src/lib/content/directusUtils.ts index 83e09b1..131910f 100644 --- a/src/lib/content/directusUtils.ts +++ b/src/lib/content/directusUtils.ts @@ -108,12 +108,14 @@ export const mapPost = (post: any) => { return { id: post.id, title: post.title, + status: post.status, feature_image: generateAssetUrl(post.feature_image), authors: [author], primary_author: author, category: category, excerpt: post.excerpt, date_published: post.date_published, + date_updated: post.date_updated, reading_time: null, url: generatePostUrl(category?.slug, post.slug), content: post.content, diff --git a/src/routes/[tag]/[slug]/+page.svelte b/src/routes/[tag]/[slug]/+page.svelte index 00182f7..9a33c0a 100644 --- a/src/routes/[tag]/[slug]/+page.svelte +++ b/src/routes/[tag]/[slug]/+page.svelte @@ -8,7 +8,8 @@ Container, PostRelated, PostCard, - GhostStyle + GhostStyle, + Notice } from '@damillora/plachta'; import PostSeo from '$lib/components/SEO/PostSEO.svelte'; @@ -23,12 +24,17 @@ + {#if data.post.status !== "published"} + + This is a preview of a draft post. + + {/if} @@ -38,6 +44,7 @@ + {#if data.relatedPost.length > 0} {/each} - -categorycategorycategorycategorycategorydate_published \ No newline at end of file + {/if} + \ No newline at end of file diff --git a/src/routes/[tag]/[slug]/+page.ts b/src/routes/[tag]/[slug]/+page.ts index 965c539..733aecf 100644 --- a/src/routes/[tag]/[slug]/+page.ts +++ b/src/routes/[tag]/[slug]/+page.ts @@ -1,11 +1,17 @@ -import { browseNextPost, browsePrevPost, browseRelatedPost, readPost } from '$lib/content/contentApi'; +import { browseNextPost, browsePrevPost, browseRelatedPost, readPost, readPreviewPost } from '$lib/content/contentApi'; import { processPostHtml } from '$lib/content/postProcessor'; import { error } from '@sveltejs/kit'; import type { PageLoad } from './$types'; export const load: PageLoad = async ({ params }) => { const postSlug = params.slug; - const post = await readPost(postSlug); + const tag = params.tag; + let post = null; + if (tag === "p") { + post = await readPreviewPost(postSlug); + } else { + post = await readPost(postSlug); + } if (!post) { error(404, 'Post not found'); }