Adding a mailto reference in markdown/BlockContent in Sanity.io
Yes, you can add mailto: links in Portable Text (BlockContent)! The issue is that by default, the url field type only validates http and https schemes. To support mailto: links, you need to customize the validation in your block content schema.
Here's how to configure it in your blockContent schema:
export default {
title: 'Block Content',
name: 'blockContent',
type: 'array',
of: [
{
title: 'Block',
type: 'block',
marks: {
decorators: [
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'},
],
annotations: [
{
title: 'URL',
name: 'link',
type: 'object',
fields: [
{
title: 'URL',
name: 'href',
type: 'url',
validation: Rule => Rule.uri({
scheme: ['http', 'https', 'mailto']
})
},
],
},
],
},
},
],
}The key is adding the validation rule to your URL field with Rule.uri() and specifying the allowed schemes. This tells Sanity to accept mailto: links in addition to standard http and https URLs. You can see more details in the URL type documentation.
If you want separate fields for email and web links (which can provide a better editor experience), you could create two different annotation types:
annotations: [
{
title: 'External Link',
name: 'link',
type: 'object',
fields: [
{
title: 'URL',
name: 'href',
type: 'url',
validation: Rule => Rule.uri({
scheme: ['http', 'https']
})
},
],
},
{
title: 'Email Link',
name: 'emailLink',
type: 'object',
fields: [
{
title: 'Email',
name: 'href',
type: 'url',
validation: Rule => Rule.uri({
scheme: ['mailto']
})
},
],
},
]On your frontend, you'll need to render these annotations as links. The annotation data will be available in the markDefs array of your Portable Text blocks, and you can use @portabletext/react (or the appropriate package for your framework) to serialize them into clickable links. The newer @portabletext/react package has replaced the deprecated @sanity/block-content-to-react library.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.