Skip to content

Commit 88b7fe7

Browse files
committed
Add callback for links
1 parent 260fad0 commit 88b7fe7

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class Markdown extends Component {
4747
static propTypes = {
4848
children: PropTypes.node.isRequired,
4949
renderer: PropTypes.oneOfType([PropTypes.func, PropTypes.instanceOf(AstRenderer)]),
50+
onLinkPress: PropTypes.func,
5051
rules: (props, propName, componentName) => {
5152
let invalidProps = [];
5253
const prop = props[propName];
@@ -163,7 +164,8 @@ export default class Markdown extends Component {
163164
{
164165
...styles,
165166
...style,
166-
}
167+
},
168+
props.onLinkPress
167169
);
168170
}
169171
}

src/lib/AstRenderer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export default class AstRenderer {
1515
* @param {Object.<string, function>} renderRules
1616
* @param {any} style
1717
*/
18-
constructor(renderRules, style) {
18+
constructor(renderRules, style, onLinkPress) {
1919
this._renderRules = renderRules;
2020
this._style = style;
21+
this._onLinkPress = onLinkPress;
2122
}
2223

2324
/**
@@ -56,6 +57,10 @@ export default class AstRenderer {
5657
return this.renderNode(value, parents);
5758
});
5859

60+
if (node.type === "link" || node.type === "blocklink") {
61+
return renderFunction(node, children, parentNodes, this._style, this._onLinkPress);
62+
}
63+
5964
return renderFunction(node, children, parentNodes, this._style);
6065
};
6166

src/lib/renderRules.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,21 @@ const renderRules = {
5151
);
5252
},
5353
// a
54-
link: (node, children, parent, styles) => {
54+
link: (node, children, parent, styles, onLinkPress) => {
5555
return (
56-
<Text key={node.key} style={styles.link} onPress={() => openUrl(node.attributes.href)}>
56+
<Text key={node.key} style={styles.link} onPress={() => openUrl(node.attributes.href, onLinkPress)}>
5757
{children}
5858
</Text>
5959
);
6060
},
6161
// a with a non text element nested inside
62-
blocklink: (node, children, parent, styles) => {
62+
blocklink: (node, children, parent, styles, onLinkPress) => {
6363
return (
64-
<TouchableWithoutFeedback key={node.key} onPress={() => openUrl(node.attributes.href)} style={styles.blocklink}>
64+
<TouchableWithoutFeedback
65+
key={node.key}
66+
onPress={() => openUrl(node.attributes.href, onLinkPress)}
67+
style={styles.blocklink}
68+
>
6569
<View style={styles.image}>{children}</View>
6670
</TouchableWithoutFeedback>
6771
);

src/lib/util/openUrl.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Linking } from 'react-native';
22

3-
export default function openUrl(url) {
4-
if (url) {
3+
export default function openUrl(url, customCallback) {
4+
if (customCallback) {
5+
const result = customCallback(url);
6+
if (url && result && typeof result === 'boolean') {
7+
Linking.openURL(url);
8+
}
9+
} else if (url) {
510
Linking.openURL(url);
611
}
712
}

0 commit comments

Comments
 (0)