react-native-markdown-renderer
react-native-markdown-renderer copied to clipboard
Allow to override linking behavior
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.
any update?
Im also interested in this to be able to handle linking within the app!
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!'))} />
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)?
I get this error
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?
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