📆 Don't miss our Sanity Showcase virtual meetup on March 20th!

How to Make a Link in a Blog Post Open in a New Tab Instead of the Current Tab

4 replies
Last updated: Feb 9, 2022
How can I make a link in my blog post open in a new tab instead of the current tab? I am using block-content-to-react. I know I need to add
target="blank"
to the anchor tag, but I don't know how to do it using Sanity.
Feb 6, 2022, 9:02 PM
user U
, so from studio you would have to create an option to choose which link target you want to have (e.g. a boolean
blank
) and then on the frontend you can do something like this:
<Link href={href} target={blank ? '_blank' : '_self'}>
Feb 7, 2022, 8:00 AM
Thanks for your reply. I am new to sanity so I don't know how to create that option. And I don't think I need a boolean, I need all the links to be opened in a new tab. Do you know how I can do that?
Feb 7, 2022, 11:56 AM
I got it. I added the following to my serializer:
marks: {
   link: ({ children, mark }) => (
     <a href={mark.href} target="_blank" rel="noopener noreferer">
       {children}
     </a>
   ),
},
Feb 7, 2022, 12:10 PM
well done,
user U
!
Feb 9, 2022, 8:52 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
How to turn PortableText into plain text in Javascript?Dec 7, 2020
DOMParser is not defined at defaultParseHtmlDec 3, 2020
Next.js and Vercel - Error! No Output Directory named "dist" foundAug 8, 2022
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
Hi all! Does anyone have problem on deoplloying on Vercel (Using Next. js as a frontend)? I have two test projects, andonce...Jan 25, 2021
Error: unable to resolve image URL from source (undefined)Sep 11, 2022
Issue with compiling studio, resolved by nuking node_modules and installing with `sanity install`Aug 21, 2020
How to Disallow Robots on a SubdomainNov 12, 2020
Issue setting value of JSON field with document action in Sanity StudioApr 22, 2020
Issue with setting up Next.js E-commerce example and resolving the error related to the port.Jan 25, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle