Troubleshooting obtaining internalUrl with @portableText React in Sanity.io
18 replies
Last updated: Mar 7, 2022
N
How do I obtain the markDef internalUrl with @portableText React the reference to the internal page?
Mar 7, 2022, 3:20 PM
Not sure exactly where you’re running into trouble but below is a simplified version of what I’m using:
Subset of schema:
Subset of
Subset of
Subset of schema:
annotations: [ { name: 'internalLink', title: 'Internal link', type: 'object', fields: [ { name: 'reference', title: 'Reference', type: 'reference', to: [ { type: 'page' }, ], }, ], }, ]
groqquery:
portableText[] { ..., markDefs[] { ..., _type == "internalLink" => { "path": "/" + reference->slug.current } } }
componentsprop for the portable text component
const components = { marks: { internalLink: ({ text, value }) => { return ( <Link href={value.path}> <a>{text}</a> </Link> ); }, }, };
Mar 7, 2022, 3:31 PM
N
user J
Am I doing something wrong here?{ name: "internalLink", type: "object", title: "Internal link", blockEditor: { icon: icon.link, }, fields: [ { name: "reference", type: "reference", title: "Reference", to: [{ type: "page" }, { type: "author" }], }, ], },
export const articleQuery = groq`*[_type == "article" && slug.current == $slug][0] { ..., portableText[] { ..., markDefs[] { ..., _type == "internalLink" => { "path": "/" + reference->slug.current } } }, author -> { name, featuredImage, }, "slug": slug.current, }`;
Mar 7, 2022, 3:35 PM
N
keep getting the same:
{_key: '79d5d32f84d1', _type: 'internalLink', reference: {…}} reference: {_ref: '74bcefc7-d19f-4538-b904-d2bd568493bb', _type: 'reference'} _key: "79d5d32f84d1" _type: "internalLink" [[Prototype]]: Object
Mar 7, 2022, 3:38 PM
Looks alright at a quick glance.. 🤔Your portable text field’s name is
portableTextright?
Mar 7, 2022, 3:41 PM
N
Yes I am using the @portableText/React the one with components instead of serializers.
Mar 7, 2022, 3:42 PM
N
export default { title: "Block Content", name: "blockContent", type: "array", of: [ { title: "Block", type: "block", styles: [normal, h1, h2, h3, h4, quote, title], lists: [ol, li], marks: { decorators: [strong, emphasis, highlight, code], annotations: [ { name: "link", type: "object", title: "External link", blockEditor: { icon: icon.link, }, fields: [ { name: "href", type: "url", title: "URL", }, { title: "Open in new tab", name: "blank", description: "Read <https://css-tricks.com/use-target_blank/>", type: "boolean", }, ], }, { name: "internalLink", type: "object", title: "Internal link", blockEditor: { icon: icon.link, }, fields: [ { name: "reference", type: "reference", title: "Reference", to: [{ type: "page" }, { type: "author" }], }, ], }, ], }, }, image, video, ], };
Mar 7, 2022, 3:44 PM
N
You mean the name of the PortableText field?
Mar 7, 2022, 3:44 PM
yeah, change
portableText[] {to
blockContent[] {in your groq query, that might fix it
Mar 7, 2022, 3:45 PM
N
I got this after running query:
Mar 7, 2022, 3:47 PM
N
Lets see on the front-end side
Mar 7, 2022, 3:56 PM
N
Ohhhh yeahhh.
user J
:cool_doge: 🙌:fast_parrot:Mar 7, 2022, 3:57 PM
N
user J
Quick question I have a type "ImageWithAltText" I am trying to add the URL to the PortableText but not sure which prop I need to use.
2: alt: "Dogs" asset: _ref: "image-a7bdedc14df4221fc204b17f2b21dab703f71105-1296x972-jpg" _type: "reference" [[Prototype]]: Object markDefs: null path: null _key: "6dc2af8c00f1" _type: "imageWithAltText"
body[] { ..., _type == "imageWithAltText" => { ..., "path": reference->slug.current }, markDefs[] { ..., _type == "internalLink" => { "path": reference->slug.current } } },
Mar 7, 2022, 4:18 PM
N
_type == "imageWithAltText" => { ..., asset -> { ..., }, },
import { icon } from "./document-icons"; import { uploadLimitValidation } from "../../lib/helpers"; export default { name: "imageWithAltText", type: "image", title: "Image With Alt Text", icon: icon.image, options: { hotspot: true, accept: "image/*, .jpg, .jpeg", }, fields: [ { title: "Caption", name: "caption", type: "string", options: { isHighlighted: true, }, }, { title: "Alternative text", name: "alt", type: "string", validation: (Rule) => Rule.required().warning("Alt is recommended for SEO and accessiblity."), options: { isHighlighted: true, }, }, ], preview: { select: { title: "alt", image: "imageWithAltText", }, prepare({ title = "Alternative text missing", image }) { return { title, media: image, }; }, }, ...uploadLimitValidation, };
types: { imageWithAltText: ({ asset, alt, width }: any) => ( <>{console.log(asset)}</> ), },
Mar 7, 2022, 4:25 PM
Sorry not fully understanding what you’re wanting to do, which url? the url for the asset?
Mar 7, 2022, 4:32 PM
N
the URL of the image which I have pasted inside the PortableText editor
Mar 7, 2022, 4:33 PM
N
body[] { ..., _type == "imageWithAltText" => { asset -> { url, } }, markDefs[] { ..., _type == "internalLink" => { "path": reference->slug.current } } },
Mar 7, 2022, 4:38 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.