Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 25 additions & 8 deletions src/scripts/breadcrumb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import translations from '../data/breadcrumb.json';

/**
* Breadcrumb 아이템의 세그먼트 데이터 구조
* - name: 세그먼트 이름
Expand Down Expand Up @@ -27,18 +25,36 @@ interface TranslationData {
[key: string]: SegmentData;
}

/**
* Breadcrumb 번역 데이터를 로드합니다.
*/
async function loadTranslationData(): Promise<TranslationData> {
try {
const response = await fetch('/data/breadcrumb.json');
if (!response.ok) {
throw new Error('Failed to load breadcrumb translation data');
}
return await response.json();
} catch (error) {
console.error('Error loading breadcrumb translation data:', error);
// 기본값 반환 (빈 객체)
return {};
}
}

/**
* 경로 세그먼트를 번역하고 링크 가능 여부를 확인합니다.
* 계층적 구조를 고려하여 현재 경로에서 세그먼트를 찾습니다.
* @param segments 전체 경로 세그먼트 배열
* @param currentIndex 현재 처리 중인 세그먼트의 인덱스(깊이를 알기 위함)
* @param translationData 번역 데이터
* @returns 해당 세그먼트 데이터 또는 null (데이터가 없는 경우)
*/
function getSegmentData(
segments: string[],
currentIndex: number
currentIndex: number,
translationData: TranslationData
): SegmentData | null {
const translationData = translations as TranslationData;
let current = translationData[segments[0]];

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

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

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

let currentPath = '';
pathSegments.forEach((segment, index) => {
const segmentData = getSegmentData(pathSegments, index);
const segmentData = getSegmentData(pathSegments, index, translationData);
currentPath += `/${segment}`;

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

removePreviousBreadcrumb();

const breadcrumbItems = generateBreadcrumbItems();
const breadcrumbItems = await generateBreadcrumbItems();
const breadcrumbElement = createBreadcrumbElement(breadcrumbItems);

contentDiv.insertBefore(breadcrumbElement, contentDiv.firstChild);
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/components/nav-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class NavComponent extends HTMLElement {
this.currentRoute = this.getCurrentRoute();

try {
const response = await fetch(`/src/data/nav/${this.currentRoute}.json`);
const response = await fetch(`/data/nav/${this.currentRoute}.json`);
if (!response.ok) {
throw new Error(
`Failed to load navigation data for ${this.currentRoute}`
Expand All @@ -44,7 +44,7 @@ export default class NavComponent extends HTMLElement {
} catch (error) {
console.error('Error loading navigation data:', error);
// 기본값으로 get-started 데이터 반환
const fallbackResponse = await fetch('/src/data/nav/get-started.json');
const fallbackResponse = await fetch('/data/nav/get-started.json');
return await fallbackResponse.json();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ document.addEventListener('DOMContentLoaded', async () => {
try {
await initializeMarkdownLoader();
initializeTableContents();
initializeBreadcrumb();
await initializeBreadcrumb();
} catch (error) {
console.error('❌ main.ts: DOMContentLoaded : Markdown 로드 실패!', error);
// 실제 다른 작업이 필요
Expand All @@ -22,7 +22,7 @@ window.addEventListener('hashchange', async () => {
try {
await initializeMarkdownLoader();
initializeTableContents();
initializeBreadcrumb();
await initializeBreadcrumb();
window.scrollTo(0, 0); // 페이지 이동 시 최상단으로 스크롤 이동
} catch (error) {
console.error('❌ main.ts: hashchange : Markdown 로드 실패!', error);
Expand Down