本指南带你从安装到发布第一篇帖子,完成第一个 EmDash 站点。
前置要求
- Node.js v22.12.0 或更高版本(不支持奇数版本)
- npm、pnpm 或 yarn
- 代码编辑器(推荐 VS Code)
创建新项目
npm
npm create emdash@latest pnpm
pnpm create emdash@latest yarn
yarn create emdash 按提示为项目命名并完成偏好设置。
启动开发服务器
-
进入项目目录:
cd my-emdash-site -
安装依赖:
npm install -
启动开发服务器:
npm run dev -
在浏览器打开
http://localhost:4321
完成设置向导
首次访问管理后台时,EmDash 的设置向导会引导你完成初始配置:
-
打开
http://localhost:4321/_emdash/admin -
你会被重定向到设置向导。填写:
- Site Title — 站点名称
- Tagline — 简短描述
- Admin Email — 你的邮箱
-
点击 Create Site 注册通行密钥
-
浏览器会提示你使用 Touch ID、Face ID、Windows Hello 或安全密钥创建通行密钥
通行密钥注册成功后,你会登录并跳转到管理仪表盘。
第一篇帖子
-
在管理侧栏的 Content 下点击 Posts。
-
点击 New Post。
-
输入标题,并用富文本编辑器撰写内容。
-
将状态设为 Published,点击 Save。
-
访问站点首页即可看到已发布内容——无需重新构建!
项目结构
EmDash 项目在标准 Astro 结构基础上有少量补充:
my-emdash-site/
├── astro.config.mjs # Astro + EmDash 配置
├── src/
│ ├── live.config.ts # Live Collections 配置
│ ├── pages/
│ │ ├── index.astro # 首页
│ │ └── posts/
│ │ └── [...slug].astro # 动态文章页
│ ├── layouts/
│ │ └── Base.astro # 基础布局
│ └── components/ # 你的 Astro 组件
├── .emdash/
│ ├── seed.json # 模板种子文件
│ └── types.ts # 生成的 TypeScript 类型
└── package.json
配置文件
astro.config.mjs
将 EmDash 配置为 Astro 集成:
import { defineConfig } from "astro/config";
import emdash, { local } from "emdash/astro";
import { sqlite } from "emdash/db";
export default defineConfig({
integrations: [
emdash({
database: sqlite({ url: "file:./data.db" }),
storage: local({
directory: "./uploads",
baseUrl: "/_emdash/api/media/file",
}),
}),
],
});
src/live.config.ts
将 EmDash 接入 Astro 的内容系统:
import { defineLiveCollection } from "astro:content";
import { emdashLoader } from "emdash/runtime";
export const collections = {
_emdash: defineLiveCollection({ loader: emdashLoader() }),
};
环境变量
生产部署需要设置:
# Required for authentication
EMDASH_AUTH_SECRET=your-secret-here
# Optional: for content preview
EMDASH_PREVIEW_SECRET=your-preview-secret
生成安全的认证密钥:
npx emdash auth secret
在页面中查询内容
在 Astro 页面中使用 EmDash 的查询函数。它们遵循 Astro 的 live collections 模式:集合返回 { entries, error },单条返回 { entry, error }:
---
import { getEmDashCollection } from "emdash";
import Base from "../layouts/Base.astro";
const { entries: posts } = await getEmDashCollection("posts");
---
<Base title="Home">
<h1>Latest Posts</h1>
<ul>
{posts.map((post) => (
<li>
<a href={`/posts/${post.slug}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>
查询单条内容:
---
import { getEmDashEntry } from "emdash";
const { slug } = Astro.params;
const { entry: post } = await getEmDashEntry("posts", slug);
if (!post) {
return Astro.redirect("/404");
}
---
<h1>{post.data.title}</h1>
生成 TypeScript 类型
为获得完整类型安全,可根据数据库架构生成类型:
npx emdash types
这会创建 .emdash/types.ts,包含所有集合的接口。编辑器将自动补全字段名并发现类型错误。
下一步
你已经拥有一个可运行的 EmDash 站点。可以继续:
- 核心概念 — 了解 EmDash 的内部机制
- 使用内容 — 查询与渲染内容
- 媒体库 — 上传与管理图片和文件
- 创建博客 — 搭建带分类与标签的完整博客
- 部署到 Cloudflare — 将站点上线生产环境