summaryrefslogtreecommitdiff
path: root/src/lib/utils/posts.server.ts
blob: 9f34c1cf618bc23f27d01d6754bf28460edbce13 (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
import type { Post, PostMetadata } from "./posts";

const postMetadataFiles = import.meta.glob<PostMetadata>("/src/posts/*.svx", {
  eager: true,
  import: "metadata",
});

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

export function getPosts(): Post[] {
  const posts: Post[] = [];

  for (const path in postMetadataFiles) {
    const metadata = postMetadataFiles[path];
    const slug = getSlugFromPath(path);

    posts.push({
      ...metadata,
      slug,
    });
  }

  return posts.sort(
    (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime(),
  );
}