导航菜单

本页内容

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 }>>