面向 Astro 开发者的 EmDash

本页内容

EmDash 是专为 Astro 打造的 CMS,而不是带 Astro 适配器的通用无头 CMS。它在保留你熟悉的开发体验的同时,为站点加入数据库支撑的内容、精致的管理后台,以及 WordPress 风格的功能(菜单、小工具、分类法)。

你对 Astro 的了解仍然适用。EmDash 增强站点,而不是取代你的工作流。

EmDash 补充的能力

EmDash 提供纯文件型 Astro 站点通常缺少的内容管理功能:

功能说明
Admin UI位于 /_emdash/admin 的完整 WYSIWYG 编辑界面
Database storage内容存放在 SQLite、libSQL 或 Cloudflare D1
Media library上传、整理并提供图片与文件
Navigation menus支持拖拽与层级的菜单管理
Widget areas动态侧边栏与页脚区域
Site settings全局配置(标题、Logo、社交链接)
Taxonomies分类、标签与自定义分类法
Preview system用于草稿的签名预览 URL
Revisions内容版本历史

Astro Collections 与 EmDash

Astro 的 astro:content 集合基于文件,在构建期解析。EmDash 集合由数据库支撑,在运行时解析。

Astro CollectionsEmDash Collections
Storagesrc/content/ 中的 Markdown/MDX 文件SQLite/D1 数据库
Editing代码编辑器Admin UI
Content format带 frontmatter 的 MarkdownPortable Text(结构化 JSON)
Updates需要重新构建即时(SSR)
Schemacontent.config.ts 中的 Zod在管理后台定义并存入数据库
Best for由开发者维护的内容由编辑维护的内容

两者一起用

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 集成

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",
			}),
		}),
	],
});

Live Collections Loader

import { defineLiveCollection } from "astro:content";
import { emdashLoader } from "emdash/runtime";

export const collections = {
	_emdash: defineLiveCollection({
		loader: emdashLoader(),
	}),
};

这会将 EmDash 注册为实时内容源。_emdash 集合在内部路由到你的内容类型(posts、pages、products 等)。

查询内容

EmDash 提供遵循 Astro live content collections 模式的查询函数,返回 { 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 将富文本存为结构化 JSON 格式 Portable Text。用 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>

插件

用可增加钩子、存储、设置与管理 UI 的插件扩展 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: ["read:content"],

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

下一步