Seed 檔案是初始化 EmDash 網站的 JSON 文件。它們定義集合、欄位、分類法、選單、重新導向、小工具區域、網站設定和可選的範例內容。
根結構
Seed 檔案具有以下頂層結構:
{
"$schema": "https://emdashcms.com/seed.schema.json",
"version": "1",
"meta": {},
"settings": {},
"collections": [],
"taxonomies": [],
"bylines": [],
"menus": [],
"redirects": [],
"widgetAreas": [],
"sections": [],
"content": {}
}
| 欄位 | 類型 | 必需 | 描述 |
|---|---|---|---|
$schema | string | 否 | 用於編輯器驗證的 JSON 架構 URL |
version | "1" | 是 | Seed 格式版本 |
meta | object | 否 | 關於 seed 的中繼資料 |
settings | object | 否 | 網站設定 |
collections | array | 否 | 集合定義 |
taxonomies | array | 否 | 分類法定義 |
bylines | array | 否 | 署名檔案定義 |
menus | array | 否 | 導覽選單 |
redirects | array | 否 | 重新導向規則 |
widgetAreas | array | 否 | 小工具區域定義 |
sections | array | 否 | 可重複使用的內容區塊 |
content | object | 否 | 範例內容項目 |
Meta
meta 物件包含有關 seed 的可選描述性中繼資料:
{
"meta": {
"name": "Blog Starter",
"description": "A simple blog with posts, pages, and categories",
"author": "EmDash"
}
}
Settings
settings 物件包含網站範圍的設定值:
{
"settings": {
"title": "My Site",
"tagline": "A modern CMS",
"postsPerPage": 10,
"dateFormat": "MMMM d, yyyy"
}
}
設定套用於帶有 site: 前綴的 options 資料表。設定精靈將從 seed 檔案(如果提供)預填 title 和 tagline,允許使用者在初始設定期間覆寫它們。
Collections
每個集合定義在資料庫中建立一個內容類型:
{
"collections": [
{
"slug": "posts",
"label": "Posts",
"labelSingular": "Post",
"description": "Blog posts",
"icon": "file-text",
"supports": ["drafts", "revisions"],
"fields": [
{
"slug": "title",
"label": "Title",
"type": "string",
"required": true
},
{
"slug": "content",
"label": "Content",
"type": "portableText"
},
{
"slug": "featured_image",
"label": "Featured Image",
"type": "image"
}
]
}
]
}
集合屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
slug | string | 是 | URL 安全識別碼(小寫、底線) |
label | string | 是 | 複數顯示名稱 |
labelSingular | string | 否 | 單數顯示名稱 |
description | string | 否 | 管理介面描述 |
icon | string | 否 | Lucide 圖示名稱 |
supports | array | 否 | 功能:"drafts"、"revisions" |
fields | array | 是 | 欄位定義 |
欄位屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
slug | string | 是 | 欄位名稱(小寫、底線) |
label | string | 是 | 顯示名稱 |
type | string | 是 | 欄位類型 |
required | boolean | 否 | 驗證:欄位必須有值 |
unique | boolean | 否 | 驗證:值必須唯一 |
defaultValue | any | 否 | 新項目的預設值 |
validation | object | 否 | 其他驗證規則 |
widget | string | 否 | 管理介面小工具覆寫 |
options | object | 否 | 小工具特定設定 |
欄位類型
| 類型 | 描述 | 儲存為 |
|---|---|---|
string | 短文字 | TEXT |
text | 長文字(文字區域) | TEXT |
number | 數值 | REAL |
integer | 整數 | INTEGER |
boolean | 真/假 | INTEGER |
date | 日期值 | TEXT (ISO 8601) |
datetime | 日期和時間 | TEXT (ISO 8601) |
email | 電子郵件地址 | TEXT |
url | URL | TEXT |
slug | URL 安全字串 | TEXT |
portableText | 富文字內容 | JSON |
image | 圖像引用 | JSON |
file | 檔案引用 | JSON |
json | 任意 JSON | JSON |
reference | 對另一個項目的引用 | TEXT |
Taxonomies
分類法是內容的分類系統:
{
"taxonomies": [
{
"name": "category",
"label": "Categories",
"labelSingular": "Category",
"hierarchical": true,
"collections": ["posts"],
"terms": [
{ "slug": "news", "label": "News" },
{ "slug": "tutorials", "label": "Tutorials" },
{
"slug": "advanced",
"label": "Advanced Tutorials",
"parent": "tutorials"
}
]
},
{
"name": "tag",
"label": "Tags",
"labelSingular": "Tag",
"hierarchical": false,
"collections": ["posts"]
}
]
}
分類法屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
name | string | 是 | 唯一識別碼 |
label | string | 是 | 複數顯示名稱 |
labelSingular | string | 否 | 單數顯示名稱 |
hierarchical | boolean | 是 | 允許巢狀術語(類別)或扁平(標籤) |
collections | array | 是 | 此分類法適用的集合 |
terms | array | 否 | 預定義術語 |
術語屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
slug | string | 是 | URL 安全識別碼 |
label | string | 是 | 顯示名稱 |
description | string | 否 | 術語描述 |
parent | string | 否 | 父術語 slug(僅階層式) |
Menus
menus 陣列定義可從管理員編輯的導覽選單:
{
"menus": [
{
"name": "primary",
"label": "Primary Navigation",
"items": [
{ "type": "custom", "label": "Home", "url": "/" },
{ "type": "page", "ref": "about" },
{ "type": "custom", "label": "Blog", "url": "/posts" },
{
"type": "custom",
"label": "External",
"url": "https://example.com",
"target": "_blank"
}
]
}
]
}
選單項目類型
| 類型 | 描述 | 必需欄位 |
|---|---|---|
custom | 自訂 URL | url |
page | 連結到頁面項目 | ref |
post | 連結到文章項目 | ref |
taxonomy | 連結到分類法封存 | ref、collection |
collection | 連結到集合封存 | collection |
選單項目屬性
| 屬性 | 類型 | 描述 |
|---|---|---|
type | string | 項目類型(見上文) |
label | string | 顯示文字(頁面/文章引用自動產生) |
url | string | 自訂 URL(用於 custom 類型) |
ref | string | seed 中的內容 ID(用於 page/post 類型) |
collection | string | 集合 slug |
target | string | 新視窗使用 "_blank" |
titleAttr | string | HTML title 屬性 |
cssClasses | string | 自訂 CSS 類別 |
children | array | 巢狀選單項目 |
Bylines
署名檔案與所有權(author_id)分開。定義一次可重複使用的署名身分,然後從內容項目引用它們。
{
"bylines": [
{
"id": "editorial",
"slug": "emdash-editorial",
"displayName": "EmDash Editorial"
},
{
"id": "guest",
"slug": "guest-contributor",
"displayName": "Guest Contributor",
"isGuest": true
}
]
}
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
id | string | 是 | content[].bylines 使用的 seed 本地 ID |
slug | string | 是 | URL 安全的署名 slug |
displayName | string | 是 | 範本和 API 中顯示的名稱 |
bio | string | 否 | 可選的檔案簡介 |
websiteUrl | string | 否 | 可選的網站 URL |
isGuest | boolean | 否 | 將署名標記為訪客檔案 |
Redirects
redirects 陣列定義在遷移後保留舊版 URL 的重新導向規則:
{
"redirects": [
{ "source": "/old-about", "destination": "/about" },
{ "source": "/legacy-feed", "destination": "/rss.xml", "type": 308 },
{
"source": "/category/news",
"destination": "/categories/news",
"groupName": "migration"
}
]
}
重新導向屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
source | string | 是 | 來源路徑(必須以 / 開頭) |
destination | string | 是 | 目標路徑(必須以 / 開頭) |
type | number | 否 | HTTP 狀態:301、302、307 或 308 |
enabled | boolean | 否 | 重新導向是否處於作用中狀態(預設:true) |
groupName | string | 否 | 用於管理篩選/搜尋的可選分組標籤 |
Widget Areas
widgetAreas 陣列定義可設定的內容區域:
{
"widgetAreas": [
{
"name": "sidebar",
"label": "Main Sidebar",
"description": "Appears on blog posts and pages",
"widgets": [
{
"type": "component",
"title": "Recent Posts",
"componentId": "core:recent-posts",
"props": { "count": 5 }
},
{
"type": "menu",
"title": "Quick Links",
"menuName": "footer"
},
{
"type": "content",
"title": "About",
"content": [
{
"_type": "block",
"style": "normal",
"children": [{ "_type": "span", "text": "Welcome to our site!" }]
}
]
}
]
}
]
}
小工具類型
| 類型 | 描述 | 必需欄位 |
|---|---|---|
content | 富文字內容 | content(Portable Text) |
menu | 呈現選單 | menuName |
component | 已註冊的元件 | componentId |
內建元件
| 元件 ID | 描述 |
|---|---|
core:recent-posts | 最近文章清單 |
core:categories | 類別清單 |
core:tags | 標籤雲 |
core:search | 搜尋表單 |
core:archives | 月度封存 |
Sections
Sections 是編輯者透過 /section 斜線命令插入到 Portable Text 欄位中的可重複使用內容區塊:
{
"sections": [
{
"slug": "hero-centered",
"title": "Centered Hero",
"description": "Full-width hero with centered heading and CTA button",
"keywords": ["hero", "banner", "header", "landing"],
"content": [
{
"_type": "block",
"style": "h1",
"children": [{ "_type": "span", "text": "Welcome to Our Site" }]
},
{
"_type": "block",
"children": [
{ "_type": "span", "text": "Your compelling tagline goes here." }
]
}
]
}
]
}
Section 屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
slug | string | 是 | URL 安全識別碼 |
title | string | 是 | 在 section 選擇器中顯示的顯示名稱 |
description | string | 否 | 說明何時使用此 section |
keywords | array | 否 | 用於尋找 section 的搜尋詞彙 |
content | array | 是 | Portable Text 區塊 |
source | string | 否 | "theme"(seed 的預設值)或 "import" |
來自 seed 檔案的 sections 標記為 source: "theme",不能從管理介面刪除。編輯者可以建立自己的 sections(source: "user"),並在編輯內容時插入任何 section 類型。
Content
content 物件包含按集合組織的範例內容項目:
{
"content": {
"posts": [
{
"id": "hello-world",
"slug": "hello-world",
"status": "published",
"bylines": [
{ "byline": "editorial" },
{ "byline": "guest", "roleLabel": "Guest essay" }
],
"data": {
"title": "Hello World",
"content": [
{
"_type": "block",
"style": "normal",
"children": [{ "_type": "span", "text": "Welcome!" }]
}
],
"excerpt": "Your first post."
},
"taxonomies": {
"category": ["news"],
"tag": ["welcome", "first-post"]
}
}
],
"pages": [
{
"id": "about",
"slug": "about",
"status": "published",
"data": {
"title": "About Us",
"content": [
{
"_type": "block",
"style": "normal",
"children": [{ "_type": "span", "text": "About page content." }]
}
]
}
}
]
}
}
內容項目屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
id | string | 是 | 用於引用的 seed 本地 ID |
slug | string | 是 | URL slug |
status | string | 否 | "published" 或 "draft"(預設:"published") |
data | object | 是 | 欄位值 |
bylines | array | 否 | 有序的署名積分(byline、可選 roleLabel) |
taxonomies | object | 否 | 按分類法名稱的術語分配 |
Content References
使用 $ref: 前綴引用其他內容項目:
{
"data": {
"related_posts": ["$ref:another-post", "$ref:third-post"]
}
}
$ref: 前綴在種子期間將 seed ID 解析為資料庫 ID。
Media References
從 URL 包含圖像:
{
"data": {
"featured_image": {
"$media": {
"url": "https://images.unsplash.com/photo-xxx",
"alt": "Description of the image",
"filename": "hero.jpg",
"caption": "Photo by Someone"
}
}
}
}
從 .emdash/media/ 包含本地圖像:
{
"data": {
"featured_image": {
"$media": {
"file": "hero.jpg",
"alt": "Description of the image"
}
}
}
}
媒體屬性
| 屬性 | 類型 | 必需 | 描述 |
|---|---|---|---|
url | string | 是* | 要下載的遠端 URL |
file | string | 是* | .emdash/media/ 中的本地檔案名稱 |
alt | string | 否 | 用於無障礙性的替代文字 |
filename | string | 否 | 覆寫檔案名稱 |
caption | string | 否 | 媒體標題 |
*需要 url 或 file 之一,但不能同時需要兩者。
Applying Seeds Programmatically
對 CLI 工具或腳本使用 seed API:
import { applySeed, validateSeed } from "emdash/seed";
import seedData from "./.emdash/seed.json";
// 首先驗證
const validation = validateSeed(seedData);
if (!validation.valid) {
console.error(validation.errors);
process.exit(1);
}
// 套用 seed
const result = await applySeed(db, seedData, {
includeContent: true,
onConflict: "skip",
storage: myStorage,
baseUrl: "http://localhost:4321",
});
console.log(result);
// {
// collections: { created: 2, skipped: 0 },
// fields: { created: 8, skipped: 0 },
// taxonomies: { created: 2, terms: 5 },
// bylines: { created: 2, skipped: 0 },
// menus: { created: 1, items: 4 },
// redirects: { created: 3, skipped: 0 },
// widgetAreas: { created: 1, widgets: 3 },
// settings: { applied: 3 },
// content: { created: 3, skipped: 0 },
// media: { created: 2, skipped: 0 }
// }
Apply 選項
| 選項 | 類型 | 預設值 | 描述 |
|---|---|---|---|
includeContent | boolean | false | 建立範例內容項目 |
onConflict | string | "skip" | "skip"、"update" 或 "error" |
mediaBasePath | string | — | 本地媒體檔案的基本路徑 |
storage | Storage | — | 用於媒體上傳的儲存配接器 |
baseUrl | string | — | 媒體 URL 的基本 URL |
Idempotency
種子可以安全地執行多次。按實體類型的衝突行為:
| 實體 | 行為 |
|---|---|
| 集合 | 如果 slug 存在則跳過 |
| 欄位 | 如果 collection + slug 存在則跳過 |
| 分類法定義 | 如果名稱存在則跳過 |
| 分類法術語 | 如果名稱 + slug 存在則跳過 |
| 署名檔案 | 如果 slug 存在則跳過 |
| 選單 | 如果名稱存在則跳過 |
| 選單項目 | 全部取代(選單重新建立) |
| 重新導向 | 如果來源存在則跳過 |
| 小工具區域 | 如果名稱存在則跳過 |
| 小工具 | 全部取代(區域重新建立) |
| Section | 如果 slug 存在則跳過 |
| 設定 | 更新(設定旨在變更) |
| 內容 | 如果 slug 在集合中存在則跳過 |
Validation
Seed 檔案在套用之前進行驗證:
import { validateSeed } from "emdash/seed";
const { valid, errors, warnings } = validateSeed(seedData);
if (!valid) {
errors.forEach((e) => console.error(e));
}
warnings.forEach((w) => console.warn(w));
驗證檢查:
- 必需欄位存在
- Slug 對其類型有效(集合和欄位 slug 允許小寫字母、數字和底線;其他 slug 還允許連字號)
- 欄位類型有效
- 引用指向現有內容
- 階層式術語父級存在
- 重新導向路徑是安全的本地 URL
- 重新導向來源是唯一的
- 集合中沒有重複的 slug
CLI Commands
.emdash/seed.json、package.json#emdash.seed 或 seed/seed.json 中的 seed 檔案被內嵌到建置中,並在資料庫為空時的第一個請求時套用。要將現有網站的結構(以及可選的其內容)匯出為 seed 檔案:
# `mkdir -p` 因為在新專案上 .emdash/ 可能還不存在
mkdir -p .emdash
# 將目前結構匯出為 seed 檔案
npx emdash export-seed > .emdash/seed.json
# 匯出包含內容
npx emdash export-seed --with-content > .emdash/seed.json
Next Steps
- Creating Themes — 建立完整主題
- Themes Overview — 主題如何運作