examples/functions/prettier-format-code/README.md
Without enforced code formatting, code blocks in content can be inconsistently rendered.
This Sanity Function formats code blocks with Prettier when a document is published and code blocks have been modified. The formatter uses the language value of the code block to determine the parser.
post document type containing:
content field containing code blocksfilter and projection configurationThis function expects your schema to include a post document type with:
content array of block type fieldscode block field using the Code Input pluginExample schema definition:
import {defineType, defineArrayMember} from 'sanity'
export const post = defineType({
name: 'post',
title: 'Post',
type: 'document',
fields: [
{
name: 'content',
title: 'Content',
type: 'array',
of: [
defineArrayMember({type: 'block'}),
defineArrayMember({type: 'code'}),
// Add other block types as needed
],
},
// Add other fields as needed
],
})
When a post type document is published and code blocks have been modified, the function automatically:
post type documentscontent field is defined and contains code blocks (using filter)code blocks with Prettier using the language value of the code block to determine the parserResult: Code blocks are formatted with Prettier and saved back to the document.
Run this if you haven't initialized blueprints:
npx sanity blueprints init
You'll be prompted to select your organization and Sanity studio.
Then run:
npx sanity blueprints add function --example prettier-format-code
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
// ...all other settings
resources: [
//...all other functions
defineDocumentFunction({
name: 'prettier-format-code',
event: {
on: ['create', 'update'],
filter:
'_type == "post" && (delta::changedAny(content[_type == "code"]) || (delta::operation() == "create" && defined(content[_type == "code"])))',
projection: '{_id, content}',
},
}),
],
})
npm install
You can test the telegram-notify function locally using the Sanity CLI before deploying:
Test with the included sample document:
npx sanity functions test telegram-notify --file document.json
Start the development server for interactive testing:
npx sanity functions dev
Test with your own document data:
npx sanity functions test prettier-format-code --data '{
"_id": "505d8c4e-93b0-436d-8c34-ca0acf96a0b4",
"_type": "post",
"content": [
{
"_key": "7343c346d8cd",
"_type": "code",
"code": "const pets ={\n schnauzers: ['heidi', \n 'kokos'],\n}\n",
"language": "typescript"
}
]
}'
Capture a real document from your dataset:
# Export a real document for testing
npx sanity documents get "your-post-id" > document.json
# Test with the real document
npx sanity functions test prettier-format-code --file document.json
Once you've tested your function locally and are satisfied with its behavior, you can deploy it to production.
Important: Make sure you have the Deploy Studio permission for your Sanity project before attempting to deploy.
From your project root, run:
npx sanity blueprints deploy
This command will:
After deployment, you can verify your function is active by:
npx sanity functions logs prettier-format-code