Skip to content

Commit

Permalink
feat: get metadata from markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Aug 28, 2024
1 parent a9bc9bc commit 7e883d3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
65 changes: 65 additions & 0 deletions metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
author:
name: Benjamin Okkema
email: [email protected]
username: cptchloroplast
description: Benjamin Okkema's Homepage
repo: https://github.com/cptchloroplast/blog
navigation:
- text: Posts
href: /posts
- text: Tags
href: /tags
- text: The Bike Shed
href: /bikes
- text: Projects
children:
- text: Rayün Handmade
href: https://rayunhandmade.com
external: true
- text: Crank Tools
href: https://crank.tools
exernal: true
- text: Notebooks
href: https://public.notes.okkema.org
exernal: true
- text: Contact
href: /contact
- text: RSS
href: /rss.xml
icon: rss
profile:
bio:
- icon: lab
text: Console Cowboy @
link:
href: https://okkema.org
text: Okkema Labs
- icon: wrench
text: Guerrilla Bicycle Mechanic
- icon: leaf
text: Amateur Botanist
- icon: space
text: Astronomy Enthusiast
- icon: family
text: Husband and Father
contact:
- title: "@cptchloroplast"
icon: github
href: https://github.com/cptchloroplast
- title: Benjamin Okkema
icon: linkedin
href: https://www.linkedin.com/in/benokkema
- title: "@cptchloroplast"
icon: twitter
href: https://twitter.com/cptchloroplast
- title: Benjamin Okkema
icon: strava
href: https://www.strava.com/athletes/8782282
- title: [email protected]
icon: mail
href: mailto:[email protected]
- title: "@[email protected]"
icon: activitypub
href: https://ben.okkema.org/activity
---
5 changes: 4 additions & 1 deletion src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Gear } from "@schemas/strava"
import { GearService, PostService } from "@services"
import { parseMarkdown } from "@utils"
import * as Sentry from "@sentry/cloudflare"
import { PostSchema, type Post } from "@schemas"

const GearRegex = /gear\/([b0-9]+).meta.json/
const PostsRegex = /posts\/([A-Za-z0-9\-]+).md/
Expand All @@ -19,7 +20,9 @@ async function processBuckets(blog: R2Bucket, strava: R2Bucket, db: D1Database)
}
const body = await blog.get(key)
const raw = await body!.text()
const post = parseMarkdown(raw, slug)
const { content, frontmatter } = parseMarkdown<Post>(raw)
const post = { ...frontmatter, content, slug }
PostSchema.parse(post)
const service = PostService(db)
await service.upsert(post)
}
Expand Down
6 changes: 4 additions & 2 deletions src/services/MetadataService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { R2Bucket } from "@cloudflare/workers-types"
import type { Metadata } from "@schemas"
import { parseMarkdown } from "@utils"

type MetadataService = {
get(): Promise<Metadata | undefined>
Expand All @@ -8,8 +9,9 @@ type MetadataService = {
export function MetadataService(blog: R2Bucket): MetadataService {
return {
async get() {
const object = await blog.get("metadata.json")
return object?.json<Metadata>()
const object = await blog.get("metadata.md")
const raw = await object?.text()
return parseMarkdown<Metadata>(raw!).frontmatter
},
}
}
21 changes: 6 additions & 15 deletions src/utils/posts.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { PostSchema, type Post } from "@schemas"
import yaml from "js-yaml"
import { marked } from "marked"

export function sortPosts(a: Post, b: Post) {
const dateA = a.updated ?? a.published
const dateB = b.updated ?? b.published
return dateA < dateB ? 1 : -1
}

export function formatDate(date: string | Date) {
return new Date(date).toISOString().split("T")[0]
}

export function parseMarkdown(markdown: string, slug: string): Post {
const start = "---\n"
const end = "\n---"
const content = markdown.substring(markdown.indexOf(end) + end.length).trim()
const frontmatter = markdown.substring(start.length, markdown.indexOf(end))
const data = yaml.load(frontmatter) as Post
const post = { ...data, content, slug }
PostSchema.parse(post)
return post
export function parseMarkdown<T>(markdown: string): { content: string, frontmatter: T} {
const pieces = markdown.split("---")
if (pieces.length !== 3) throw new Error("Invalid formatting")
const frontmatter = yaml.load(pieces[1]) as T
const content = pieces[2].trim()
return { content, frontmatter }
}

export async function renderMarkdown(markdown: string) {
Expand Down

0 comments on commit 7e883d3

Please sign in to comment.