Skip to content

Commit 49cdf1d

Browse files
authored
[fix] JSON 데이터를 public 경로로 수정 & 코드 반영 (#153)
1 parent 785dc4b commit 49cdf1d

6 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/scripts/breadcrumb.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import translations from '../data/breadcrumb.json';
2-
31
/**
42
* Breadcrumb 아이템의 세그먼트 데이터 구조
53
* - name: 세그먼트 이름
@@ -27,18 +25,36 @@ interface TranslationData {
2725
[key: string]: SegmentData;
2826
}
2927

28+
/**
29+
* Breadcrumb 번역 데이터를 로드합니다.
30+
*/
31+
async function loadTranslationData(): Promise<TranslationData> {
32+
try {
33+
const response = await fetch('/data/breadcrumb.json');
34+
if (!response.ok) {
35+
throw new Error('Failed to load breadcrumb translation data');
36+
}
37+
return await response.json();
38+
} catch (error) {
39+
console.error('Error loading breadcrumb translation data:', error);
40+
// 기본값 반환 (빈 객체)
41+
return {};
42+
}
43+
}
44+
3045
/**
3146
* 경로 세그먼트를 번역하고 링크 가능 여부를 확인합니다.
3247
* 계층적 구조를 고려하여 현재 경로에서 세그먼트를 찾습니다.
3348
* @param segments 전체 경로 세그먼트 배열
3449
* @param currentIndex 현재 처리 중인 세그먼트의 인덱스(깊이를 알기 위함)
50+
* @param translationData 번역 데이터
3551
* @returns 해당 세그먼트 데이터 또는 null (데이터가 없는 경우)
3652
*/
3753
function getSegmentData(
3854
segments: string[],
39-
currentIndex: number
55+
currentIndex: number,
56+
translationData: TranslationData
4057
): SegmentData | null {
41-
const translationData = translations as TranslationData;
4258
let current = translationData[segments[0]];
4359

4460
if (!current) {
@@ -59,21 +75,22 @@ function getSegmentData(
5975
/**
6076
* 현재 hash를 기반으로 breadcrumb 아이템들을 생성합니다.
6177
*/
62-
function generateBreadcrumbItems(): BreadcrumbItem[] {
78+
async function generateBreadcrumbItems(): Promise<BreadcrumbItem[]> {
6379
const hash = window.location.hash.slice(1); // # 제거
6480

6581
if (!hash || hash === '/') {
6682
return [{ name: '홈', path: '/', linkable: true }];
6783
}
6884

85+
const translationData = await loadTranslationData();
6986
const pathSegments = hash.split('/').filter((segment) => segment !== '');
7087
const breadcrumbItems: BreadcrumbItem[] = [
7188
{ name: '홈', path: '/', linkable: true },
7289
];
7390

7491
let currentPath = '';
7592
pathSegments.forEach((segment, index) => {
76-
const segmentData = getSegmentData(pathSegments, index);
93+
const segmentData = getSegmentData(pathSegments, index, translationData);
7794
currentPath += `/${segment}`;
7895

7996
// translations 데이터에 없는 세그먼트는 breadcrumb에 추가하지 않음
@@ -131,12 +148,12 @@ function removePreviousBreadcrumb(): void {
131148
/**
132149
* Breadcrumb을 생성하고 div#content의 첫 번째 자식으로 추가합니다.
133150
*/
134-
export function initializeBreadcrumb(): void {
151+
export async function initializeBreadcrumb(): Promise<void> {
135152
const contentDiv = document.getElementById('content')!;
136153

137154
removePreviousBreadcrumb();
138155

139-
const breadcrumbItems = generateBreadcrumbItems();
156+
const breadcrumbItems = await generateBreadcrumbItems();
140157
const breadcrumbElement = createBreadcrumbElement(breadcrumbItems);
141158

142159
contentDiv.insertBefore(breadcrumbElement, contentDiv.firstChild);

src/scripts/components/nav-component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class NavComponent extends HTMLElement {
3434
this.currentRoute = this.getCurrentRoute();
3535

3636
try {
37-
const response = await fetch(`/src/data/nav/${this.currentRoute}.json`);
37+
const response = await fetch(`/data/nav/${this.currentRoute}.json`);
3838
if (!response.ok) {
3939
throw new Error(
4040
`Failed to load navigation data for ${this.currentRoute}`
@@ -44,7 +44,7 @@ export default class NavComponent extends HTMLElement {
4444
} catch (error) {
4545
console.error('Error loading navigation data:', error);
4646
// 기본값으로 get-started 데이터 반환
47-
const fallbackResponse = await fetch('/src/data/nav/get-started.json');
47+
const fallbackResponse = await fetch('/data/nav/get-started.json');
4848
return await fallbackResponse.json();
4949
}
5050
}

src/scripts/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ document.addEventListener('DOMContentLoaded', async () => {
1111
try {
1212
await initializeMarkdownLoader();
1313
initializeTableContents();
14-
initializeBreadcrumb();
14+
await initializeBreadcrumb();
1515
} catch (error) {
1616
console.error('❌ main.ts: DOMContentLoaded : Markdown 로드 실패!', error);
1717
// 실제 다른 작업이 필요
@@ -22,7 +22,7 @@ window.addEventListener('hashchange', async () => {
2222
try {
2323
await initializeMarkdownLoader();
2424
initializeTableContents();
25-
initializeBreadcrumb();
25+
await initializeBreadcrumb();
2626
window.scrollTo(0, 0); // 페이지 이동 시 최상단으로 스크롤 이동
2727
} catch (error) {
2828
console.error('❌ main.ts: hashchange : Markdown 로드 실패!', error);

0 commit comments

Comments
 (0)