快速开始

本页内容

本指南带你从安装到发布第一篇帖子,完成第一个 EmDash 站点。

前置要求

  • Node.js v22.12.0 或更高版本(不支持奇数版本)
  • npmpnpmyarn
  • 代码编辑器(推荐 VS Code)

创建新项目

npm

npm create emdash@latest

pnpm

pnpm create emdash@latest

yarn

yarn create emdash

按提示为项目命名并完成偏好设置。

启动开发服务器

  1. 进入项目目录:

    cd my-emdash-site
  2. 安装依赖:

    npm install
  3. 启动开发服务器:

    npm run dev
  4. 在浏览器打开 http://localhost:4321

完成设置向导

首次访问管理后台时,EmDash 的设置向导会引导你完成初始配置:

  1. 打开 http://localhost:4321/_emdash/admin

  2. 你会被重定向到设置向导。填写:

    • Site Title — 站点名称
    • Tagline — 简短描述
    • Admin Email — 你的邮箱
  3. 点击 Create Site 注册通行密钥

  4. 浏览器会提示你使用 Touch ID、Face ID、Windows Hello 或安全密钥创建通行密钥

通行密钥注册成功后,你会登录并跳转到管理仪表盘。

EmDash 管理仪表盘,显示内容概览、最近活动和导航侧栏

第一篇帖子

  1. 在管理侧栏的 Content 下点击 Posts

  2. 点击 New Post

  3. 输入标题,并用富文本编辑器撰写内容。

    EmDash 文章编辑器,含富文本工具栏与发布侧栏
  4. 将状态设为 Published,点击 Save

  5. 访问站点首页即可看到已发布内容——无需重新构建!

项目结构

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 站点。可以继续: