react-native-markdown-renderer icon indicating copy to clipboard operation
react-native-markdown-renderer copied to clipboard

Allow to override linking behavior

Open sr3d opened this issue 6 years ago • 7 comments

For links, the component should make the onPress() function to be a prop to be passed in from outside so that the linking behavior can be controlled, and can be defaulted to use the openUrl() method.

sr3d avatar Aug 28 '18 21:08 sr3d

any update?

kuriel-trivu avatar Jun 06 '19 21:06 kuriel-trivu

Im also interested in this to be able to handle linking within the app!

samiede avatar Jun 19 '19 16:06 samiede

After looking into the source code we found that it's actually pretty easy to override linking behaviour by supplying a custom rule like so:

const OverrideOnPressRule = (callback) => ({
  link: (node, children, parent, styles) => {
    return (
      <Text key={node.key} style={styles.link} onPress={() => callback(node.attributes.href)}>
        {children}
      </Text>
    )
  },
  // a with a non text element nested inside
  blocklink: (node, children, parent, styles) => {
    return (
      <TouchableWithoutFeedback key={node.key} onPress={() => callback(node.attributes.href)} style={styles.blocklink}>
        <View style={styles.image}>{children}</View>
      </TouchableWithoutFeedback>
    )
  }
})

and then setting the rules prop:

<Markdown rules={OverrideOnPressRule(() => console.log('Your callback here!'))} />

samiede avatar Jun 20 '19 08:06 samiede

I need a callback as well as being able to open the link. is that possible to have the callback and open the url (the default)?

isa3bel avatar Nov 04 '19 19:11 isa3bel

Screen Shot 2019-11-04 at 11 48 38 AM

I get this error

isa3bel avatar Nov 04 '19 19:11 isa3bel

Ok so if I remove the returns in the custom rules, then the red error goes away. however the link is not visible and so I can't press on it. Do you know what the problem could be?

isa3bel avatar Nov 04 '19 20:11 isa3bel

it seems that doing rules={Override...} isn't actually overriding the rules at all. do you know what the problem could be? Can I see your entire code? @samiede thanks

isa3bel avatar Nov 05 '19 01:11 isa3bel