Skip to content

Commit 443120a

Browse files
feat: add blog slug routes
Signed-off-by: Kieran Klukas <[email protected]>
1 parent a26708c commit 443120a

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22
import http from "http";
3-
import { getPosts } from "./utils/vrite.ts";
3+
import { getPostSummaries, getPostDetail } from "./utils/vrite.ts";
44
import { getMessage } from "./utils/files.ts";
55
import { streamData } from "./utils/streaming.ts";
66

@@ -23,10 +23,15 @@ app.get("/", async (req, res) => {
2323

2424
// create blog mirror
2525
app.get("/blog", async (req, res) => {
26-
const posts = await getPosts();
26+
const posts = await getPostSummaries();
2727
streamData(req, res, posts);
2828
});
2929

30+
app.get("/blog/:slug", async (req, res) => {
31+
const post = await getPostDetail(req.params.slug);
32+
streamData(req, res, post);
33+
});
34+
3035
// Create HTTP server
3136
const server = http.createServer(app);
3237

utils/vrite.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { createClient } from "@vrite/sdk/api";
2+
import { gfmOutputTransformer } from "@vrite/sdk/transformers";
23

34
const vrite = createClient({
45
token: process.env.VRITE_ACCESS_TOKEN || "",
56
});
67

7-
export async function getPosts(): Promise<any> {
8+
export async function getPostSummaries(): Promise<any> {
89
const vritePosts = await vrite.contentPieces.list({
910
contentGroupId: process.env.VRITE_CONTENT_GROUP_ID || "",
1011
});
@@ -16,9 +17,35 @@ export async function getPosts(): Promise<any> {
1617
id: post.id,
1718
content: true,
1819
});
19-
body += `\n---\n## ${contentPiece.title}\n${contentPiece.description?.replace("<p>", "").replace("</p>", "")}`;
20+
body += `\n---\n## ${contentPiece.title}\n${contentPiece.description?.replace("<p>", "").replace("</p>", "")}\n{slug: "/blog/${post.slug}"}`;
2021
}
2122

2223
body += "\n---";
2324
return body;
2425
}
26+
27+
export async function getPostDetail(slug: string): Promise<any> {
28+
const vritePosts = await vrite.contentPieces.list({
29+
contentGroupId: process.env.VRITE_CONTENT_GROUP_ID || "",
30+
});
31+
32+
const vritePost = vritePosts.find((post) => post.slug === slug);
33+
34+
if (!vritePost) {
35+
return `huh? looks like you found a broken link or are snooping around ^_^`;
36+
}
37+
38+
const postData = await vrite.contentPieces.get({
39+
id: vritePost.id,
40+
content: true,
41+
});
42+
43+
// get the content of the post in markdown format and remove any images
44+
const content = gfmOutputTransformer(postData.content).replace(
45+
/!\[.*\]\(.*\)/g,
46+
"",
47+
);
48+
const body = `# ${postData.title}\n${content}`;
49+
50+
return body;
51+
}

0 commit comments

Comments
 (0)