summaryrefslogtreecommitdiff
path: root/src/lib/utils/posts.ts
blob: d0eca687992cf9e39986ef2abf5ebef164b015fe (plain)
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
import type { Component } from 'svelte';

export interface PostMetadata {
  title: string;
  date: string;
  description: string;
  tags?: string[];
}

export interface Post extends PostMetadata {
  slug: string;
}

export interface PostModule {
  default: Component;
  metadata: PostMetadata;
}

const postLoaders = import.meta.glob<PostModule>('/src/posts/*.svx');

function getSlugFromPath(path: string): string {
  return path.split('/').pop()?.replace('.svx', '') ?? '';
}

export function getPostSlugs(): string[] {
  return Object.keys(postLoaders)
    .map((path) => getSlugFromPath(path))
    .sort();
}

export async function getPost(slug: string): Promise<PostModule | undefined> {
  const path = `/src/posts/${slug}.svx`;
  const loader = postLoaders[path];

  if (!loader) {
    return undefined;
  }

  return loader();
}