小工具区域是模板中可由管理员放置内容块的命名区域。可用于侧边栏、页脚列、促销横幅,或任何需要编辑者在不改代码的情况下控制的区块。
查询小工具区域
使用 getWidgetArea() 按名称获取小工具区域:
---
import { getWidgetArea } from "emdash";
const sidebar = await getWidgetArea("sidebar");
---
{sidebar && sidebar.widgets.length > 0 && (
<aside class="sidebar">
{sidebar.widgets.map(widget => (
<div class="widget">
{widget.title && <h3>{widget.title}</h3>}
<!-- 渲染小工具内容 -->
</div>
))}
</aside>
)}
若小工具区域不存在,函数返回 null。
小工具区域结构
小工具区域包含元数据与一组小工具:
interface WidgetArea {
id: string;
name: string; // 唯一标识(如 "sidebar"、"footer-1")
label: string; // 显示名称(如「主侧边栏」)
description?: string;
widgets: Widget[];
}
interface Widget {
id: string;
type: "content" | "menu" | "component";
title?: string;
// 按类型区分的字段
content?: PortableTextBlock[]; // 内容类小工具
menuName?: string; // 菜单类小工具
componentId?: string; // 组件类小工具
componentProps?: Record<string, unknown>;
}
小工具类型
EmDash 支持三种小工具类型:
内容小工具
以 Portable Text 存储的富文本。使用 PortableText 组件渲染:
---
import { PortableText } from "emdash/ui";
---
{widget.type === "content" && widget.content && (
<div class="widget-content">
<PortableText value={widget.content} />
</div>
)}
菜单小工具
在小工具区域内显示导航菜单:
---
import { getMenu } from "emdash";
const menu = widget.menuName ? await getMenu(widget.menuName) : null;
---
{widget.type === "menu" && menu && (
<nav class="widget-nav">
<ul>
{menu.items.map(item => (
<li><a href={item.url}>{item.label}</a></li>
))}
</ul>
</nav>
)}
组件小工具
渲染已注册的组件并传入可配置属性。EmDash 内置以下核心组件:
| 组件 ID | 说明 | Props |
|---|---|---|
core:recent-posts | 最近文章列表 | count, showThumbnails, showDate |
core:categories | 分类列表 | showCount, hierarchical |
core:tags | 标签云 | showCount, limit |
core:search | 搜索表单 | placeholder |
core:archives | 按月/年归档 | type, limit |
渲染小工具
创建一个可复用的小工具渲染组件:
---
import { PortableText } from "emdash/ui";
import { getMenu } from "emdash";
import type { Widget } from "emdash";
import RecentPosts from "./widgets/RecentPosts.astro";
import Categories from "./widgets/Categories.astro";
import TagCloud from "./widgets/TagCloud.astro";
import SearchForm from "./widgets/SearchForm.astro";
import Archives from "./widgets/Archives.astro";
interface Props {
widget: Widget;
}
const { widget } = Astro.props;
const componentMap: Record<string, any> = {
"core:recent-posts": RecentPosts,
"core:categories": Categories,
"core:tags": TagCloud,
"core:search": SearchForm,
"core:archives": Archives,
};
const menu = widget.type === "menu" && widget.menuName
? await getMenu(widget.menuName)
: null;
---
<div class="widget">
{widget.title && <h3 class="widget-title">{widget.title}</h3>}
{widget.type === "content" && widget.content && (
<div class="widget-content">
<PortableText value={widget.content} />
</div>
)}
{widget.type === "menu" && menu && (
<nav class="widget-menu">
<ul>
{menu.items.map(item => (
<li><a href={item.url}>{item.label}</a></li>
))}
</ul>
</nav>
)}
{widget.type === "component" && widget.componentId && componentMap[widget.componentId] && (
<Fragment>
{(() => {
const Component = componentMap[widget.componentId!];
return <Component {...widget.componentProps} />;
})()}
</Fragment>
)}
</div>
示例小工具组件
最近文章小工具
---
import { getEmDashCollection } from "emdash";
interface Props {
count?: number;
showThumbnails?: boolean;
showDate?: boolean;
}
const { count = 5, showThumbnails = false, showDate = true } = Astro.props;
const { entries: posts } = await getEmDashCollection("posts", {
limit: count,
orderBy: { publishedAt: "desc" },
});
---
<ul class="recent-posts">
{posts.map(post => (
<li>
{showThumbnails && post.data.featured_image && (
<img src={post.data.featured_image} alt="" class="thumbnail" />
)}
<a href={`/posts/${post.slug}`}>{post.data.title}</a>
{showDate && post.data.publishedAt && (
<time datetime={post.data.publishedAt.toISOString()}>
{post.data.publishedAt.toLocaleDateString()}
</time>
)}
</li>
))}
</ul>
搜索小工具
---
interface Props {
placeholder?: string;
}
const { placeholder = "搜索…" } = Astro.props;
---
<form action="/search" method="get" class="search-form">
<input
type="search"
name="q"
placeholder={placeholder}
aria-label="搜索"
/>
<button type="submit">搜索</button>
</form>
在布局中使用小工具区域
以下为带侧边栏小工具区域的博客布局示例:
---
import { getWidgetArea } from "emdash";
import WidgetRenderer from "../components/WidgetRenderer.astro";
const sidebar = await getWidgetArea("sidebar");
---
<div class="layout">
<main class="content">
<slot />
</main>
{sidebar && sidebar.widgets.length > 0 && (
<aside class="sidebar">
{sidebar.widgets.map(widget => (
<WidgetRenderer widget={widget} />
))}
</aside>
)}
</div>
<style>
.layout {
display: grid;
grid-template-columns: 1fr 300px;
gap: 2rem;
}
@media (max-width: 768px) {
.layout {
grid-template-columns: 1fr;
}
}
</style>
列出所有小工具区域
使用 getWidgetAreas() 获取所有已填充小工具的区域:
import { getWidgetAreas } from "emdash";
const areas = await getWidgetAreas();
// 返回所有区域及其小工具
创建小工具区域
在管理后台 /_emdash/admin/widgets 创建,或使用管理 API:
POST /_emdash/api/widget-areas
Content-Type: application/json
{
"name": "footer-1",
"label": "页脚第 1 列",
"description": "页脚第一列"
}
添加内容小工具:
POST /_emdash/api/widget-areas/footer-1/widgets
Content-Type: application/json
{
"type": "content",
"title": "关于我们",
"content": [
{
"_type": "block",
"style": "normal",
"children": [{ "_type": "span", "text": "欢迎访问本站。" }]
}
]
}
添加组件小工具:
POST /_emdash/api/widget-areas/sidebar/widgets
Content-Type: application/json
{
"type": "component",
"title": "最近文章",
"componentId": "core:recent-posts",
"componentProps": { "count": 5, "showDate": true }
}
API 参考
getWidgetArea(name)
按名称获取小工具区域及其全部小工具。
参数:
name— 小工具区域的唯一标识(字符串)
返回: Promise<WidgetArea | null>
getWidgetAreas()
列出所有小工具区域及其小工具。
返回: Promise<WidgetArea[]>
getWidgetComponents()
列出管理后台可用的小工具组件定义。
返回: WidgetComponentDef[]