Skip to content

add option for resolving links #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged

add option for resolving links #8

merged 1 commit into from
Apr 14, 2021

Conversation

melono6
Copy link

@melono6 melono6 commented Apr 1, 2021

Was having an issue where i was using richTextOutputFormat: 'none'
The assets being linked in the richtext eg: an image was not pulling in the data as resolveLinks is hardcoded to false.

added an option to set to true

@smnh
Copy link
Member

smnh commented Apr 4, 2021

Hey @melono6,
Just curious, how do you access images from within the rich text field?

@melono6
Copy link
Author

melono6 commented Apr 6, 2021

you enable the option in rich text settings on the right
Screenshot 2021-04-06 at 09 40 39

and then you can add in top right in the richtext
Screenshot 2021-04-06 at 09 40 59

@smnh
Copy link
Member

smnh commented Apr 6, 2021

Hey @melono6

Thanks for the PR. Approved and will release new version soon.

P.S.: in my previous question I meant how do you access images embedded in rich text from within your website code.

Also note, that with this flag, Contentful will resolved links between the objects.
But this Sourcebit plugin has its own method of resolving objects by using value.sys.id of one field to match id of another object:
https://github.com/stackbit/sourcebit-source-contentful/blob/23e13cbf4b/lib/contentful-util.js#L71-L84

I hope these two methods of resolving links won't conflict.

@melono6
Copy link
Author

melono6 commented Apr 7, 2021

i am using the contentful rich text package
https://www.npmjs.com/package/@contentful/rich-text-react-renderer

you then pass the array that contentful gives you into the function and can provide differnet rendering for different node block types:
eg:
{documentToReactComponents(richText, { renderNode: { [INLINES.HYPERLINK]: (node, renderedValues) => { return ( <Link href={node.data.uri} passHref> <a target="_blank" rel="noopener"> {renderedValues} </a> </Link> ); }, [BLOCKS.EMBEDDED_ASSET]: (node) => { return ( <div> <Image src={node.data.target.fields.file['en-US'].url} alt={node.data.target.fields.file['en-US'].fileName} width={node.data.target.fields.file['en-US'].details.image.width} height={node.data.target.fields.file['en-US'].details.image.height} /> </div> ); }, [INLINES.ENTRY_HYPERLINK]: (node, text) => { const { slug } = node.data.target.fields; return ( <Link href={/${slug}} passHref> <a>{text}</a> </Link> ); }, [INLINES.ASSET_HYPERLINK]: (node) => { const { title, file } = node.data.target.fields; const { url } = file; return ( <Link href={url} passHref> <a target="_blank" rel="noopener"> {title} </a> </Link> ); }, }, })}

the image node being BLOCKS.EMBEDDED_ASSET in above snippet

@smnh smnh merged commit 6d7d536 into stackbit:master Apr 14, 2021
@smnh
Copy link
Member

smnh commented Apr 14, 2021

Published under v0.6.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants