Sanity CMS Skill
Publishes documents to Sanity CMS via the Content API. Works with any schema.
References
- - API patterns (upload, mutate, query): INLINECODE0
- Portable Text body format: INLINECODE1
Workflow
1. Understand the target schema
Four ways to get schema info — try in this order:
A — File in workspace: User drops schema at a known path (e.g. sanity-schemas/blogPost.ts). Read it directly.
B — Pasted in chat: User pastes the schema TypeScript/JS. Read it from the conversation.
C — Remote URL: User shares a GitHub raw URL or similar. Fetch with web_fetch.
D — API introspection (no file needed): Query the dataset directly — see references/api.md (Schema Introspection section). Use array::unique(*[]._type) to discover document types, then fetch one sample document to infer field names and shapes. Works without any schema file at all.
Once you have the schema (by any method):
- - For
array fields with type: 'block', use Portable Text format — see INLINECODE8 - For
reference fields (categories, authors, tags), query existing documents via GROQ — see INLINECODE10
2. Format the document JSON
Build a JSON object matching the schema:
- - Omit
_id — the script generates a drafts. prefixed UUID automatically - Omit the cover image field — the script injects it after uploading
- All required fields must be present and within any validation constraints
- Save to a logical path (e.g.
brain/projects/<slug>-sanity.json or similar)
3. Run the publish script
The script is at scripts/publish_draft.sh within this skill directory. Resolve the path relative to where the skill is installed (e.g. ~/.openclaw/skills/sanity-cms/scripts/publish_draft.sh or <workspace>/skills/sanity-cms/scripts/publish_draft.sh).
CODEBLOCK0
Optional env overrides:
| Var | Default | Purpose |
|---|
| INLINECODE17 | INLINECODE18 | Target dataset |
| INLINECODE19 |
coverImage | Field name for cover image |
|
DRAFT_PREFIX |
true | Set to
false to publish immediately |
Example with overrides:
CODEBLOCK1
4. Confirm and report
After the script prints a draft ID, report to the user:
- - The draft document ID
- A link to Sanity Studio (ask if unsure of the Studio URL)
- Which fields, if any, still need manual attention in Studio (e.g. unpopulated references)
Env Vars
| Var | Description |
|---|
| INLINECODE24 | Sanity project ID |
| INLINECODE25 |
Write-enabled token (Editor or higher) |
|
SANITY_DATASET | Dataset (optional, default:
production) |
Tips
- - Always create drafts first (
DRAFT_PREFIX=true) unless the user explicitly wants to publish live - If a schema has
reference fields, query for the referenced document IDs via GROQ before building the JSON — see INLINECODE30 - The script works with any document type: blog posts, pages, products, authors, etc.
- Cover image upload is optional — omit the second argument if the schema has no image field
Sanity CMS 技能
通过内容 API 将文档发布到 Sanity CMS。适用于任何 schema。
参考
- - API 模式(上传、变更、查询):references/api.md
- Portable Text 正文格式:references/portable-text.md
工作流程
1. 理解目标 schema
四种获取 schema 信息的方式——按此顺序尝试:
A — 工作区中的文件: 用户在已知路径下提供 schema(例如 sanity-schemas/blogPost.ts)。直接读取。
B — 粘贴在聊天中: 用户粘贴 schema 的 TypeScript/JS 代码。从对话中读取。
C — 远程 URL: 用户分享 GitHub raw URL 或类似链接。使用 web_fetch 获取。
D — API 内省(无需文件): 直接查询数据集——参见 references/api.md(Schema 内省部分)。使用 array::unique(*[]._type) 发现文档类型,然后获取一个示例文档以推断字段名称和结构。无需任何 schema 文件即可工作。
一旦你通过任何方法获得了 schema:
- - 对于 type: block 的 array 字段,使用 Portable Text 格式——参见 references/portable-text.md
- 对于 reference 字段(分类、作者、标签),通过 GROQ 查询现有文档——参见 references/api.md
2. 格式化文档 JSON
构建与 schema 匹配的 JSON 对象:
- - 省略 _id——脚本会自动生成一个以 drafts. 为前缀的 UUID
- 省略封面图片字段——脚本在上传后注入
- 所有必填字段必须存在并符合任何验证约束
- 保存到逻辑路径(例如 brain/projects/-sanity.json 或类似路径)
3. 运行发布脚本
脚本位于此技能目录内的 scripts/publishdraft.sh。根据技能安装位置解析路径(例如 ~/.openclaw/skills/sanity-cms/scripts/publishdraft.sh 或 /skills/sanity-cms/scripts/publish_draft.sh)。
bash
bash /scripts/publish_draft.sh \
path/to/document.json \
path/to/cover-image.png # 可选
可选的环境变量覆盖:
| 变量 | 默认值 | 用途 |
|---|
| SANITYDATASET | production | 目标数据集 |
| COVERIMAGE_FIELD |
coverImage | 封面图片的字段名 |
| DRAFT_PREFIX | true | 设为 false 可立即发布 |
带覆盖的示例:
bash
SANITYDATASET=staging COVERIMAGE_FIELD=mainImage \
bash /scripts/publish_draft.sh doc.json cover.jpg
4. 确认和报告
脚本打印草稿 ID 后,向用户报告:
- - 草稿文档 ID
- Sanity Studio 的链接(如果不确定 Studio URL 请询问)
- 哪些字段(如果有)仍需要在 Studio 中手动处理(例如未填充的引用)
环境变量
| 变量 | 描述 |
|---|
| SANITYPROJECTID | Sanity 项目 ID |
| SANITYAPITOKEN |
具有写入权限的令牌(Editor 或更高权限) |
| SANITY_DATASET | 数据集(可选,默认:production) |
提示
- - 始终先创建草稿(DRAFT_PREFIX=true),除非用户明确要求直接发布
- 如果 schema 包含 reference 字段,在构建 JSON 之前通过 GROQ 查询引用的文档 ID——参见 references/api.md
- 脚本适用于任何文档类型:博客文章、页面、产品、作者等
- 封面图片上传是可选的——如果 schema 没有图片字段,省略第二个参数