Skip to content

Commit 5970a32

Browse files
committed
fixed issue where renderer param needs to be the AstRender. this is not needed any more. can provide a simple function
1 parent 2fefa82 commit 5970a32

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Simple Implementation with custom styles
2+
3+
So to describe what i customized
4+
- Heading1 has a **fontSize** of 32, **backgroundColor** black and a **color** white.
5+
- all headers have a **border** at the bottom, of width 1 with a black color.
6+
7+
8+
```jsx
9+
import react from 'react';
10+
import {View, PureComponent, Text} from 'react-native';
11+
import Markdown, { AstRenderer } from 'react-native-markdown-renderer';
12+
import { StyleSheet } from 'react-native';
13+
14+
const styles = StyleSheet.create({
15+
heading: {
16+
borderBottomWidth: 1,
17+
borderColor: '#000000',
18+
},
19+
heading1: {
20+
fontSize: 32,
21+
backgroundColor: '#000000',
22+
color: '#FFFFFF',
23+
},
24+
heading2: {
25+
fontSize: 24,
26+
},
27+
heading3: {
28+
fontSize: 18,
29+
},
30+
heading4: {
31+
fontSize: 16,
32+
},
33+
heading5: {
34+
fontSize: 13,
35+
},
36+
heading6: {
37+
fontSize: 11,
38+
}
39+
});
40+
41+
const copy = `
42+
# h1 Heading 8-)
43+
## h2 Heading 8-)
44+
### h3 Heading 8-)
45+
46+
| Option | Description |
47+
| ------ | ----------- |
48+
| data | path to data files to supply the data that will be passed into templates. |
49+
| engine | engine to be used for processing templates. Handlebars is the default. |
50+
| ext | extension to be used for dest files. |
51+
`;
52+
53+
export default class Page extends PureComponent {
54+
render() {
55+
return (
56+
<Markdown style={styles}>{copy}</Markdown>
57+
);
58+
}
59+
}
60+
```

index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class Markdown extends Component {
4040
*/
4141
static propTypes = {
4242
children: PropTypes.node.isRequired,
43-
renderer: PropTypes.instanceOf(AstRenderer),
43+
renderer: PropTypes.oneOf([PropTypes.func, PropTypes.instanceOf(AstRenderer)]),
4444
rules: (props, propName, componentName) => {
4545
let invalidProps = [];
4646
const prop = props[propName];
@@ -111,12 +111,28 @@ export default class Markdown extends Component {
111111

112112
if (renderer && rules) {
113113
console.warn(
114-
'react-native-markdown-renderer you are using renderer and rules at the same time. This is not possible, props.rules are ignored'
114+
'react-native-markdown-renderer you are using renderer and rules at the same time. This is not possible, props.rules is ignored'
115+
);
116+
}
117+
118+
if (renderer && style) {
119+
console.warn(
120+
'react-native-markdown-renderer you are using renderer and style at the same time. This is not possible, props.style is ignored'
115121
);
116122
}
117123

118124
if (renderer) {
119-
this.renderer = renderer;
125+
if (typeof renderer === 'function') {
126+
this.renderer = {
127+
render: renderer,
128+
};
129+
} else if (renderer instanceof AstRenderer) {
130+
this.renderer = renderer;
131+
} else {
132+
throw new Error(
133+
'Provided renderer is not compatible with function or AstRenderer. please change'
134+
);
135+
}
120136
} else {
121137
this.renderer = new AstRenderer(
122138
{
@@ -150,7 +166,6 @@ export default class Markdown extends Component {
150166
*/
151167
render() {
152168
const copy = (this.copy = this.getCopyFromChildren());
153-
154-
return parser(copy, this.renderer, this.markdownParser);
169+
return parser(copy, this.renderer.render, this.markdownParser);
155170
}
156171
}

lib/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export function stringToTokens(source, markdownIt) {
1818
/**
1919
*
2020
* @param {string} source
21-
* @param {AstRenderer} [renderer]
21+
* @param {function} [renderer]
2222
* @param {AstRenderer} [markdownIt]
2323
* @return {View}
2424
*/
2525
export function parser(source, renderer, markdownIt) {
2626
const tokens = groupTextTokens(removeInlineTokens(stringToTokens(source, markdownIt)));
2727
const asttree = tokensToAST(tokens);
2828

29-
return <View>{renderer.render(asttree)}</View>;
29+
return renderer(asttree);
3030
}

0 commit comments

Comments
 (0)