Skip to content

Commit 96f9bbe

Browse files
committed
Support custom callback for link press
1 parent 260fad0 commit 96f9bbe

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,23 @@ const renderRules = {
5151
);
5252
},
5353
// a
54-
link: (node, children, parent, styles) => {
54+
link: (node, children, parent, styles, onLinkPress) => {
55+
const url = node.attributes.href;
5556
return (
56-
<Text key={node.key} style={styles.link} onPress={() => openUrl(node.attributes.href)}>
57+
<Text key={node.key} style={styles.link} onPress={() => (onLinkPress ? onLinkPress(url) : openUrl(url))}>
5758
{children}
5859
</Text>
5960
);
6061
},
6162
// a with a non text element nested inside
62-
blocklink: (node, children, parent, styles) => {
63-
return (
64-
<TouchableWithoutFeedback key={node.key} onPress={() => openUrl(node.attributes.href)} style={styles.blocklink}>
63+
blocklink: (node, children, parent, styles, onLinkPress) => {
64+
const url = node.attributes.href;
65+
return (
66+
<TouchableWithoutFeedback
67+
key={node.key}
68+
onPress={() => (onLinkPress ? onLinkPress(url) : openUrl(url))}
69+
style={styles.blocklink}
70+
>
6571
<View style={styles.image}>{children}</View>
6672
</TouchableWithoutFeedback>
6773
);

0 commit comments

Comments
 (0)