Skip to content

Commit f343ba5

Browse files
committed
- refactoring defaultRenderFunctions
- AstRenderer now has AstRenderer.hasParents and AstRenderer.openLink
1 parent 52fd5f1 commit f343ba5

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

lib/AstRenderer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Linking } from 'react-native';
2+
13
let uuid = new Date().getTime();
24

35
/**
@@ -13,6 +15,14 @@ export default class AstRenderer {
1315
return uuid.toString(16);
1416
}
1517

18+
static hasParents(parents, type) {
19+
return parents.findIndex(el => el.type === type) > -1;
20+
}
21+
22+
static openUrl = url => {
23+
Linking.openURL(url);
24+
};
25+
1626
/**
1727
*
1828
* @param {Object.<string, function>} renderFunctions

lib/PluginContainer.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
export default class PluginContainer {
2-
3-
4-
5-
constructor (plugin, ...options)
6-
{
7-
this.plugin = plugin;
8-
this.options = options;
9-
}
10-
11-
toArray()
12-
{
13-
return [
14-
this.plugin,
15-
...this.options
16-
]
17-
}
18-
}
2+
constructor(plugin, ...options) {
3+
this.plugin = plugin;
4+
this.options = options;
5+
}
6+
7+
toArray() {
8+
return [this.plugin, ...this.options];
9+
}
10+
}

lib/defaultRenderFunctions.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import React, { Component, PropTypes } from 'react';
2-
import { Linking, Text, View } from 'react-native';
2+
import { Text, View } from 'react-native';
33

44
import FitImage from 'react-native-fit-image';
55
import { markdownStyles } from './style';
66
import AstRenderer from './AstRenderer';
77

8-
const openUrl = url => {
9-
Linking.openURL(url);
10-
};
118

12-
const hasParents = (parents, type) => {
13-
return parents.findIndex(el => el.type === type) > -1;
14-
};
9+
10+
1511

1612
const defaultRenderFunctions = {
1713
// when unknown elements are introduced, so it wont break
@@ -57,13 +53,13 @@ const defaultRenderFunctions = {
5753
},
5854
a: (node, children) => {
5955
return (
60-
<Text style={markdownStyles.a} onPress={() => openUrl(node.attributes.href)}>
56+
<Text style={markdownStyles.a} onPress={() => AstRenderer.openUrl(node.attributes.href)}>
6157
{children}
6258
</Text>
6359
);
6460
},
6561
em: (node, children, parents) => {
66-
if (hasParents(parents, 'ICONLIST')) {
62+
if (AstRenderer.hasParents(parents, 'ICONLIST')) {
6763
return (
6864
<Text style={markdownStyles.icon} key={AstRenderer.getUniqueID()}>
6965
{children}
@@ -149,7 +145,7 @@ const defaultRenderFunctions = {
149145
);
150146
},
151147
li: (node, children, parents) => {
152-
if (hasParents(parents, 'ul')) {
148+
if (AstRenderer.hasParents(parents, 'ul')) {
153149
return (
154150
<View key={AstRenderer.getUniqueID()} style={markdownStyles.listUnorderedItem}>
155151
<Text style={{ marginLeft: -10, marginRight: 10, lineHeight: 40 }}>
@@ -162,7 +158,7 @@ const defaultRenderFunctions = {
162158
);
163159
}
164160

165-
if (hasParents(parents, 'ol')) {
161+
if (AstRenderer.hasParents(parents, 'ol')) {
166162
return (
167163
<View key={AstRenderer.getUniqueID()} style={{ flexDirection: 'row' }}>
168164
<Text style={{ width: 20, lineHeight: 40 }}>

0 commit comments

Comments
 (0)