Skip to content
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

Linkify elements using dangerouslySetInnerHTML #45

Conversation

pablolmiranda
Copy link

@pablolmiranda pablolmiranda commented Jul 27, 2017

Sometimes we have a pure HTML text to parse and the links are not inside the tags. On these cases, we want to render the HTML using dangerouslySetInnerHTML and linkify the content.
Example:

<Linkify>
  <div>Take a look on the link: https://github.com/tasti/react-linkify</div>
  <div dangerouslySetInnerHTML={{ __html: '<div>Take a look on the link https://github.com/tasti/react-linkify</div>' }} />
</Linkify>

Please take a look on the Codesandbox example: https://codesandbox.io/s/DRlyjgNL6

On this example, the first div is correctly linkified but not the second. This PR tries to fix that.

Depends on #44

@pablolmiranda pablolmiranda force-pushed the parse-elements-with-dangerouslySetInnerHTML-props branch from 00253c6 to 7351c81 Compare August 30, 2017 20:29
@anaumov
Copy link

anaumov commented Sep 4, 2017

Cool! Need this feature!

@Amitom
Copy link

Amitom commented Nov 21, 2017

Need it too ! Thx

@tasti tasti mentioned this pull request Jan 6, 2018
16 tasks

for ( const prop in children.props ) {
if(prop !== 'dangerouslySetInnerHTML') {
clonedProps[prop] = children.prop[prop];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be children.props[prop]?


parsed = React.createElement(
children.type,
Object.assign(clonedProps, {key: `parse${++this.parseCounter}`}),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use { ...clonedProps, key: parse${++this.parseCounter} }, here and let babel handle it instead of Object.assign to avoid polyfill on IE11?

@ironosity
Copy link

I was trying this out today as I ran into same problem as you guys and @pablolmiranda it just doesn't work. Sure it turns links inside the dangerouslySetInnerHTML into links, but it breaks dangerouslySetInnerHTML all together such that html tags are now rendered to the page. I.E. if dangerouslySetInnerHTML={{ __html: '<p>some text and a link.com</p>' }} it will print

tags but link.com will be linkified

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.

4 participants