feat: add preview posts
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Damillora 2024-12-02 21:48:51 +07:00
parent 4e9b70f33d
commit 793be8efd8
5 changed files with 48 additions and 15 deletions

View File

@ -62,8 +62,8 @@
property="og:image" property="og:image"
content={post.feature_image ?? 'https://blog.nanao.moe/images/default-feature.jpg'} content={post.feature_image ?? 'https://blog.nanao.moe/images/default-feature.jpg'}
/> />
<meta property="article:published_time" content="2022-07-22T12:42:23.000Z" /> <meta property="article:published_time" content="{post.date_published}" />
<meta property="article:modified_time" content="2022-07-22T12:47:19.000Z" /> <meta property="article:modified_time" content="{post.date_updated}" />
<meta property="article:tag" content={post.category.name} /> <meta property="article:tag" content={post.category.name} />
<meta property="article:publisher" content="https://www.facebook.com/Damillora" /> <meta property="article:publisher" content="https://www.facebook.com/Damillora" />

View File

@ -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'; import { DIRECTUS_URL, mapAuthor, mapCategory, mapIndexPosts, mapPost, mapSitemapPosts } from './directusUtils';
const POSTS_PER_PAGE = 10; const POSTS_PER_PAGE = 10;
@ -10,6 +10,7 @@ const indexPageFields = [
"title", "title",
"feature_image.*", "feature_image.*",
"date_published", "date_published",
"date_updated",
"excerpt", "excerpt",
"slug", "slug",
"author.*", "author.*",
@ -17,9 +18,9 @@ const indexPageFields = [
"author.profile_image.*", "author.profile_image.*",
]; ];
const allPageFields = [ const allPageFields = [
...indexPageFields, "date_updated" ...indexPageFields
]; ];
const postPageFields = [...indexPageFields, "content"]; const postPageFields = [...indexPageFields, "status", "content"];
const authorPageFields = [ const authorPageFields = [
"id", "id",
"name", "name",
@ -221,9 +222,14 @@ export const browsePostWithAuthor = async (slug: string, page = 1) => {
export const readPost = async (slug: string) => { export const readPost = async (slug: string) => {
const filterPost = { const filterPost = {
"_and": [
{
"slug": { "slug": {
"_eq": slug, "_eq": slug,
} }
},
filterPublished
]
}; };
try { try {
const post = await directus.request(readItems("posts", { const post = await directus.request(readItems("posts", {
@ -237,6 +243,18 @@ export const readPost = async (slug: string) => {
return null; 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) => { export const readTag = async (slug: string) => {
const filterTag = { const filterTag = {
"slug": { "slug": {

View File

@ -108,12 +108,14 @@ export const mapPost = (post: any) => {
return { return {
id: post.id, id: post.id,
title: post.title, title: post.title,
status: post.status,
feature_image: generateAssetUrl(post.feature_image), feature_image: generateAssetUrl(post.feature_image),
authors: [author], authors: [author],
primary_author: author, primary_author: author,
category: category, category: category,
excerpt: post.excerpt, excerpt: post.excerpt,
date_published: post.date_published, date_published: post.date_published,
date_updated: post.date_updated,
reading_time: null, reading_time: null,
url: generatePostUrl(category?.slug, post.slug), url: generatePostUrl(category?.slug, post.slug),
content: post.content, content: post.content,

View File

@ -8,7 +8,8 @@
Container, Container,
PostRelated, PostRelated,
PostCard, PostCard,
GhostStyle GhostStyle,
Notice
} from '@damillora/plachta'; } from '@damillora/plachta';
import PostSeo from '$lib/components/SEO/PostSEO.svelte'; import PostSeo from '$lib/components/SEO/PostSEO.svelte';
@ -23,12 +24,17 @@
<Container> <Container>
<Post> <Post>
{#if data.post.status !== "published"}
<Notice>
This is a <strong>preview</strong> of a draft post.
</Notice>
{/if}
<PostHeader <PostHeader
background={data.post.feature_image} background={data.post.feature_image}
title={data.post.title} title={data.post.title}
authors={data.post.authors} authors={data.post.authors}
primary_tag={data.post.category} primary_tag={data.post.category}
date={dayjs(data.post.date_published).format('DD MMM YYYY')} date={data.post.date_published ? dayjs(data.post.date_published).format('DD MMM YYYY') : null}
/> />
<PostMain> <PostMain>
<GhostStyle> <GhostStyle>
@ -38,6 +44,7 @@
<PostNavigator prev_post={data.prevPost} next_post={data.nextPost} /> <PostNavigator prev_post={data.prevPost} next_post={data.nextPost} />
</Post> </Post>
{#if data.relatedPost.length > 0}
<PostRelated <PostRelated
name={data.post.category.name} name={data.post.category.name}
url={data.post.category?.url} url={data.post.category?.url}
@ -55,5 +62,5 @@
/> />
{/each} {/each}
</PostRelated> </PostRelated>
{/if}
</Container> </Container>
categorycategorycategorycategorycategorydate_published

View File

@ -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 { processPostHtml } from '$lib/content/postProcessor';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types'; import type { PageLoad } from './$types';
export const load: PageLoad = async ({ params }) => { export const load: PageLoad = async ({ params }) => {
const postSlug = params.slug; 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) { if (!post) {
error(404, 'Post not found'); error(404, 'Post not found');
} }