Skip to content

Commit

Permalink
feat: dynamic posts
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Aug 22, 2024
1 parent 4863598 commit 8d04fe8
Show file tree
Hide file tree
Showing 29 changed files with 90 additions and 220 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public/bikes.json
.wrangler
.astro
.dev.vars
build/
build/
posts/
2 changes: 1 addition & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cloudflare from "@astrojs/cloudflare"
import markdown from "@astropub/md"

export default defineConfig({
output: "hybrid",
output: "server",
adapter: cloudflare({
imageService: "passthrough",
platformProxy: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/PostList.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { posts } = Astro.props
<ol>
{posts.map(post => (
<li>
<a href={`/posts/${post.url}`}>
<a href={`/posts/${post.slug}`}>
<span>{post.title}</span>
<small>{post.updated ? `Updated: ${formatDate(post.updated)}` : formatDate(post.published)}</small>
</a>
Expand Down
9 changes: 0 additions & 9 deletions src/content/config.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/content/posts/fox.md

This file was deleted.

28 changes: 0 additions & 28 deletions src/content/posts/git-hooks.md

This file was deleted.

14 changes: 0 additions & 14 deletions src/content/posts/hello-world.md

This file was deleted.

18 changes: 0 additions & 18 deletions src/content/posts/new-layout-and-projects.md

This file was deleted.

9 changes: 0 additions & 9 deletions src/content/posts/terrarium.md

This file was deleted.

6 changes: 1 addition & 5 deletions src/layouts/PhotoLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ type Props = {
const { post } = Astro.props as Props
---
<PostLayout post={post}>
<div>
<slot />
</div>
</PostLayout>
<PostLayout post={post} />

<style is:global>
#markdown > div > p {
Expand Down
6 changes: 4 additions & 2 deletions src/layouts/PostLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { Tags } from "@components"
import { Card, Subtitle, Title } from "@components/ui"
import { BaseLayout } from "@layouts"
import { formatDate } from "@utils"
import { markdown } from "@astropub/md"
type Props = {
post: Post
}
const { post } = Astro.props
const { title, description, published, tags, updated } = post
const { content, title, description, published, tags, type, updated } = post
const body = await markdown(content)
---
<BaseLayout title={title} description={description}>
<Card>
Expand All @@ -21,7 +23,7 @@ const { title, description, published, tags, updated } = post
</Subtitle>
<hr>
<div id="markdown">
<slot />
{type === "photo" ? <div>{body}</div> : body}
</div>
<hr>
<Tags tags={tags} />
Expand Down
1 change: 1 addition & 0 deletions src/pages/404.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { BaseLayout } from "@layouts"
import { Card, Subtitle, Title } from "@components/ui"
export const prerender = true
---
<BaseLayout title="Page Not Found">
<Card>
Expand Down
12 changes: 5 additions & 7 deletions src/pages/activity/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { APIContext } from "astro"
import metadata from "../../metadata"
import { getCollection } from "astro:content"
import { sortPosts } from "@utils"
export const prerender = false
import { PostService } from "@services"

export async function GET(context: APIContext) {
const { published } = (await getCollection("posts"))
.map(x => ({ ...x.data, url: x.slug}))
.sort(sortPosts)[0]
const service = PostService(context.locals.runtime.env.DB)
const post = await service.getEarliest()
const { author: { name, username }, description } = metadata
const { locals: { runtime: { env } }, request: { url } } = context
const { origin } = new URL(url)
Expand All @@ -24,7 +22,7 @@ export async function GET(context: APIContext) {
"summary": description,
"url": origin,
"discoverable": true,
"published": published.toISOString(),
"published": post!.published.toISOString(),
"icon": {
"type": "Image",
"mediaType": "image/webp",
Expand Down
2 changes: 0 additions & 2 deletions src/pages/api/message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { APIContext } from "astro"
import { json } from "@utils"

export const prerender = false

export async function POST(context: APIContext) {
const data = await context.request.json<any>()
if (!data["h-captcha-response"]) return json({
Expand Down
2 changes: 0 additions & 2 deletions src/pages/api/subscribe/confirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { R2Repository } from "@services"
import type { APIContext } from "astro"
import type { Subscriber } from "@schemas"

export const prerender = false

export async function GET(context: APIContext) {
const { request, locals: { runtime: { env } } } = context
const params = new URL(request.url).searchParams
Expand Down
2 changes: 0 additions & 2 deletions src/pages/api/subscribe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { EmailService, R2Repository } from "@services"
import type { APIContext } from "astro"
import type { Subscriber } from "@schemas"

export const prerender = false

export async function POST(context: APIContext) {
const { env } = context.locals.runtime
const site = context.site?.hostname
Expand Down
2 changes: 1 addition & 1 deletion src/pages/bikes/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Card, Subtitle, Title } from "@components/ui"
import { BaseLayout } from "@layouts"
import { GearService } from "@services"
export const prerender = false
const { slug } = Astro.params
if (!slug) return Astro.redirect("/404")
const bike = await GearService(Astro.locals.runtime.env.DB).getBySlug(slug)
Expand Down
5 changes: 3 additions & 2 deletions src/pages/bikes/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import { BaseLayout } from "@layouts"
import { Card, Subtitle, Title } from "@components/ui"
import { GearService } from "@services"
export const prerender = false
const bikes = await GearService(Astro.locals.runtime.env.DB).list()
const service = GearService(Astro.locals.runtime.env.DB)
const bikes = await service.list()
---
<BaseLayout title="The Bike Shed">
<Card>
Expand Down
1 change: 1 addition & 0 deletions src/pages/contact.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ContactForm } from "@components"
import { Card, Subtitle, Title } from "@components/ui"
import { BaseLayout } from "@layouts"
export const prerender = true
---
<BaseLayout title="Contact">
<Card>
Expand Down
15 changes: 6 additions & 9 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
---
import { getCollection } from "astro:content"
import { PostLayout, PhotoLayout } from "@layouts"
import { sortPosts } from "@utils"
import { PostService } from "@services"
const entry = (await getCollection("posts"))
.sort((a, b) => sortPosts(a.data, b.data))[0]
const Layout = entry.data.type == "post" ? PostLayout : PhotoLayout
const { Content } = await entry.render()
const service = PostService(Astro.locals.runtime.env.DB)
const post = await service.getLatest()
if (!post) return Astro.redirect("/404")
const Layout = post.type === "post" ? PostLayout : PhotoLayout
---
<Layout post={entry.data}>
<Content />
</Layout>
<PostLayout post={post} />
27 changes: 7 additions & 20 deletions src/pages/posts/[...slug].astro
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
---
import { getCollection } from "astro:content"
import { PostLayout, PhotoLayout } from "@layouts"
export async function getStaticPaths() {
const entries = await getCollection("posts")
return entries.map(function(entry) {
return {
params: {
slug: entry.slug
},
props: {
entry
},
}
})
}
const { entry } = Astro.props
const { Content } = await entry.render()
const Layout = entry.data.type == "post" ? PostLayout : PhotoLayout
import { PostService } from "@services"
const { slug } = Astro.params
const service = PostService(Astro.locals.runtime.env.DB)
const post = await service.getBySlug(slug!)
if (!post) return Astro.redirect("/404")
---
<Layout post={entry.data}>
<Content />
</Layout>
{post.type === "post" ? <PostLayout post={post}/> : <PhotoLayout post={post}/>}
8 changes: 3 additions & 5 deletions src/pages/posts/index.astro
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
---
import { getCollection } from "astro:content"
import { PostList } from "@components"
import { Card, Subtitle, Title } from "@components/ui"
import { BaseLayout } from "@layouts"
import { sortPosts } from "@utils"
import { PostService } from "@services"
const posts = (await getCollection("posts"))
.map(x => ({ ...x.data, url: x.slug }))
.sort(sortPosts)
const service = PostService(Astro.locals.runtime.env.DB)
const posts = await service.list()
---
<BaseLayout title="Posts">
<Card>
Expand Down
22 changes: 0 additions & 22 deletions src/pages/rss.xml.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import rss from "@astrojs/rss"
import metadata from "../metadata"
import { PostService } from "@services"
import type { APIContext } from "astro"

const { description } = metadata

export async function GET(context: APIContext) {
const service = PostService(context.locals.runtime.env.DB)
const posts = await service.list()
return rss({
title: context.site!.origin,
description: description,
site: context.site!,
items: posts.map((post) => ({
title: post.title,
description: post.tags.join(","),
link: `/posts/${post.slug}`,
pubDate: post.published,
}),
)
})
}
Loading

0 comments on commit 8d04fe8

Please sign in to comment.