-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPath.ts
More file actions
36 lines (31 loc) · 1.24 KB
/
Copy pathgetPath.ts
File metadata and controls
36 lines (31 loc) · 1.24 KB
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
import { BLOG_PATH } from "@/content.config";
import { slugifyStr } from "./slugify";
/**
* Get full path of a blog post
* @param id - id of the blog post (aka slug)
* @param filePath - the blog post full file location
* @param includeBase - whether to include `/posts` in return value
* @returns blog post path
*/
export function getPath(
id: string,
filePath: string | undefined,
includeBase = true
) {
const pathSegments = filePath
?.replace(BLOG_PATH, "")
.split("/")
.filter(path => path !== "") // remove empty string in the segments ["", "other-path"] <- empty string will be removed
.filter(path => !path.startsWith("_")) // exclude directories start with underscore "_"
.slice(0, -1) // remove the last segment_ file name_ since it's unnecessary
.map(segment => slugifyStr(segment)); // slugify each segment path
const basePath = includeBase ? "/posts" : "";
// Making sure `id` does not contain the directory
const blogId = id.split("/");
const slug = blogId.length > 0 ? blogId.slice(-1) : blogId;
// If not inside the sub-dir, simply return the file path
if (!pathSegments || pathSegments.length < 1) {
return [basePath, slug].join("/");
}
return [basePath, ...pathSegments, slug].join("/");
}