Skip to content

Commit a7aa9aa

Browse files
authored
Merge pull request #90 from iamacup/6.x
6.x
2 parents 293993a + 2d0c71c commit a7aa9aa

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The `<Markdown>` object takes the following common props:
6161

6262
| Property | Default | Required | Description
6363
| --- | --- | --- | ---
64-
| `children` | N/A | `true` | The markdown string to render
64+
| `children` | N/A | `true` | The markdown string to render, or the [pre-processed tree](#pre-processing)
6565
| `style` | [source](https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/styles.js) | `false` | An object to override the styling for the various rules, [see style section below](#style) for more info
6666
| `mergeStyle` | `true` | `false` | If true, when a style is supplied, the individual items are merged with the default styles instead of overwriting them
6767
| `rules` | [source](https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/renderRules.js) | `false` | An object of rules that specify how to render each markdown item, [see rules section below](#rules) for more info
@@ -449,7 +449,7 @@ const App: () => React$Node = () => {
449449
// examine the node properties to see what video we need to render
450450
console.log(node); // expected output of this is in readme.md below this code snip
451451

452-
return (<Text key={getUniqueID()} style={styles.video}>
452+
return (<Text key={node.key} style={styles.video}>
453453
Return a video component instead of this text component!
454454
</Text>);
455455
}
@@ -1033,7 +1033,7 @@ Something like this with `yourCustomHandlerFunctionOrLogicHere`:
10331033
import React from 'react';
10341034
import { SafeAreaView, ScrollView, StatusBar, Text } from 'react-native';
10351035

1036-
import Markdown, {getUniqueID} from 'react-native-markdown-display';
1036+
import Markdown from 'react-native-markdown-display';
10371037

10381038
const copy = `[This is a link!](https://github.com/iamacup/react-native-markdown-display/)`;
10391039

@@ -1120,6 +1120,48 @@ export default App;
11201120
A full list of things you can turn off is [here](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/commonmark.js)
11211121

11221122

1123+
### Pre Processing
1124+
1125+
It is possible to need to pre-process the data outside of this library ([related discussion here](https://github.com/iamacup/react-native-markdown-display/issues/79)). As a result, you can pass an AST tree directly as the children like this:
1126+
1127+
```jsx
1128+
import React from 'react';
1129+
import { SafeAreaView, ScrollView, StatusBar, Text } from 'react-native';
1130+
1131+
import Markdown, { MarkdownIt, tokensToAST, stringToTokens } from 'react-native-markdown-display';
1132+
1133+
const markdownItInstance = MarkdownIt({typographer: true});
1134+
1135+
const copy = `
1136+
# Hello this is a title
1137+
1138+
This is some text with **BOLD!**
1139+
`;
1140+
1141+
const ast = tokensToAST(stringToTokens(copy, markdownItInstance))
1142+
1143+
const App: () => React$Node = () => {
1144+
return (
1145+
<>
1146+
<StatusBar barStyle="dark-content" />
1147+
<SafeAreaView>
1148+
<ScrollView
1149+
contentInsetAdjustmentBehavior="automatic"
1150+
style={{height: '100%'}}
1151+
>
1152+
<Markdown
1153+
>
1154+
{ast}
1155+
</Markdown>
1156+
</ScrollView>
1157+
</SafeAreaView>
1158+
</>
1159+
);
1160+
};
1161+
1162+
export default App;
1163+
```
1164+
11231165

11241166
### Other Notes
11251167

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-markdown-display",
3-
"version": "6.1.4",
3+
"version": "6.1.5",
44
"description": "Markdown renderer for react-native, with CommonMark spec support + adds syntax extensions & sugar (URL autolinking, typographer), originally created by Mient-jan Stelling as react-native-markdown-renderer",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import removeTextStyleProps from './lib/util/removeTextStyleProps';
1818
import {styles} from './lib/styles';
1919
import {stringToTokens} from './lib/util/stringToTokens';
2020
import FitImage from 'react-native-fit-image';
21+
import textStyleProps from './lib/data/textStyleProps';
2122

2223
export {
2324
getUniqueID,
@@ -32,6 +33,7 @@ export {
3233
styles,
3334
removeTextStyleProps,
3435
FitImage,
36+
textStyleProps,
3537
};
3638

3739
// we use StyleSheet.flatten here to make sure we have an object, in case someone
@@ -185,7 +187,7 @@ const Markdown = React.memo(
185187
);
186188

187189
Markdown.propTypes = {
188-
children: PropTypes.node.isRequired,
190+
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array]).isRequired,
189191
renderer: PropTypes.oneOfType([
190192
PropTypes.func,
191193
PropTypes.instanceOf(AstRenderer),

src/lib/parser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import groupTextTokens from './util/groupTextTokens';
1111
* @return {View}
1212
*/
1313
export default function parser(source, renderer, markdownIt) {
14+
if (Array.isArray(source)) {
15+
return renderer(source);
16+
}
17+
1418
let tokens = stringToTokens(source, markdownIt);
1519
tokens = cleanupTokens(tokens);
1620
tokens = groupTextTokens(tokens);

0 commit comments

Comments
 (0)