This Friday: Hear from Sanity + Vercel experts on AI and better personalization in e-commerce

Converting Sanity Block Content to HTML with Anchor Tags for JSON-LD

1 replyLast updated: Nov 29, 2025

Hi - trying to generate rich results using JSON-LD. The problem I'm facing is that the content from the CMS comes in Block format. I would need to parse it to HTML (no need for classnames). Most importantly I need to keep anchor tags in the parsed result. I see
block-content-to-html
does that but examples are limited. Anyone with experience on this?
AI Update

Based on the documentation, here's how to convert Sanity block content to HTML while preserving anchor tags:

Use @portabletext/to-html (Modern Approach)

The @sanity/block-content-to-html package you mentioned is deprecated. The modern replacement is @portabletext/to-html.

Here's how to use it and preserve links:

import {toHTML} from '@portabletext/to-html'

const portableText = [/* your block content from Sanity */]

const html = toHTML(portableText, {
  components: {
    marks: {
      link: ({value, children}) => {
        const href = value?.href || ''
        const target = value?.blank ? ' target="_blank" rel="noopener"' : ''
        return `<a href="${href}"${target}>${children}</a>`
      }
    }
  }
})

If you're using mark annotations for internal links, you'll need to join the reference data in your GROQ query first:

*[_type == "post"]{
  ...,
  body[]{
    ...,
    markDefs[]{
      ...,
      _type == "internalLink" => {
        "slug": @.reference->slug
      }
    }
  }
}

Then serialize with a custom component:

const html = toHTML(portableText, {
  components: {
    marks: {
      internalLink: ({value, children}) => {
        const slug = value?.slug?.current || ''
        return `<a href="/${slug}">${children}</a>`
      },
      link: ({value, children}) => {
        return `<a href="${value?.href || ''}">${children}</a>`
      }
    }
  }
})

Key Points

Check out the full documentation on presenting Portable Text for more examples and the @portabletext/to-html GitHub repo for detailed usage.

Show original thread
1 reply

Was this answer helpful?

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.

Related contributions