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}/ - 自訂連結 → 原樣使用
呈現巢狀選單
項目可有子項以形成下拉導覽。遞迴呈現 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 }, ...]
主要用於後台介面或偵錯。
建立選單
於後台 /_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 }>>