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
11 changes: 9 additions & 2 deletions MarkdownView.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from 'react'

import {
Text,
View,
} from 'react-native'

Expand Down Expand Up @@ -81,10 +82,11 @@ class MarkdownView extends Component {
onLinkPress?: (string) => void,
styles?: Styles,
children: string,
textProps?: Object,
}

render() {
const {rules = {}, styles = {}, onLinkPress} = this.props
const {rules = {}, styles = {}, onLinkPress, textProps = {}} = this.props

const mergedStyles = mergeStyles(DefaultStyles, styles)
const mergedRules = mergeRules(SimpleMarkdown.defaultRules, simpleMarkdownRules(mergeRules(DefaultRules, rules), mergedStyles))
Expand All @@ -93,7 +95,7 @@ class MarkdownView extends Component {

const ast = SimpleMarkdown.parserFor(mergedRules)(markdown, {inline: false})
const render = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(mergedRules, 'react'))
const initialRenderState = {onLinkPress: onLinkPress}
const initialRenderState = {onLinkPress: onLinkPress, textProps: textProps}

return (
<View style={this.props.style}>
Expand Down Expand Up @@ -163,6 +165,11 @@ MarkdownView.propTypes = {
* string (first and only argument).
*/
onLinkPress: PropTypes.func,

/**
* Props passed to all <Text/> components. See https://facebook.github.io/react-native/docs/text#props
*/
textProps: PropTypes.object,
}

export default MarkdownView
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ e.g.
}
```

## onLinkPress
### onLinkPress

Callback function for when a link is pressed. The callback receives the URL of the link as a
string (first and only argument).

### textProps

Props passed to all `<Text/>` components. See https://facebook.github.io/react-native/docs/text#props
16 changes: 8 additions & 8 deletions renders.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function renderTableCell(cell, row, column, rowCount, columnCount, output, state
}

return <Cell rowId={row} id={column} key={column} style={cellStyle}>
<Text style={contentStyle}>
<Text {...state.textProps} style={contentStyle}>
{output(cell, state)}
</Text>
</Cell>
Expand All @@ -91,7 +91,7 @@ function paragraphRenderer() {

function textContentRenderer(styleName, styleName2) {
return (node: InlineContentNode, output: OutputFunction, state: RenderState, styles: RenderStyles) => (
<Text key={state.key} style={styleName2 ? [styles[styleName], styles[styleName2]] : styles[styleName]}>
<Text {...state.textProps} key={state.key} style={styleName2 ? [styles[styleName], styles[styleName2]] : styles[styleName]}>
{typeof node.content === 'string' ? node.content : output(node.content, state)}
</Text>
)
Expand Down Expand Up @@ -119,7 +119,7 @@ function paddedSize(size, style) {
export default Object.freeze({
blockQuote: textContentRenderer('blockQuote'),
br: (node: EmptyNode, output: OutputFunction, state: RenderState, styles: RenderStyles) => (
<Text key={state.key} style={styles.br}>
<Text {...state.textProps} key={state.key} style={styles.br}>
{'\n\n'}
</Text>
),
Expand All @@ -136,7 +136,7 @@ export default Object.freeze({
inlineCode: textContentRenderer('inlineCode'),
link: (node: LinkNode, output: OutputFunction, state: RenderState, styles: RenderStyles) => {
const onPress = state.onLinkPress
return <Text key={state.key} style={styles.link} onPress={onPress ? () => onPress(node.target) : null}>
return <Text {...state.textProps} key={state.key} style={styles.link} onPress={onPress ? () => onPress(node.target) : null}>
{typeof node.content === 'string' ? node.content : output(node.content, state)}
</Text>
},
Expand All @@ -146,21 +146,21 @@ export default Object.freeze({
<View key={i} style={styles.listItem}>
{
node.ordered ?
<Text style={styles.listItemNumber}>{`${i + 1}.`}</Text>
<Text {...state.textProps} style={styles.listItemNumber}>{`${i + 1}.`}</Text>
:
<Text style={styles.listItemBullet}>
<Text {...state.textProps} style={styles.listItemBullet}>
{styles.listItemBullet && styles.listItemBullet.content ? styles.listItemBullet.content : '\u2022'}
</Text>
}
<Text style={node.ordered ? styles.listItemOrderedContent : styles.listItemUnorderedContent}>
<Text {...state.textProps} style={node.ordered ? styles.listItemOrderedContent : styles.listItemUnorderedContent}>
{output(item, state)}
</Text>
</View>
))}
</View>
),
newline: (node: EmptyNode, output: OutputFunction, state: RenderState, styles: RenderStyles) => (
<Text key={state.key} style={styles.newline}>
<Text {...state.textProps} key={state.key} style={styles.newline}>
{'\n'}
</Text>
),
Expand Down
3 changes: 2 additions & 1 deletion types.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export type NodeKey = string
export type OutputFunction = (Node, Object) => ?any
export type RenderState = {
key: string,
onLinkPress: ?(string) => void
onLinkPress: ?(string) => void,
textProps: Object,
}
export type RenderStyle = Object
export type RenderStyles = {[key: NodeKey]: RenderStyle}
Expand Down