How to add links to images in Portable Text with annotations?

13 replies
Last updated: Aug 11, 2022
Question about links. I've setup links similar to this post: https://www.sanity.io/guides/portable-text-internal-and-external-links and that is fine with one drawback, how to add a link to an image? I'm currently importing a wordpress blog and ran into this limitation. Is there a recommended way to link an image that works in the portableText editor with annotations?
AI Update

The limitation you're encountering is a fundamental aspect of how Portable Text works. Annotations (like links) only work on inline text spans, not on block-level objects like images. This is by design in the Portable Text specification.

However, there are a few recommended approaches to achieve image linking:

The most straightforward solution is to extend your image block type with a URL field:

{
  type: 'image',
  fields: [
    {
      name: 'alt',
      type: 'string',
      title: 'Alternative text'
    },
    {
      name: 'link',
      type: 'url',
      title: 'Link URL',
      description: 'URL to navigate to when image is clicked'
    }
  ]
}

Then in your frontend renderer, you'd wrap the image with an anchor tag when the link field is present.

2. Create a custom "linked image" block type

You could create a dedicated block type specifically for linked images:

{
  name: 'linkedImage',
  type: 'object',
  title: 'Linked Image',
  fields: [
    {
      name: 'image',
      type: 'image',
      title: 'Image'
    },
    {
      name: 'link',
      type: 'url',
      title: 'Link URL'
    },
    {
      name: 'openInNewTab',
      type: 'boolean',
      title: 'Open in new tab'
    }
  ]
}

Then add this to your Portable Text of array alongside the regular image block.

3. Use an internal reference instead of URL

If you're migrating from WordPress and want to link images to other content in your system:

{
  type: 'image',
  fields: [
    {
      name: 'linkTo',
      type: 'reference',
      title: 'Link to',
      to: [{type: 'post'}, {type: 'page'}]
    }
  ]
}

Why annotations don't work on images

Annotations in Portable Text are specifically designed for inline content within text blocks (spans). Images are block-level objects that exist at the same level as text blocks in the array structure, not within them. This architectural decision keeps the data model clean and predictable.

The approaches above work well because they store the link metadata directly with the image object, which is the appropriate place for block-level content. When rendering, you have full control over how to handle these links in your frontend code.

Show original thread
13 replies
Would this be an image in your dataset or an image hosted outside of sanity?
I'm importing the images into sanity as I go, using block-tools. So it would be an image I import. Creating ldjson as I go through the html.
I guess I could add a link option on my media types, just wondering if there is a better way.
not really hard to check if the link child is an image in the processing, block-tools is super cool
Ah, got it. I understand. If you're adding the image to your dataset, could you just add it to your array of blocks like in this example? You could then use conditionals in your query to expand the asset on your frontend.
But how would you attach a link, e.g <a href="some external url"><img src="sanity image asset" /></a>?
only thing I can think of is a custom image object with a link option but the portableText UI wouldn't be used to manage the linking. It would have to be set when the image is inserted from the popup
Ok, I think I misunderstood. Is the idea to a) set a link and b) set an image that will represent that link on the frontend?
If so, you can add a field to your image object for that url:
{
  name: 'image',
  type: 'image',
  fields: [
    {
      name: 'link',
      type: 'url',
      options: {
        isHighlighted: true
      }
    }
  ]
}
yeah, the wordpress site I'm imported uses images linked to youtube videos. So I want to maintain that on import.
fair enough, I'll go that route. It was my initial plan but wasn't sure if there was a better way.
thank you
Happy to help!

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.

Was this answer helpful?