Ken Jones Pizza
Designer who spends most of his time coding
Generate content changes for a piece of content
defineField({
name: 'suggestedChanges',
title: 'Suggested Changes',
type: 'text',
readOnly: () => true, // This is optional
description: 'Content suggestions will be automatically generated when you publish a post',
}),import {createClient} from '@sanity/client'
import {documentEventHandler} from '@sanity/functions'
export const handler = documentEventHandler(async ({context, event}) => {
const {data} = event
const {local} = context // local is true when running locally
const client = createClient({
...context.clientOptions,
apiVersion: 'vX',
useCdn: false,
})
const brandsWritingStyleGuide = `
# Brand Style Guide
## Voice and Tone
Keep the language casual, clear, and friendly. Speak directly to readers as "you."
- **Casual yet Professional:** Informal enough to feel personal but always trustworthy.
- **Inclusive and Welcoming:** Assume readers are curious but not experts.
- **Light-hearted and Enthusiastic:** Keep enthusiasm genuine.
## Language and Vocabulary
- **Jargon:** Use sparingly. Always briefly explain technical terms.
- **Examples:** Offer relatable, real-life analogies or scenarios.
- **Tone:** Optimistic, helpful, and down-to-earth.
## Headlines and Subheadings
- **Clear and Engaging:** Capture interest without clickbait.
- **Friendly and Informative:** Indicate clearly what readers will learn.
## Formatting and Structure
- **Short Paragraphs:** 2–3 sentences maximum for readability.
- **Bullet Points and Lists:** Use liberally to break down complex ideas clearly.
`
try {
const result = await client.agent.action.generate({
noWrite: local ? true : false, // if local is true, we don't want to write to the document, just return the result for logging
instructionParams: {
content: {
type: 'field',
path: 'content',
},
brandsWritingStyleGuide: {
type: 'constant',
value: brandsWritingStyleGuide,
},
},
instruction: `Examine the $content. Using the $brandsWritingStyleGuide, list specific changes that the author should make to improve the content. Start each suggestion with a dash and be concise.`,
target: {
path: 'suggestedChanges',
},
conditionalPaths: {
defaultReadOnly: false,
},
documentId: data._id,
schemaId: '_.schemas.default',
forcePublishedWrite: true,
})
console.log(
local
? 'Generated content suggestions (LOCAL TEST MODE - Content Lake not updated):'
: 'Generated content suggestions:',
result.suggestedChanges,
)
} catch (error) {
console.error('Error occurred during content suggestions generation:', error)
}
})
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
type: 'sanity.function.document',
name: 'brand-voice-validator',
src: './functions/brand-voice-validator',
memory: 2,
timeout: 60,
event: {
on: ['create', 'update'],
filter:
"_type == 'post' && defined(content) && (delta::changedAny(content) || delta::operation() == 'create')",
projection: '{_id}',
},
}),
],
})This Sanity Function addresses a critical content management challenge: ensuring brand consistency across your content library. Content creators often struggle to maintain alignment with brand guidelines, leading to inconsistent voice and missed opportunities for optimization across your editorial workflow.
Quick Start
View full instructions and source code.
Initialize blueprints if you haven't already: npx sanity blueprints init
Then: npx sanity blueprints add function --example brand-voice-validator
Then deploy: npx sanity blueprints deploy
How It Works
When a content editor publishes a new blog post, the function automatically:
suggestedChanges field in the documentKey Benefits
Technical Implementation
The function uses Sanity's AI Agent Actions to analyze portable text content against comprehensive brand guidelines. It's built with:
Perfect For
The function works with any Sanity project and can be customized to match your specific brand guidelines and content requirements.
Designer who spends most of his time coding
Automatically track when content was first published with a timestamp that sets once and never overwrites, providing reliable publication history for analytics and editorial workflows.
Go to First Published Timestamp FunctionAI-powered automatic tagging for Sanity blog posts that analyzes content to generate 3 relevant tags, maintaining consistency by reusing existing tags from your content library.
Go to Automatically tag blog postsOn document publish, send a Slack notification so your team is informed
Go to Notify your team via Slack on publishCreate, preview, and send Klaviyo campaigns without ever leaving Sanity Studio"
Go to Klaviyo campaigns without leaving your Studio