-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
135 lines (113 loc) · 3.55 KB
/
utils.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import fs from "fs";
import path from "path";
export type Metadata = {
title: string;
publishedAt: string;
summary: string;
image?: string;
slug?: string;
cover?: string;
};
function parseFrontmatter(fileContent: string) {
let frontmatterRegex = /---\s*([\s\S]*?)\s*---/;
let match = frontmatterRegex.exec(fileContent);
let frontMatterBlock = match![1];
let content = fileContent.replace(frontmatterRegex, "").trim();
let frontMatterLines = frontMatterBlock.trim().split("\n");
let metadata: Partial<Metadata> = {};
frontMatterLines.forEach((line) => {
let [key, ...valueArr] = line.split(": ");
let value = valueArr.join(": ").trim();
value = value.replace(/^['"](.*)['"]$/, "$1"); // Remove quotes
metadata[key.trim() as keyof Metadata] = value;
});
return { metadata: metadata as Metadata, content };
}
function getMDXFiles(dir: string) {
let files: string[] = [];
// Recursively traverse through directories
function traverseDirectory(currentDir: string) {
let dirEntries = fs.readdirSync(currentDir, { withFileTypes: true });
dirEntries.forEach((entry) => {
let entryPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
// Recursively search in subdirectories
traverseDirectory(entryPath);
} else if (entry.isFile() && path.extname(entry.name) === ".mdx") {
files.push(entryPath);
}
});
}
traverseDirectory(dir);
return files;
}
function readMDXFile(filePath: string) {
let rawContent = fs.readFileSync(filePath, "utf-8");
return parseFrontmatter(rawContent);
}
function getMDXData(dir: string) {
let mdxFiles = getMDXFiles(dir);
return mdxFiles.map((file) => {
let { metadata, content } = readMDXFile(file);
let slug = path.basename(file, path.extname(file));
return {
metadata,
slug,
content,
};
});
}
export function getBlogPosts() {
return getMDXData(path.join(process.cwd(), "content", "posts")).sort(
(a, b) =>
new Date(b.metadata.publishedAt).getTime() -
new Date(a.metadata.publishedAt).getTime(),
);
}
export function getProjects() {
// Modified to search recursively in /app/projects and subdirectories
return getMDXData(path.join(process.cwd(), "content", "projects"));
}
export function getPages() {
// Modified to search recursively in /app/projects and subdirectories
return getMDXData(path.join(process.cwd(), "content", "pages"));
}
export function formatDate(date: string, includeRelative = false) {
let currentDate = new Date();
if (!date.includes("T")) {
date = `${date}T00:00:00`;
}
let targetDate = new Date(date);
let yearsAgo = currentDate.getFullYear() - targetDate.getFullYear();
let monthsAgo = currentDate.getMonth() - targetDate.getMonth();
let daysAgo = currentDate.getDate() - targetDate.getDate();
let formattedDate = "";
if (yearsAgo > 0) {
formattedDate = `${yearsAgo}y ago`;
} else if (monthsAgo > 0) {
formattedDate = `${monthsAgo}mo ago`;
} else if (daysAgo > 0) {
formattedDate = `${daysAgo}d ago`;
} else {
formattedDate = "Today";
}
let fullDate = targetDate.toLocaleString("en-us", {
month: "long",
day: "numeric",
year: "numeric",
});
if (!includeRelative) {
return fullDate;
}
return `${fullDate} (${formattedDate})`;
}
export const groupPostsByYear = (posts: any[]) => {
return posts.reduce((acc, post) => {
const year: number = new Date(post.metadata.publishedAt).getFullYear();
if (!acc[year]) {
acc[year] = [];
}
acc[year].push(post);
return acc;
}, {});
};