EmDash 支持 16 种字段类型来定义内容模式。每种类型都映射到 SQLite 列类型,并提供相应的管理界面。
概述
下表列出了每种字段类型及其 SQLite 列:
| 类型 | SQLite 列 | 描述 |
|---|---|---|
string | TEXT | 短文本输入 |
text | TEXT | 多行文本 |
url | TEXT | URL 值 |
number | REAL | 十进制数 |
integer | INTEGER | 整数 |
boolean | INTEGER | 真/假 |
datetime | TEXT | 日期和时间 |
select | TEXT | 从选项中单选 |
multiSelect | JSON | 多选 |
portableText | JSON | 富文本内容 |
image | TEXT | 图像引用 |
file | TEXT | 文件引用 |
reference | TEXT | 对另一个条目的引用 |
json | JSON | 任意 JSON 数据 |
slug | TEXT | URL 安全标识符 |
repeater | JSON | 重复字段组 |
文本类型
string
短的单行文本。用于标题、名称和短值。
{
slug: "title",
label: "Title",
type: "string",
required: true,
validation: {
minLength: 1,
maxLength: 200,
},
}
验证选项:
minLength— 最小字符数maxLength— 最大字符数pattern— 要匹配的正则表达式模式
小部件选项:
- 无特定选项
text
多行纯文本。用于描述、摘要和较长的纯文本。
{
slug: "excerpt",
label: "Excerpt",
type: "text",
options: {
rows: 3,
},
}
验证选项:
minLength— 最小字符数maxLength— 最大字符数
小部件选项:
rows— 文本区域的行数(默认:3)
slug
URL 安全标识符。从另一个字段自动生成或手动输入。
{
slug: "slug",
label: "URL Slug",
type: "slug",
required: true,
unique: true,
}
Slug 会自动清理:小写,空格替换为连字符,特殊字符移除。
数字类型
number
十进制数。用于价格、评分和测量值。
{
slug: "price",
label: "Price",
type: "number",
required: true,
validation: {
min: 0,
max: 999999.99,
},
}
验证选项:
min— 最小值max— 最大值
存储为 SQLite REAL(64 位浮点数)。
integer
整数。用于数量、计数和订单值。
{
slug: "quantity",
label: "Quantity",
type: "integer",
defaultValue: 1,
validation: {
min: 0,
max: 1000,
},
}
验证选项:
min— 最小值max— 最大值
存储为 SQLite INTEGER。
boolean
真或假。用于开关和标志。
{
slug: "featured",
label: "Featured",
type: "boolean",
defaultValue: false,
}
存储为 SQLite INTEGER(0 或 1)。
日期和时间
datetime
日期和时间值。以 ISO 8601 格式存储。
{
slug: "publishedAt",
label: "Published At",
type: "datetime",
}
验证选项:
min— 最小日期(ISO 字符串)max— 最大日期(ISO 字符串)
存储格式: 2025-01-24T12:00:00.000Z
选择类型
select
从预定义选项中单选。
{
slug: "status",
label: "Status",
type: "select",
required: true,
defaultValue: "draft",
validation: {
options: ["draft", "published", "archived"],
},
}
验证选项:
options— 允许值的数组(必需)
存储为包含所选值的 TEXT。
multiSelect
从预定义选项中多选。
{
slug: "tags",
label: "Tags",
type: "multiSelect",
validation: {
options: ["news", "tutorial", "review", "opinion"],
},
}
验证选项:
options— 允许值的数组(必需)
存储为 JSON 数组:["news", "tutorial"]
富文本内容
portableText
使用 Portable Text 格式的富文本内容。支持标题、列表、链接、图像和自定义块。
{
slug: "content",
label: "Content",
type: "portableText",
required: true,
}
值存储为 Portable Text 块的 JSON 数组,例如:
[
{
"_type": "block",
"style": "normal",
"children": [{ "_type": "span", "text": "Hello world" }]
}
]
插件可以向编辑器添加自定义块类型(嵌入、小部件等)。这些会出现在斜杠命令菜单中,并在网站上自动呈现。参见 Portable Text 渲染组件。
媒体类型
image
对已上传图像的引用。包括尺寸和替代文本等元数据。
{
slug: "featuredImage",
label: "Featured Image",
type: "image",
options: {
showPreview: true,
},
}
小部件选项:
showPreview— 在管理界面中显示图像预览(默认:true)
值存储为包含媒体引用及其元数据的对象:
{
"id": "01HXK5MZSN...",
"url": "https://cdn.example.com/image.jpg",
"alt": "Description",
"width": 1920,
"height": 1080
}
file
对已上传文件(如文档或 PDF)的引用。
{
slug: "document",
label: "Document",
type: "file",
}
值存储为包含文件引用及其元数据的对象:
{
"id": "01HXK5MZSN...",
"url": "https://cdn.example.com/doc.pdf",
"filename": "report.pdf",
"mimeType": "application/pdf",
"size": 102400
}
关系类型
reference
对另一个内容条目的引用。
{
slug: "author",
label: "Author",
type: "reference",
required: true,
options: {
collection: "authors",
},
}
小部件选项:
collection— 目标集合 slug(必需)allowMultiple— 允许多个引用(默认:false)
单个引用存储为目标条目 ID:
"01HXK5MZSN..."
多个引用存储为条目 ID 数组:
["01HXK5MZSN...", "01HXK6NATS..."]
灵活类型
json
任意 JSON 数据。用于复杂的嵌套结构、第三方集成或没有固定架构的数据。
{
slug: "metadata",
label: "Metadata",
type: "json",
}
原样存储在 SQLite JSON 列中。
字段属性
所有字段都支持这些通用属性:
| 属性 | 类型 | 描述 |
|---|---|---|
slug | string | 唯一标识符(必需) |
label | string | 显示名称(必需) |
type | FieldType | 字段类型(必需) |
required | boolean | 需要值(默认:false) |
unique | boolean | 强制唯一性(默认:false) |
defaultValue | unknown | 新条目的默认值 |
validation | object | 特定类型的验证规则 |
widget | string | 自定义小部件覆盖 |
options | object | 小部件配置 |
sortOrder | number | 管理界面中的显示顺序 |
保留字段 slug
这些 slug 是保留的,不能使用:
idslugstatusauthor_idprimary_byline_idcreated_atupdated_atpublished_atscheduled_atdeleted_atversionlive_revision_iddraft_revision_idtermsbylinesbyline
TypeScript 类型
导入字段类型定义以供编程使用:
import type { FieldType, Field, CreateFieldInput } from "emdash";
const fieldTypes: FieldType[] = [
"string",
"text",
"url",
"number",
"integer",
"boolean",
"datetime",
"select",
"multiSelect",
"portableText",
"image",
"file",
"reference",
"json",
"slug",
"repeater",
];