Skip to content

Commit 87872fb

Browse files
committed
feat: 사이드바 및 URL 경로를 동적으로 생성하도록 수정
1 parent 041af6f commit 87872fb

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

.vitepress/config.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ export default defineConfig({
2222
{ text: "WEB", link: "/posts/web/" },
2323
],
2424
},
25+
{ text: "Projects", link: "/projects/" },
2526
],
2627

2728
sidebar: {
28-
"/posts/": generateSidebar(),
29+
"/posts/": generateSidebar("posts", "/posts"),
30+
"/projects/": generateSidebar("projects", "/projects"),
2931

3032
"/": [
3133
{
@@ -49,6 +51,8 @@ export default defineConfig({
4951
rewrites: {
5052
"pages/posts/index.md": "posts/index.md",
5153
"pages/posts/:slug*": "posts/:slug*",
54+
"pages/projects/index.md": "projects/index.md",
55+
"pages/projects/:slug*": "projects/:slug*",
5256
},
5357

5458
vite: {

.vitepress/plugins/sidebar.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@ interface SidebarItem {
1111
}
1212

1313
/**
14-
* pages/posts 폴더 구조를 기반으로 자동으로 사이드바를 생성하는 함수
14+
* 폴더 구조를 기반으로 자동으로 사이드바를 생성하는 함수
15+
* @param folderPath - pages 폴더 내의 상대 경로 (예: 'posts', 'projects')
16+
* @param urlPath - URL 경로 (예: '/posts', '/projects')
1517
*/
16-
export function generateSidebar(): DefaultTheme.SidebarItem[] {
17-
const postsDir = join(process.cwd(), "pages", "posts");
18+
export function generateSidebar(
19+
folderPath: string = "posts",
20+
urlPath?: string,
21+
): DefaultTheme.SidebarItem[] {
22+
const targetDir = join(process.cwd(), "pages", folderPath);
23+
const finalUrlPath = urlPath || `/${folderPath}`;
1824

1925
try {
20-
// posts 폴더가 존재하지 않으면 빈 사이드바 반환
21-
if (!statSync(postsDir).isDirectory()) {
26+
// 폴더가 존재하지 않으면 빈 사이드바 반환
27+
if (!statSync(targetDir).isDirectory()) {
2228
return [];
2329
}
2430
} catch (error) {
25-
// posts 폴더가 없으면 빈 사이드바 반환
31+
// 폴더가 없으면 빈 사이드바 반환
2632
return [];
2733
}
2834

29-
const sidebarItems = generateSidebarItems(postsDir, "/posts");
35+
const sidebarItems = generateSidebarItems(targetDir, finalUrlPath);
3036
return sidebarItems;
3137
}
3238

@@ -85,7 +91,7 @@ function generateSidebarItems(dir: string, basePath: string): SidebarItem[] {
8591
if (indexFile) {
8692
items.unshift({
8793
text: "개요",
88-
link: basePath === "/posts" ? "/posts/" : basePath,
94+
link: `${basePath}/`,
8995
});
9096
}
9197
} catch (error) {
@@ -123,10 +129,16 @@ function formatTitle(name: string): string {
123129

124130
/**
125131
* 특정 경로에 대한 사이드바만 생성 (다중 사이드바 사용 시)
132+
* @param folderPath - pages 폴더 내의 상대 경로
133+
* @param urlPath - URL 경로
126134
*/
127-
export function generateSidebarForPath(path: string): DefaultTheme.SidebarItem[] {
135+
export function generateSidebarForPath(
136+
folderPath: string,
137+
urlPath?: string,
138+
): DefaultTheme.SidebarItem[] {
128139
const pagesDir = join(process.cwd(), "pages");
129-
const targetDir = join(pagesDir, path);
140+
const targetDir = join(pagesDir, folderPath);
141+
const finalUrlPath = urlPath || `/${folderPath}`;
130142

131143
try {
132144
if (!statSync(targetDir).isDirectory()) {
@@ -136,5 +148,5 @@ export function generateSidebarForPath(path: string): DefaultTheme.SidebarItem[]
136148
return [];
137149
}
138150

139-
return generateSidebarItems(targetDir, `/posts/${path}`);
151+
return generateSidebarItems(targetDir, finalUrlPath);
140152
}

0 commit comments

Comments
 (0)