编写 Hookify 规则
概述
Hookify 规则是带有 YAML 前置元数据的 Markdown 文件,用于定义要监控的模式以及匹配时显示的消息。规则存储在 .claude/hookify.{rule-name}.local.md 文件中。
规则文件格式
基本结构
---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: regex-pattern-here
---
当此规则触发时向 Claude 显示的消息。
可包含 Markdown 格式、警告、建议等内容。
前置元数据字段
| 字段 | 必填 | 值 | 描述 |
|---|---|---|---|
| name | 是 | kebab-case 字符串 | 唯一标识符(动词优先:warn-、block-、require-*) |
| enabled | 是 | true/false | 无需删除即可切换 |
| event | 是 | bash/file/stop/prompt/all | 触发规则的钩子事件 |
| action | 否 | warn/block | warn(默认)显示消息;block 阻止操作 |
| pattern | 是* | 正则表达式字符串 | 要匹配的模式(*或使用 conditions 实现复杂规则) |
高级格式(多条件)
---
name: warn-env-api-keys
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
你正在向 .env 文件中添加 API 密钥。请确保该文件已包含在 .gitignore 中!
按事件划分的条件字段:
- bash:
command - file:
file_path、new_text、old_text、content - prompt:
user_prompt
运算符: regex_match、contains、equals、not_contains、starts_with、ends_with
所有条件必须同时满足才能触发规则。