Last active
May 4, 2023 13:46
-
-
Save xinnks/022fc356326ab4369e6ed46fa31c4e57 to your computer and use it in GitHub Desktop.
Astro + Turso blog src/pages/post/[slug].astro file source code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| import { Markdown } from "@astropub/md" | |
| import BlogPost from '../../layouts/BlogPost.astro'; | |
| import { client } from '../../lib/tursoDb'; | |
| import type { Blog } from '../../lib/types'; | |
| const { slug } = Astro.params; | |
| let post: Blog | null = null; | |
| if(!slug){ | |
| return new Response(null, { | |
| status: 404, | |
| statusText: 'Not found' | |
| }); | |
| } | |
| try { | |
| const postResponse = await client.execute({ | |
| sql: "select posts.content, posts.published, posts.title, posts.description, posts.slug, posts.hero, posts.created_at, authors.first_name, authors.last_name, authors.slug, authors.avatar, authors.twitter from posts left join authors on authors.id = posts.author_id where posts.slug = ?;", | |
| args: [slug as string], | |
| }); | |
| if(!postResponse.rows.length){ | |
| return new Response(null, { | |
| status: 404, | |
| statusText: 'Not found' | |
| }); | |
| } | |
| const blogPostData = postResponse.rows[0] as any; | |
| if(!blogPostData){ | |
| return new Response(null, { | |
| status: 404, | |
| statusText: 'Not found' | |
| }); | |
| } | |
| post = { | |
| content: blogPostData.content, | |
| published: blogPostData.published, | |
| title: blogPostData.title, | |
| description: blogPostData.description, | |
| slug: blogPostData.slug, | |
| hero: blogPostData.hero, | |
| created_at: blogPostData.created_at, | |
| author: { | |
| first_name: blogPostData.first_name, | |
| last_name: blogPostData.last_name, | |
| slug: blogPostData.slug, | |
| avatar: blogPostData.avatar, | |
| email: blogPostData.email, | |
| socials: { | |
| twitter: blogPostData.twitter | |
| }, | |
| created_at: blogPostData.created_at | |
| } | |
| }; | |
| } catch (error) { | |
| // TODO: Handle error and notify user | |
| } | |
| --- | |
| <BlogPost {...post}> | |
| <Markdown of={post?.content || ""} /> | |
| </BlogPost> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment