EmDash 是专为 Astro 构建的 CMS。它通过数据库支持的内容、精致的管理界面和 WordPress 风格的功能(菜单、小部件、分类法)扩展您的 Astro 站点,同时保留您期望的开发者体验。
您对 Astro 的所有了解仍然适用。EmDash 在您现有的 Astro 工作流程之上添加内容管理。
EmDash 添加了什么
EmDash 提供了基于文件的 Astro 站点所缺少的内容管理功能:
| 功能 | 描述 |
|---|---|
| 管理界面 | 在 /_emdash/admin 的完整 WYSIWYG 编辑界面 |
| 数据库存储 | 内容存储在 SQLite、libSQL、Cloudflare D1 或 PostgreSQL 中 |
| 媒体库 | 上传、组织和提供图像和文件 |
| 导航菜单 | 具有嵌套功能的拖放菜单管理 |
| 小部件区域 | 动态侧边栏和页脚区域 |
| 站点设置 | 全局配置(标题、徽标、社交链接) |
| 分类法 | 类别、标签和自定义分类法 |
| 预览系统 | 草稿内容的签名预览 URL |
| 修订版本 | 内容版本历史 |
Astro 集合 vs EmDash
Astro 的 astro:content 集合基于文件,在构建时解析。EmDash 集合基于数据库,在运行时解析。
| Astro 集合 | EmDash 集合 | |
|---|---|---|
| 存储 | src/content/ 中的 Markdown/MDX 文件 | SQL 数据库(SQLite、libSQL、D1 或 Postgres) |
| 编辑 | 代码编辑器 | 管理界面 |
| 内容格式 | 带有 frontmatter 的 Markdown | Portable Text(结构化 JSON) |
| 更新 | 需要重新构建 | 即时(SSR) |
| 架构 | content.config.ts 中的 Zod | 在管理后台定义,存储在数据库中 |
| 最适合 | 开发者管理的内容 | 编辑者管理的内容 |
一起使用
Astro 集合和 EmDash 可以共存。将 Astro 集合用于开发者内容(文档、变更日志),将 EmDash 用于编辑者内容(博客文章、页面):
---
import { getCollection } from "astro:content";
import { getEmDashCollection } from "emdash";
// Developer-managed docs from files
const docs = await getCollection("docs");
// Editor-managed posts from database
const { entries: posts } = await getEmDashCollection("posts", {
status: "published",
limit: 5,
});
---
配置
EmDash 需要两个配置文件。
Astro 集成
以下配置在服务器输出模式下将 EmDash 注册为 Astro 集成:
import { defineConfig } from "astro/config";
import emdash, { local } from "emdash/astro";
import { sqlite } from "emdash/db";
export default defineConfig({
output: "server", // Required for EmDash
integrations: [
emdash({
database: sqlite({ url: "file:./data.db" }),
storage: local({
directory: "./uploads",
baseUrl: "/_emdash/api/media/file",
}),
}),
],
});
实时集合加载器
以下文件将 EmDash 注册为实时内容源:
import { defineLiveCollection } from "astro:content";
import { emdashLoader } from "emdash/runtime";
export const collections = {
_emdash: defineLiveCollection({
loader: emdashLoader(),
}),
};
_emdash 集合在内部路由到您的内容类型(帖子、页面、产品)。
查询内容
EmDash 提供遵循 Astro 实时内容集合模式的查询函数,返回 { entries, error } 或 { entry, error }:
EmDash
import { getEmDashCollection, getEmDashEntry } from "emdash";
// Get all published posts - returns { entries, error }
const { entries: posts } = await getEmDashCollection("posts", {
status: "published",
});
// Get a single post by slug - returns { entry, error, isPreview }
const { entry: post } = await getEmDashEntry("posts", "my-post");
Astro
import { getCollection, getEntry } from "astro:content";
// Get all blog entries
const posts = await getCollection("blog");
// Get a single entry by slug
const post = await getEntry("blog", "my-post"); 过滤选项
getEmDashCollection 支持 Astro 的 getCollection 不提供的过滤:
const { entries: posts } = await getEmDashCollection("posts", {
status: "published", // draft | published | archived
limit: 10, // max results
where: { category: "news" }, // taxonomy filter
});
渲染内容
EmDash 将富文本存储为 Portable Text,一种结构化的 JSON 格式。使用 PortableText 组件渲染它:
EmDash
---
import { getEmDashEntry } from "emdash";
import { PortableText } from "emdash/ui";
const { slug } = Astro.params;
const { entry: post } = await getEmDashEntry("posts", slug);
if (!post) {
return Astro.redirect("/404");
}
---
<article>
<h1>{post.data.title}</h1>
<PortableText value={post.data.content} />
</article> Astro
---
import { getEntry, render } from "astro:content";
const { slug } = Astro.params;
const post = await getEntry("blog", slug);
const { Content } = await render(post);
---
<article>
<h1>{post.data.title}</h1>
<Content />
</article> 动态功能
EmDash 为 Astro 内容层中不存在的 WordPress 风格功能提供 API。
导航菜单
以下布局按位置获取菜单并使用嵌套项目渲染它:
---
import { getMenu } from "emdash";
const primaryMenu = await getMenu("primary");
---
{primaryMenu && (
<nav>
<ul>
{primaryMenu.items.map(item => (
<li>
<a href={item.url}>{item.label}</a>
{item.children.length > 0 && (
<ul>
{item.children.map(child => (
<li><a href={child.url}>{child.label}</a></li>
))}
</ul>
)}
</li>
))}
</ul>
</nav>
)}
小部件区域
以下布局获取小部件区域并渲染每个小部件:
---
import { getWidgetArea } from "emdash";
import { PortableText } from "emdash/ui";
const sidebar = await getWidgetArea("sidebar");
---
{sidebar && sidebar.widgets.length > 0 && (
<aside>
{sidebar.widgets.map(widget => (
<div class="widget">
{widget.title && <h3>{widget.title}</h3>}
{widget.type === "content" && widget.content && (
<PortableText value={widget.content} />
)}
</div>
))}
</aside>
)}
站点设置
以下组件读取全局站点设置并渲染徽标或标题:
---
import { getSiteSettings, getSiteSetting } from "emdash";
const settings = await getSiteSettings();
// Or fetch individual values:
const title = await getSiteSetting("title");
---
<header>
{settings.logo ? (
<img src={settings.logo.url} alt={settings.title} />
) : (
<span>{settings.title}</span>
)}
{settings.tagline && <p>{settings.tagline}</p>}
</header>
插件
使用添加钩子、存储、设置和管理界面的插件扩展 EmDash:
import emdash from "emdash/astro";
import seoPlugin from "@emdash-cms/plugin-seo";
export default defineConfig({
integrations: [
emdash({
// ...
plugins: [seoPlugin({ generateSitemap: true })],
}),
],
});
使用 definePlugin 创建自定义插件:
import { definePlugin } from "emdash";
export default definePlugin({
id: "analytics",
version: "1.0.0",
capabilities: ["content:read"],
hooks: {
"content:afterSave": async (event, ctx) => {
ctx.log.info("Content saved", { id: event.content.id });
},
},
admin: {
settingsSchema: {
trackingId: { type: "string", label: "Tracking ID" },
},
},
});
服务器渲染
EmDash 站点在 SSR 模式下运行,因此内容在运行时提供,更改会立即显示。
对于使用 getStaticPaths 的静态页面,内容在构建时获取:
---
import { getEmDashCollection, getEmDashEntry } from "emdash";
export async function getStaticPaths() {
const { entries: posts } = await getEmDashCollection("posts", {
status: "published",
});
return posts.map((post) => ({
params: { slug: post.data.slug },
}));
}
const { slug } = Astro.params;
const { entry: post } = await getEmDashEntry("posts", slug);
---
对于动态页面,设置 prerender = false 以在每个请求上获取内容:
---
export const prerender = false;
import { getEmDashEntry } from "emdash";
const { slug } = Astro.params;
const { entry: post, error } = await getEmDashEntry("posts", slug);
if (error) {
return new Response("Server error", { status: 500 });
}
if (!post) {
return new Response(null, { status: 404 });
}
---
下一步
Getting Started
创建您的第一个 EmDash 站点,5 分钟内完成。
Querying Content
Create a Blog
构建完整的博客,包含类别和标签。
Deploy to Cloudflare
将您的站点部署到生产环境在 Workers 上。