summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/blog-query.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/src/lib/blog-query.ts')
-rw-r--r--apps/web/src/lib/blog-query.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/apps/web/src/lib/blog-query.ts b/apps/web/src/lib/blog-query.ts
new file mode 100644
index 00000000..71ee7341
--- /dev/null
+++ b/apps/web/src/lib/blog-query.ts
@@ -0,0 +1,70 @@
+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";
+
+const url = process.env.MARBLE_API_URL;
+const key = process.env.MARBLE_WORKSPACE_KEY;
+
+export async function getPosts() {
+ try {
+ const raw = await fetch(`${url}/${key}/posts`);
+ const data: MarblePostList = await raw.json();
+ return data;
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+export async function getTags() {
+ try {
+ const raw = await fetch(`${url}/${key}/tags`);
+ const data: MarbleTagList = await raw.json();
+ return data;
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+export async function getSinglePost(slug: string) {
+ try {
+ const raw = await fetch(`${url}/${key}/posts/${slug}`);
+ const data: MarblePost = await raw.json();
+ return data;
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+export async function getCategories() {
+ try {
+ const raw = await fetch(`${url}/${key}/categories`);
+ const data: MarbleCategoryList = await raw.json();
+ return data;
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+export async function getAuthors() {
+ try {
+ const raw = await fetch(`${url}/${key}/authors`);
+ const data: MarbleAuthorList = await raw.json();
+ return data;
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+export async function processHtmlContent(html: string): Promise<string> {
+ const processor = unified()
+ .use(rehypeParse, { fragment: true })
+ .use(rehypeSlug)
+ .use(rehypeAutolinkHeadings, { behavior: "append" })
+ .use(rehypeStringify);
+
+ const file = await processor.process(html);
+ return String(file);
+} \ No newline at end of file