Skip to content

Commit f7b21f7

Browse files
committed
- fixed issue with not providing custom markdown
1 parent a422f30 commit f7b21f7

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

example/App.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from 'react';
22
import { StyleSheet, Text, View, ScrollView } from 'react-native';
3-
import Markdown, { AstRenderer, style, defaultRenderFunctions, PluginContainer, blockPlugin } from 'react-native-markdown-renderer';
3+
import Markdown, {
4+
AstRenderer, style, defaultRenderFunctions, PluginContainer, blockPlugin,
5+
getUniqueID
6+
} from 'react-native-markdown-renderer';
47

58
const markdownText = `
69
# Syntax Support
@@ -176,13 +179,13 @@ const renderer = new AstRenderer({
176179
...defaultRenderFunctions,
177180
h1: (node, children, parents) => {
178181
return (
179-
<Text key={AstRenderer.getUniqueID()} style={{ backgroundColor: 'red' }}>{children}</Text>
182+
<Text key={getUniqueID()} style={{ backgroundColor: 'red' }}>{children}</Text>
180183
);
181184
},
182185
// added custom block element defined by plugin
183186
block: (node, children, parents) => {
184187
return (
185-
<Text key={AstRenderer.getUniqueID()} style={{ backgroundColor: 'green' }}>{children}</Text>
188+
<Text key={getUniqueID()} style={{ backgroundColor: 'green' }}>{children}</Text>
186189
);
187190
},
188191
});

index.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export default class Markdown extends Component {
4444
rules: (props, propName, componentName) => {
4545
let invalidProps = [];
4646
const prop = props[propName];
47+
48+
if (!prop) {
49+
return;
50+
}
51+
4752
if (typeof prop === 'object') {
4853
invalidProps = Object.keys(prop).filter(key => typeof prop[key] !== 'function');
4954
}
@@ -82,11 +87,11 @@ export default class Markdown extends Component {
8287
markdownParser = null;
8388

8489
/**
85-
* Only when the copy changes will the markdown render again.
86-
* @param nextProps
87-
* @param nextState
88-
* @return {boolean}
89-
*/
90+
* Only when the copy changes will the markdown render again.
91+
* @param nextProps
92+
* @param nextState
93+
* @return {boolean}
94+
*/
9095
shouldComponentUpdate(nextProps, nextState) {
9196
const copy = this.getCopyFromChildren(nextProps.children);
9297

@@ -99,8 +104,8 @@ export default class Markdown extends Component {
99104
}
100105

101106
/**
102-
*
103-
*/
107+
*
108+
*/
104109
componentWillMount() {
105110
const { renderer, rules, style, plugins, markdownit } = this.props;
106111

@@ -125,29 +130,27 @@ export default class Markdown extends Component {
125130
);
126131
}
127132

133+
let md = markdownit;
128134
if (plugins && plugins.length > 0) {
129-
let md = markdownit;
130-
131135
plugins.forEach(plugin => {
132136
md = md.use.apply(md, plugin.toArray());
133137
});
134-
135-
this.markdownParser = md;
136138
}
139+
140+
this.markdownParser = md;
137141
}
138142

139143
getCopyFromChildren(children = this.props.children) {
140144
return children instanceof Array ? children.join('') : children;
141145
}
142146

143147
/**
144-
*
145-
* @return {View}
146-
*/
148+
*
149+
* @return {View}
150+
*/
147151
render() {
148152
const copy = (this.copy = this.getCopyFromChildren());
149-
const { renderer } = this.props;
150153

151-
return parser(copy, renderer, this.markdownParser);
154+
return parser(copy, this.renderer, this.markdownParser);
152155
}
153156
}

lib/parser.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React from 'react';
22
import { View } from 'react-native';
3-
import MarkdownIt from 'markdown-it';
43
import tokensToAST from './util/tokensToAST';
54
import removeInlineTokens from './util/removeInlineTokens';
65
import groupTextTokens from './util/groupTextTokens';
76

8-
let md = MarkdownIt();
9-
10-
export function stringToTokens(source, markdownIt = md) {
7+
export function stringToTokens(source, markdownIt) {
118
let result = [];
129
try {
13-
result = md.parse(source, {});
14-
} catch (err) {}
10+
result = markdownIt.parse(source, {});
11+
} catch (err) {
12+
console.warn(err);
13+
}
14+
1515
return result;
1616
}
1717

@@ -22,7 +22,7 @@ export function stringToTokens(source, markdownIt = md) {
2222
* @param {AstRenderer} [markdownIt]
2323
* @return {View}
2424
*/
25-
export function parser(source, renderer, markdownIt = md) {
25+
export function parser(source, renderer, markdownIt) {
2626
const tokens = groupTextTokens(removeInlineTokens(stringToTokens(source, markdownIt)));
2727
const asttree = tokensToAST(tokens);
2828

lib/renderRules.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import FitImage from 'react-native-fit-image';
55
import AstRenderer from './AstRenderer';
66
import getUniqueID from "./util/getUniqueID";
77
import openUrl from "./util/openUrl";
8+
import hasParents from "./util/hasParents";
89

910
const renderRules = {
1011
// when unknown elements are introduced, so it wont break
@@ -149,7 +150,7 @@ const renderRules = {
149150
);
150151
},
151152
li: (node, children, parent, styles) => {
152-
if (AstRenderer.hasParents(parent, 'ul')) {
153+
if (hasParents(parent, 'ul')) {
153154
return (
154155
<View key={getUniqueID()} style={styles.listUnorderedItem}>
155156
<Text style={styles.listUnorderedItemIcon}>
@@ -162,7 +163,7 @@ const renderRules = {
162163
);
163164
}
164165

165-
if (AstRenderer.hasParents(parent, 'ol')) {
166+
if (hasParents(parent, 'ol')) {
166167
return (
167168
<View key={getUniqueID()} style={styles.listOrderedItem}>
168169
<Text style={styles.listOrderedItemIcon}>

0 commit comments

Comments
 (0)