Hook 参考

本页内容

钩子允许插件在内容、媒体、邮件、评论和页面生命周期的特定节点拦截和修改 EmDash 行为。

Hook 概览

Hook触发时机可修改内容排他
content:beforeSave内容保存之前内容数据
content:afterSave内容保存之后
content:beforeDelete内容删除之前可取消
content:afterDelete内容删除之后
media:beforeUpload文件上传之前文件元数据
media:afterUpload文件上传之后
cron定时任务触发
email:beforeSend邮件发送之前消息,可取消
email:deliver通过传输层发送邮件
email:afterSend邮件成功发送之后
comment:beforeCreate评论存储之前评论,可取消
comment:moderate决定评论审核状态状态
comment:afterCreate评论存储之后
comment:afterModerate管理员更改评论状态之后
page:metadata渲染公共页面 head贡献标签
page:fragments渲染公共页面 body注入脚本
plugin:install插件首次安装时
plugin:activate插件启用时
plugin:deactivate插件禁用时
plugin:uninstall插件移除时

内容钩子

content:beforeSave

在内容保存到数据库之前运行。用于验证、转换或丰富内容。

import { definePlugin } from "emdash";

export default definePlugin({
	id: "my-plugin",
	version: "1.0.0",
	hooks: {
		"content:beforeSave": async (event, ctx) => {
			const { content, collection, isNew } = event;

			if (isNew) {
				content.createdBy = "system";
			}
			content.modifiedAt = new Date().toISOString();

			return content;
		},
	},
});

Event

interface ContentHookEvent {
	content: Record<string, unknown>; // 内容数据
	collection: string; // 集合 slug
	isNew: boolean; // 创建为 true,更新为 false
}

返回值

  • 返回修改后的内容对象以应用更改
  • 返回 void 不做修改直接传递

content:afterSave

在内容保存之后运行。用于通知、缓存失效或外部同步等副作用。

hooks: {
  "content:afterSave": async (event, ctx) => {
    const { content, collection, isNew } = event;

    if (collection === "posts" && content.status === "published") {
      await ctx.http?.fetch("https://api.example.com/notify", {
        method: "POST",
        body: JSON.stringify({ postId: content.id }),
      });
    }
  },
}

返回值

无需返回值。

content:beforeDelete

在内容删除之前运行。用于验证删除操作或阻止删除。

hooks: {
  "content:beforeDelete": async (event, ctx) => {
    const { id, collection } = event;

    const item = await ctx.content?.get(collection, id);
    if (item?.data.protected) {
      return false; // 取消删除
    }

    return true;
  },
}

Event

interface ContentDeleteEvent {
	id: string; // 条目 ID
	collection: string; // 集合 slug
}

返回值

  • 返回 false 取消删除
  • 返回 truevoid 允许删除

content:afterDelete

在内容删除之后运行。用于清理任务。

hooks: {
  "content:afterDelete": async (event, ctx) => {
    const { id, collection } = event;

    await ctx.storage.relatedItems.delete(`${collection}:${id}`);
  },
}

媒体钩子

media:beforeUpload

在文件上传之前运行。用于验证、重命名或拒绝文件。

hooks: {
  "media:beforeUpload": async (event, ctx) => {
    const { file } = event;

    if (file.size > 10 * 1024 * 1024) {
      throw new Error("File too large");
    }

    return {
      name: `${Date.now()}-${file.name}`,
      type: file.type,
      size: file.size,
    };
  },
}

Event

interface MediaUploadEvent {
	file: {
		name: string; // 原始文件名
		type: string; // MIME 类型
		size: number; // 大小(字节)
	};
}

返回值

  • 返回修改后的文件元数据以应用更改
  • 返回 void 不做修改直接传递
  • 抛出异常以拒绝上传

media:afterUpload

在文件上传之后运行。用于处理、缩略图生成或元数据提取。

hooks: {
  "media:afterUpload": async (event, ctx) => {
    const { media } = event;

    if (media.mimeType.startsWith("image/")) {
      await ctx.kv.set(`media:${media.id}:analyzed`, {
        processedAt: new Date().toISOString(),
      });
    }
  },
}

Event

interface MediaAfterUploadEvent {
	media: {
		id: string;
		filename: string;
		mimeType: string;
		size: number | null;
		url: string;
		createdAt: string;
	};
}

生命周期钩子

plugin:install

插件首次安装时运行。用于初始设置、创建存储集合或填充数据。

hooks: {
  "plugin:install": async (event, ctx) => {
    await ctx.kv.set("settings:enabled", true);
    await ctx.kv.set("settings:threshold", 100);

    ctx.log.info("Plugin installed successfully");
  },
}

plugin:activate

插件启用时运行(安装后或重新启用)。

hooks: {
  "plugin:activate": async (event, ctx) => {
    ctx.log.info("Plugin activated");
  },
}

plugin:deactivate

插件禁用时运行。

hooks: {
  "plugin:deactivate": async (event, ctx) => {
    ctx.log.info("Plugin deactivated");
  },
}

plugin:uninstall

插件移除时运行。用于清理。

hooks: {
  "plugin:uninstall": async (event, ctx) => {
    const { deleteData } = event;

    if (deleteData) {
      const items = await ctx.kv.list("settings:");
      for (const { key } of items) {
        await ctx.kv.delete(key);
      }
    }

    ctx.log.info("Plugin uninstalled");
  },
}

Event

interface UninstallEvent {
	deleteData: boolean; // 用户选择删除数据
}

Cron 钩子

cron

定时任务执行时触发。通过 ctx.cron.schedule() 调度任务。

hooks: {
  "cron": async (event, ctx) => {
    if (event.name === "daily-sync") {
      const data = await ctx.http?.fetch("https://api.example.com/data");
      ctx.log.info("Sync complete");
    }
  },
}

Event

interface CronEvent {
	name: string;
	data?: Record<string, unknown>;
	scheduledAt: string;
}

邮件钩子

邮件钩子形成一个管道:email:beforeSendemail:deliveremail:afterSend

email:beforeSend

能力: email:intercept

在投递前运行的中间件钩子。转换消息或取消投递。

hooks: {
  "email:beforeSend": async (event, ctx) => {
    return {
      ...event.message,
      text: event.message.text + "\n\n—Sent from My Site",
    };

    // 或返回 false 取消投递
  },
}

Event

interface EmailBeforeSendEvent {
	message: { to: string; subject: string; text: string; html?: string };
	source: string;
}

返回值

  • 返回修改后的消息进行转换
  • 返回 false 取消投递
  • 返回 void 不做修改直接传递

email:deliver

能力: email:provide | 排他:

传输提供者。只能有一个插件负责投递邮件。负责通过邮件服务实际发送消息。

hooks: {
  "email:deliver": {
    exclusive: true,
    handler: async (event, ctx) => {
      await sendViaSES(event.message);
    },
  },
}

email:afterSend

能力: email:intercept

成功投递后的即发即弃钩子。错误会被记录但不会传播。

hooks: {
  "email:afterSend": async (event, ctx) => {
    await ctx.kv.set(`email:log:${Date.now()}`, {
      to: event.message.to,
      subject: event.message.subject,
    });
  },
}

评论钩子

评论钩子形成一个管道:comment:beforeCreatecomment:moderatecomment:afterCreatecomment:afterModerate 钩子在管理员更改评论状态时单独触发。

comment:beforeCreate

能力: read:users

评论存储之前的中间件钩子。丰富、验证或拒绝评论。

hooks: {
  "comment:beforeCreate": async (event, ctx) => {
    if (event.comment.body.includes("http")) {
      return false;
    }
  },
}

Event

interface CommentBeforeCreateEvent {
	comment: {
		collection: string;
		contentId: string;
		parentId: string | null;
		authorName: string;
		authorEmail: string;
		authorUserId: string | null;
		body: string;
		ipHash: string | null;
		userAgent: string | null;
	};
	metadata: Record<string, unknown>;
}

返回值

  • 返回修改后的事件进行转换
  • 返回 false 拒绝
  • 返回 void 直接传递

comment:moderate

能力: read:users | 排他:

决定评论是通过、待审核还是垃圾评论。只有一个审核提供者处于活动状态。

hooks: {
  "comment:moderate": {
    exclusive: true,
    handler: async (event, ctx) => {
      const score = await checkSpam(event.comment);
      return {
        status: score > 0.8 ? "spam" : score > 0.5 ? "pending" : "approved",
        reason: `Spam score: ${score}`,
      };
    },
  },
}

Event

interface CommentModerateEvent {
	comment: { /* 与 beforeCreate 相同 */ };
	metadata: Record<string, unknown>;
	collectionSettings: {
		commentsEnabled: boolean;
		commentsModeration: "all" | "first_time" | "none";
		commentsClosedAfterDays: number;
		commentsAutoApproveUsers: boolean;
	};
	priorApprovedCount: number;
}

返回值

{ status: "approved" | "pending" | "spam"; reason?: string }

comment:afterCreate

能力: read:users

评论存储后的即发即弃钩子。用于通知。

hooks: {
  "comment:afterCreate": async (event, ctx) => {
    if (event.comment.status === "approved") {
      await ctx.email?.send({
        to: event.contentAuthor?.email,
        subject: `New comment on "${event.content.title}"`,
        text: `${event.comment.authorName} commented: ${event.comment.body}`,
      });
    }
  },
}

comment:afterModerate

能力: read:users

管理员手动更改评论状态时的即发即弃钩子。

Event

interface CommentAfterModerateEvent {
	comment: { id: string; /* ... */ };
	previousStatus: string;
	newStatus: string;
	moderator: { id: string; name: string | null };
}

页面钩子

页面钩子在渲染公共页面时运行。它们允许插件注入元数据和脚本。

page:metadata

能力: page:inject

向页面 head 贡献 meta 标签、Open Graph 属性、JSON-LD 结构化数据或 link 标签。

hooks: {
  "page:metadata": async (event, ctx) => {
    return [
      { kind: "meta", name: "generator", content: "EmDash" },
      { kind: "property", property: "og:site_name", content: event.page.siteName },
      { kind: "jsonld", graph: { "@type": "WebSite", name: event.page.siteName } },
    ];
  },
}

贡献类型

type PageMetadataContribution =
	| { kind: "meta"; name: string; content: string; key?: string }
	| { kind: "property"; property: string; content: string; key?: string }
	| { kind: "link"; rel: string; href: string; hreflang?: string; key?: string }
	| { kind: "jsonld"; id?: string; graph: Record<string, unknown> };

key 字段用于去重——只使用具有给定 key 的最后一个贡献。

page:fragments

能力: page:inject

向页面注入脚本或 HTML。仅对受信任(原生)插件可用。

hooks: {
  "page:fragments": async (event, ctx) => {
    return [
      {
        kind: "external-script",
        placement: "body:end",
        src: "https://analytics.example.com/script.js",
        async: true,
      },
      {
        kind: "inline-script",
        placement: "head",
        code: `window.siteId = "abc123";`,
      },
    ];
  },
}

贡献类型

type PageFragmentContribution =
	| {
			kind: "external-script";
			placement: "head" | "body:start" | "body:end";
			src: string;
			async?: boolean;
			defer?: boolean;
			attributes?: Record<string, string>;
			key?: string;
		}
	| {
			kind: "inline-script";
			placement: "head" | "body:start" | "body:end";
			code: string;
			attributes?: Record<string, string>;
			key?: string;
		}
	| {
			kind: "html";
			placement: "head" | "body:start" | "body:end";
			html: string;
			key?: string;
		};

Hook 配置

钩子接受处理函数或配置对象:

hooks: {
  // 简单处理函数
  "content:afterSave": async (event, ctx) => { ... },

  // 带配置
  "content:beforeSave": {
    priority: 50,        // 越小越先执行(默认:100)
    timeout: 10000,      // 最大执行时间,毫秒(默认:5000)
    dependencies: [],    // 在这些插件之后运行
    errorPolicy: "abort", // "continue" 或 "abort"(默认)
    handler: async (event, ctx) => { ... },
  },
}

配置选项

选项类型默认值描述
prioritynumber100执行顺序(越小越先执行)
timeoutnumber5000最大执行时间(毫秒)
dependenciesstring[][]必须先运行的插件 ID
errorPolicystring"abort""continue" 忽略错误
exclusivebooleanfalse只有一个插件可以作为活动提供者(用于 email:delivercomment:moderate 等提供者模式钩子)

插件上下文

所有钩子接收一个包含插件 API 访问的上下文对象:

interface PluginContext {
	plugin: { id: string; version: string };
	storage: PluginStorage;
	kv: KVAccess;
	content?: ContentAccess;
	media?: MediaAccess;
	http?: HttpAccess;
	log: LogAccess;
	site: { name: string; url: string; locale: string };
	url(path: string): string;
	users?: UserAccess;
	cron?: CronAccess;
	email?: EmailAccess;
}

能力要求和方法详情参见插件概览 — 插件上下文

错误处理

钩子中的错误根据 errorPolicy 进行记录和处理:

  • "abort"(默认)— 停止执行,如适用则回滚事务
  • "continue" — 记录错误并继续到下一个钩子
hooks: {
  "content:beforeSave": {
    errorPolicy: "continue",
    handler: async (event, ctx) => {
      try {
        await ctx.http?.fetch("https://api.example.com/validate");
      } catch (error) {
        ctx.log.warn("Validation service unavailable", error);
      }
    },
  },
}

执行顺序

钩子按以下顺序运行:

  1. priority 升序排列
  2. dependencies 的插件在其依赖之后运行
  3. 相同优先级内,顺序是确定性的但未指定
// 首先运行(priority 10)
{ priority: 10, handler: ... }

// 其次运行(priority 50)
{ priority: 50, handler: ... }

// 最后运行(默认 priority 100)
{ handler: ... }