Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add remote content fetching for blog
Marc Seitz89071b6aa13f
diff --git a/app/(static)/blog/[slug]/page.tsx b/app/(static)/blog/[slug]/page.tsxindex ea96546d..ed73a6d5 100644--- a/app/(static)/blog/[slug]/page.tsx+++ b/app/(static)/blog/[slug]/page.tsx@@ -1,4 +1,4 @@-import { getPosts, getPost } from "@/lib/content/blog";+import { getPostsRemote as getPosts, getPost } from "@/lib/content/blog";import { ContentBody } from "@/components/mdx/post-body";import { notFound } from "next/navigation";import Link from "next/link";diff --git a/app/sitemap.ts b/app/sitemap.tsindex 3980fd20..1501a135 100644--- a/app/sitemap.ts+++ b/app/sitemap.ts@@ -1,62 +1,62 @@-import { getPosts } from "@/lib/content/blog";+import { getPostsRemote as getPosts } from "@/lib/content/blog";import { MetadataRoute } from "next";export default async function sitemap(): Promise<MetadataRoute.Sitemap> {- const posts = await getPosts()+ const posts = await getPosts();const blogs = posts.map((post) => ({url: `https://www.papermark.io/blog/${post?.data.slug}`,- lastModified: new Date().toISOString().split('T')[0],- }))-+ lastModified: new Date().toISOString().split("T")[0],+ }));+return [{url: "https://www.papermark.io",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/alternatives/docsend",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/alternatives/brieflink",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/alternatives/pandadoc",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/alternatives/google-drive",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/alternatives/pitch",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/privacy",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/open-source-investors",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/docsend-alternatives",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/oss-friends",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://vc.papermark.io",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},{url: "https://www.papermark.io/investors",- lastModified: new Date().toISOString().split('T')[0],+ lastModified: new Date().toISOString().split("T")[0],},- ...blogs+ ...blogs,];}diff --git a/lib/content/blog.ts b/lib/content/blog.tsindex 9e638d3c..d6ea112a 100644--- a/lib/content/blog.ts+++ b/lib/content/blog.ts@@ -1,6 +1,4 @@import matter from "gray-matter";-import path from "path";-import fs from "fs/promises";import { cache } from "react";type Post = {@@ -16,31 +14,40 @@ type Post = {body: string;};-// `cache` is a React 18 feature that allows you to cache a function for the lifetime of a request.-// this means getPosts() will only be called once per page build, even though we may call it multiple times-// when rendering the page.-export const getPosts = cache(async () => {- const postsDir = path.join(process.cwd(), 'content/blog/')- const posts = await fs.readdir(postsDir);+const GITHUB_CONTENT_TOKEN = process.env.GITHUB_CONTENT_TOKEN;+const GITHUB_CONTENT_REPO = process.env.GITHUB_CONTENT_REPO;++export const getPostsRemote = cache(async () => {+ const apiUrl = `https://api.github.com/repos/${GITHUB_CONTENT_REPO}/contents/content/blog`;+ const headers = {+ Authorization: `Bearer ${GITHUB_CONTENT_TOKEN}`,+ Accept: "application/vnd.github+json",+ "X-GitHub-Api-Version": "2022-11-28",+ };++ const response = await fetch(apiUrl, { headers });+ const data = await response.json();return Promise.all(- posts- .filter((file) => path.extname(file) === ".mdx")- .map(async (file) => {- const filePath = path.join(process.cwd(), `content/blog/${file}`)- const postContent = await fs.readFile(filePath, "utf8");- const { data, content } = matter(postContent);+ data+ .filter((file: any) => file.name.endsWith(".mdx"))+ .map(async (file: any) => {+ const contentResponse = await fetch(file.url, { headers });+ const fileDetails = await contentResponse.json();+ const content = fileDetails.content; // Getting the base64 encoded content+ const decodedContent = Buffer.from(content, "base64").toString("utf8"); // Decoding the base64 content+ const { data, content: fileContent } = matter(decodedContent);if (data.published === false) {return null;}- return { data, body: content } as Post;+ return { data, body: fileContent } as Post;}),);});export async function getPost(slug: string) {- const posts = await getPosts();- return posts.find((post) => post?.data.slug === slug);+ const postsRemote = await getPostsRemote();+ return postsRemote.find((post) => post?.data.slug === slug);}