summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/blog-query.ts
blob: 6ad8cb6c09d7fb9c567825557134ea78f563c3e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { MarbleAuthorList, MarbleCategoryList, MarblePost, MarblePostList, MarbleTagList } from '@/types/post';
import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeSanitize from "rehype-sanitize";

const url = process.env.NEXT_PUBLIC_MARBLE_API_URLL ?? "https://api.marblecms.com";
const key = process.env.MARBLE_WORKSPACE_KEY ?? "cm6ytuq9x0000i803v0isidst";

async function fetchFromMarble<T>(endpoint: string): Promise<T> {
    try {
      const response = await fetch(`${url}/${key}/${endpoint}`);
      if (!response.ok) {
        throw new Error(`Failed to fetch ${endpoint}: ${response.status} ${response.statusText}`);
      }
      return await response.json() as T;
    } catch (error) {
      console.error(`Error fetching ${endpoint}:`, error);
      throw error;
    }
  }
  
  export async function getPosts() {
    return fetchFromMarble<MarblePostList>('posts');
  }
  
  export async function getTags() {
    return fetchFromMarble<MarbleTagList>('tags');
  }
  
  export async function getSinglePost(slug: string) {
    return fetchFromMarble<MarblePost>(`posts/${slug}`);
  }
  
  export async function getCategories() {
    return fetchFromMarble<MarbleCategoryList>('categories');
  }
  
  export async function getAuthors() {
    return fetchFromMarble<MarbleAuthorList>('authors');
  }

export async function processHtmlContent(html: string): Promise<string> {
	const processor = unified()
        .use(rehypeSanitize)
		.use(rehypeParse, { fragment: true })
		.use(rehypeSlug)
		.use(rehypeAutolinkHeadings, { behavior: "append" })
		.use(rehypeStringify);

	const file = await processor.process(html);
	return String(file);
}