EmDash 메뉴는 관리 화면에서 관리하는 순서가 있는 링크 목록입니다. 중첩을 지원해 드롭다운을 만들 수 있으며, 페이지, 글, 분류 용어 또는 외부 URL에 연결할 수 있습니다.
메뉴 조회
고유 이름으로 메뉴를 가져오려면 getMenu()를 사용합니다.
---
import { getMenu } from "emdash";
const primaryMenu = await getMenu("primary");
---
{primaryMenu && (
<nav>
<ul>
{primaryMenu.items.map(item => (
<li>
<a href={item.url}>{item.label}</a>
</li>
))}
</ul>
</nav>
)}
해당 이름의 메뉴가 없으면 함수는 null을 반환합니다.
메뉴 구조
메뉴는 메타데이터와 항목 배열을 포함합니다.
interface Menu {
id: string;
name: string; // Unique identifier ("primary", "footer")
label: string; // Display name ("Primary Navigation")
items: MenuItem[];
}
interface MenuItem {
id: string;
label: string;
url: string; // Resolved URL
target?: string; // "_blank" for new window
titleAttr?: string; // HTML title attribute
cssClasses?: string; // Custom CSS classes
children: MenuItem[]; // Nested items for dropdowns
}
URL은 항목 유형에 따라 자동으로 해석됩니다.
- 페이지/글 항목 →
/{collection}/{slug} - 분류 항목 →
/{taxonomy}/{slug} - 컬렉션 항목 →
/{collection}/ - 사용자 지정 링크 → URL 그대로 사용
중첩 메뉴 렌더링
드롭다운 내비게이션을 위해 메뉴 항목에 자식이 있을 수 있습니다. children 배열을 재귀적으로 렌더링해 중첩을 처리합니다.
---
import { getMenu } from "emdash";
import type { MenuItem } from "emdash";
interface Props {
name: string;
}
const menu = await getMenu(Astro.props.name);
---
{menu && (
<nav class="nav">
<ul class="nav-list">
{menu.items.map(item => (
<li class:list={["nav-item", item.cssClasses]}>
<a
href={item.url}
target={item.target}
title={item.titleAttr}
aria-current={Astro.url.pathname === item.url ? "page" : undefined}
>
{item.label}
</a>
{item.children.length > 0 && (
<ul class="submenu">
{item.children.map(child => (
<li>
<a href={child.url} target={child.target}>
{child.label}
</a>
</li>
))}
</ul>
)}
</li>
))}
</ul>
</nav>
)}
메뉴 항목 유형
관리 화면에서는 다섯 가지 메뉴 항목 유형을 지원합니다.
| 유형 | 설명 | URL 해석 |
|---|---|---|
page | 페이지 링크 | /{collection}/{slug} |
post | 글 링크 | /{collection}/{slug} |
taxonomy | 카테고리 또는 태그 | /{taxonomy}/{slug} |
collection | 컬렉션 아카이브 | /{collection}/ |
custom | 외부 또는 임의 URL | 그대로 사용 |
모든 메뉴 나열
getMenus()로 모든 메뉴 정의(항목 없음)를 가져옵니다.
import { getMenus } from "emdash";
const menus = await getMenus();
// Returns: [{ id, name, label }, ...]
주로 관리 UI나 디버깅에 유용합니다.
메뉴 만들기
/_emdash/admin/menus 관리 화면에서 만들거나 관리 API를 사용합니다.
POST /_emdash/api/menus
Content-Type: application/json
{
"name": "footer",
"label": "Footer Navigation"
}
메뉴에 항목 추가:
POST /_emdash/api/menus/footer/items
Content-Type: application/json
{
"type": "page",
"referenceCollection": "pages",
"referenceId": "page_privacy",
"label": "Privacy Policy"
}
사용자 지정 외부 링크 추가:
POST /_emdash/api/menus/footer/items
Content-Type: application/json
{
"type": "custom",
"customUrl": "https://github.com/example",
"label": "GitHub",
"target": "_blank"
}
순서 및 중첩 변경
재정렬 엔드포인트로 항목 순서와 부모-자식 관계를 업데이트합니다.
POST /_emdash/api/menus/primary/reorder
Content-Type: application/json
{
"items": [
{ "id": "item_1", "parentId": null, "sortOrder": 0 },
{ "id": "item_2", "parentId": null, "sortOrder": 1 },
{ "id": "item_3", "parentId": "item_2", "sortOrder": 0 }
]
}
이렇게 하면 item_3이 item_2의 자식이 되어 드롭다운이 됩니다.
전체 예시
다음은 기본 내비게이션이 있는 반응형 헤더 예시입니다.
---
import { getMenu, getSiteSettings } from "emdash";
const settings = await getSiteSettings();
const primaryMenu = await getMenu("primary");
---
<html lang="en">
<head>
<title>{settings.title}</title>
</head>
<body>
<header class="header">
<a href="/" class="logo">
{settings.logo ? (
<img src={settings.logo.url} alt={settings.logo.alt || settings.title} />
) : (
settings.title
)}
</a>
{primaryMenu && (
<nav class="main-nav" aria-label="Main navigation">
<ul>
{primaryMenu.items.map(item => (
<li class:list={[item.cssClasses, { "has-children": item.children.length > 0 }]}>
<a
href={item.url}
target={item.target}
aria-current={Astro.url.pathname === item.url ? "page" : undefined}
>
{item.label}
</a>
{item.children.length > 0 && (
<ul class="dropdown">
{item.children.map(child => (
<li>
<a href={child.url} target={child.target}>{child.label}</a>
</li>
))}
</ul>
)}
</li>
))}
</ul>
</nav>
)}
</header>
<main>
<slot />
</main>
</body>
</html>
API 참고
getMenu(name)
이름으로 메뉴를 가져오며 모든 항목과 해석된 URL을 포함합니다.
매개변수:
name— 메뉴의 고유 식별자(문자열)
반환: Promise<Menu | null>
getMenus()
항목 없이 모든 메뉴 정의를 나열합니다.
반환: Promise<Array<{ id: string; name: string; label: string }>>