区块(Sections)是可复用的内容块,编辑者可在任意内容中通过斜杠命令插入。适用于 CTA、客户证言、功能栅格等需要在多页出现的模式。
查询区块
getSection
按 slug 获取单个区块:
import { getSection } from "emdash";
const cta = await getSection("newsletter-cta");
if (cta) {
console.log(cta.title);
console.log(cta.content);
}
getSections
按条件获取多个区块:
import { getSections } from "emdash";
const { items: all } = await getSections();
const { items: themeSections } = await getSections({ source: "theme" });
const { items: results } = await getSections({ search: "newsletter" });
getSections 返回 { items: Section[], nextCursor?: string },遵循常见分页模式。
数据结构
interface Section {
id: string;
slug: string;
title: string;
description?: string;
keywords: string[];
content: PortableTextBlock[];
previewUrl?: string;
source: "theme" | "user" | "import";
themeId?: string;
createdAt: string;
updatedAt: string;
}
来源类型
| 来源 | 说明 |
|---|---|
theme | 在种子文件中定义,由主题管理 |
user | 在后台由编辑者创建 |
import | 从 WordPress 导入(可重用区块) |
在内容中使用
编辑者在富文本编辑器中用 /section 插入:
-
输入
/section(或/pattern、/block、/template) -
搜索或浏览可用区块
-
点击在光标处插入该区块内容
区块的 Portable Text 会复制进文档,因此:
- 之后修改区块不会影响已插入的内容
- 编辑者可自行调整已插入内容
- 内容保持自洽
创建区块
在后台
-
在侧边栏进入 Sections
-
点击 New Section
-
填写 Title、Slug、Description
-
在富文本编辑器中添加正文
-
可选:设置 keywords 便于检索
通过种子文件
在主题种子中包含 sections 数组(示例与英文文档相同)。
WordPress 导入
可重用区块(wp_block)会导入为区块,source 为 "import",Gutenberg 内容转换为 Portable Text。
在代码中渲染
---
import { getSection } from "emdash";
import { PortableText } from "emdash/ui";
const newsletter = await getSection("newsletter-cta");
---
{newsletter && (
<aside class="cta-box">
<PortableText value={newsletter.content} />
</aside>
)}
后台功能
/_emdash/admin/sections 提供网格预览、按标题/关键词搜索、按来源筛选、复制 slug、编辑与删除(主题区块删除时会有提示)。
API 参考
getSection(slug)
参数: slug(string)。返回: Promise<Section | null>
getSections(options?)
参数: source、search。返回: Promise<Section[]>
REST API
与英文文档相同的 GET / POST / PUT / DELETE 路径与示例 JSON。