You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think I understand how to create the new block definitions for h1, h2 etc..., or a complete handler like in the example above, but in the code below, I don't believe node is available from props? And I'm not sure what to return instead of return PortableText.defaultSerializers.types.block(props) (since defaultSerializers is no longer on PortableText)
constcomponents: PortableTextComponents={block: props=>{const{ node, children }=propsconst{ style, _key }=nodeif(/^h\d/.test(style)){constHeadingTag=style// Even though HTML5 allows id to start with a digit, we append it with a letter to avoid various JS methods to act up and make problemsconstheadingId=`h${_key}`return(<HeadingTagid={headingId}><ahref={`#${headingId}`}aria-hidden="true"tabIndex={-1}>
#
</a><span>{children}</span></HeadingTag>)}// ... you can put in other overrides here// or return the default ones 👇returnPortableText.defaultSerializers.types.block(props)}}
Any thoughts or suggestions greatly appreciated.
The text was updated successfully, but these errors were encountered:
In case anyone else finds this helpful - here's how I did this in the end.... creating a url-safe kebab-case ID for the heading element and anchor ref using slugify and a helper function to extract the text from the heading element.
import*asReactfrom'react'import{PortableText}from'@portabletext/react'importslugifyfrom'slugify'constgetTextFromChildren=(children: React.ReactNode)=>{lettext=''React.Children.map(children,child=>{if(typeofchild==='string'){text+=child}})returntext}typeHeadingWithAnchorProps={heading: stringchildren: React.ReactNode}functionHeadingWithAnchor({ heading, children }: HeadingWithAnchorProps){consttext=getTextFromChildren(children)constheadingId=slugify(text,{lower: true})constElement=headingaskeyofJSX.IntrinsicElementsreturn(<Elementid={headingId}><ahref={`#${headingId}`}aria-hidden="true"tabIndex={-1}>
#
</a><span>{children}</span></Element>)}constcomponents: PortableTextComponents={block: {h2: ({ children })=>{return<HeadingWithAnchorheading="h2">{children}</HeadingWithAnchor>},h3: ({ children })=>{return<HeadingWithAnchorheading="h3">{children}</HeadingWithAnchor>},h4: ({ children })=>{return<HeadingWithAnchorheading="h4">{children}</HeadingWithAnchor>},}}
Wondering how we would do something similar to this...
https://www.sanity.io/schemas/anchored-headings-for-portable-text-a6c70b6e - created by @kmelve
I think I understand how to create the new
block
definitions for h1, h2 etc..., or a complete handler like in the example above, but in the code below, I don't believenode
is available fromprops
? And I'm not sure what to return instead ofreturn PortableText.defaultSerializers.types.block(props)
(sincedefaultSerializers
is no longer onPortableText
)Any thoughts or suggestions greatly appreciated.
The text was updated successfully, but these errors were encountered: