Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types'
import {
TouchableOpacity,
Text,
View,
Image,
Expand All @@ -22,19 +21,24 @@ class Markdown extends Component {
renderImage: PropTypes.func,
renderLink: PropTypes.func,
renderListBullet: PropTypes.func,
renderInline: PropTypes.bool,
}

static defaultProps = {
debug: false,
useDefaultStyles: true,
parseInline: false,
markdownStyles: {}
markdownStyles: {},
renderInline: false
}

constructor(props) {
super(props);

const rules = SimpleMarkdown.defaultRules;
let rules = {
...SimpleMarkdown.defaultRules,
...this.props.rules
};
this.parser = SimpleMarkdown.parserFor(rules);
this.reactOutput = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'));
const blockSource = this.props.children + '\n\n';
Expand Down Expand Up @@ -181,9 +185,9 @@ class Markdown extends Component {
}

return(
<TouchableOpacity style={styles.linkWrapper} key={'linkWrapper_' + key} onPress={() => Linking.openURL(node.props.href).catch(() => {})}>
<Text style={styles.linkWrapper} key={'linkWrapper_' + key} onPress={() => Linking.openURL(node.props.href).catch(() => {})}>
{children}
</TouchableOpacity>
</Text>
);
}

Expand Down Expand Up @@ -216,8 +220,15 @@ class Markdown extends Component {
</View>
);
} else {
const additionalProps = {}
if (this.props.renderInline) {
additionalProps = {
ellipsizeMode:'tail',
numberOfLines: 1,
}
}
return(
<Text key={'block_' + key} style={styles.block}>
<Text key={'block_' + key} style={styles.block} {...additionalProps}>
{nodes}
</Text>
);
Expand All @@ -231,14 +242,29 @@ class Markdown extends Component {
}
}

renderCustom(node) {
return React.Children.only(node.props.children);
}

renderCode(node, key, extras) {
const {styles} = this.state;

let style = (extras && extras.style) ? [styles.code].concat(extras.style) : styles.code;

return(
<Text key={key} style={style}>
{node.props.children}
</Text>
);
}

renderNode(node, key, index, extras) {
if (node == null || node == "null" || node == "undefined" || node == "") {
return null;
}

const {styles} = this.state;


switch(node.type) {
case 'h1': return this.renderText(node, key, Utils.concatStyles(extras, styles.h1));
case 'h2': return this.renderText(node, key, Utils.concatStyles(extras, styles.h2));
Expand All @@ -258,6 +284,8 @@ class Markdown extends Component {
case 'em': return this.renderText(node, key, Utils.concatStyles(extras, styles.em));
case 'u': return this.renderText(node, key, Utils.concatStyles(extras, styles.u));
case 'blockquote': return this.renderBlockQuote(node, key);
case 'code': return this.renderCode(node, key, extras);
case 'custom': return this.renderCustom(node, key);
case undefined: return this.renderText(node, key, extras);
default: if (this.props.debug) console.log('Node type '+node.type+' is not supported'); return null;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"peerDependencies": {
"react-native": "*"
}
}
}
3 changes: 3 additions & 0 deletions styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const defaultStyles = {
flex: 1,
minWidth: 200,
height: 200
},
code: {
backgroundColor: '#cccccc'
}
};

Expand Down