content/*.mdx → [gray-matter로 파싱] → [fs로 스캔] → content-tree.json → [페이지에서 import] → FileExplorer
packages/utils에 설치했고, MDX 파일 상단의 frontmatter(YAML
메타데이터)를 파싱하는 데 사용한다.
---
title: React 시작하기
date: "2025-03-05"
tags: [react, frontend]
description: React의 기본...
---
{ data, content }로 분리해줌content/ 디렉토리를 재귀 스캔해서 트리 구조 JSON을 만드는 함수다.
fs.readdirSync()로 현재 디렉토리의 엔트리(파일/폴더)를 읽음children을 채움.mdx/.md면 matter(raw)로
frontmatter 파싱localeCompare("ko"))const raw = fs.readFileSync(fullPath, "utf-8"); // MDX 파일 전체 읽기
const { data } = matter(raw); // frontmatter → JS 객체
// data = { title: "React 시작하기", date: "2025-03-05", tags: [...], ... }
targetPath = "/frontend/hooks"
→ segments = ["frontend", "hooks"]
→ tree에서 name === "frontend"인 폴더 찾기 → 그 children으로 이동
→ children에서 name === "hooks"인 폴더 찾기 → 그 children 반환
루트(/)면 tree 자체를 반환하고, 경로가 존재하지 않으면 빈 배열 [] 반환.
tsx(TypeScript executor)로 실행되는 빌드 스크립트로, pnpm dev 또는 pnpm build 전에 자동 실행된다.
"generate:content": "tsx scripts/generate-content-tree.ts",
"dev": "pnpm run generate:content && vinext dev",
"build": "pnpm run generate:content && vinext build"
content/ 디렉토리에 scanContentDir() 호출 2. 결과를
src/generated/content-tree.json에 JSON으로 저장 3. 이 JSON은 .gitignore에
포함되어 커밋되지 않음 (매번 빌드 시 생성)import contentTree from "../../generated/content-tree.json"; // 정적 import
// 쿼리스트링에서 현재 경로 추출
const currentPath =
new URLSearchParams(window.location.search).get("path") || "/";
// 해당 경로의 아이템 가져오기
const nodes = getItemsForPath(contentTree, currentPath);
// ContentNode[] → ExplorerItem[] 변환 후 FileExplorer에 전달
const items = toExplorerItems(nodes);
| 구성 요소 | 위치 | 역할 |
|---|---|---|
gray-matter | packages/utils 의존성 | MDX frontmatter → JS 객체 파싱 |
scanContentDir() | content.ts | fs로 디렉토리 재귀 스캔 + frontmatter 파싱 → 트리 생성 |
getItemsForPath() | content.ts | 트리에서 특정 경로의 아이템만 추출 |
generate-content-tree.ts | scripts/ | 빌드 타임에 JSON 생성 |
content-tree.json | src/generated/ (자동 생성) | 정적 JSON, 페이지에서 import |