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 Collections | EmDash Collections | |
|---|---|---|
| Storage | src/content/ 中的 Markdown/MDX 檔案 | SQLite/D1 資料庫 |
| Editing | 程式碼編輯器 | Admin UI |
| Content format | 含 frontmatter 的 Markdown | Portable Text(結構化 JSON) |
| Updates | 需要重新建置 | 即時(SSR) |
| Schema | content.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>
外掛
以可新增 hook、儲存空間、設定與管理 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 });
}
---
下一步
Getting Started
不到 5 分鐘 建立第一個 EmDash 網站。
Querying Content
Create a Blog
Deploy to Cloudflare
在 Workers 上 部署到正式環境。