Skip to content

Commit 4e85ac5

Browse files
committed
refactoring code so it will be easier to add plugins
1 parent 49b290f commit 4e85ac5

17 files changed

+134
-102
lines changed

lib/parser.js

Lines changed: 0 additions & 30 deletions
This file was deleted.
File renamed without changes.
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
1+
import React, { Component, PropTypes } from "react";
2+
import { Text, View } from "react-native";
3+
import getUniqueID from "./util/getUniqueID";
4+
5+
export function rootRenderRule(children) {
6+
return <View key={getUniqueID()}>{children}</View>;
7+
}
8+
19
/**
210
*
311
*/
412
export default class AstRenderer {
5-
6-
/**
7-
*
8-
* @param {Object.<string, function>} renderRules
9-
* @param {any} style
10-
*/
13+
/**
14+
*
15+
* @param {Object.<string, function>} renderRules
16+
* @param {any} style
17+
*/
1118
constructor(renderRules, style) {
1219
this._renderRules = renderRules;
1320
this._style = style;
1421
}
1522

1623
/**
17-
*
18-
* @param {string} type
19-
* @return {string}
20-
*/
24+
*
25+
* @param {string} type
26+
* @return {string}
27+
*/
2128
getRenderFunction = type => {
2229
const renderFunction = this._renderRules[type];
30+
2331
if (!renderFunction) {
24-
throw new Error(`${type} renderer not defined`);
32+
throw new Error(
33+
`${type} renderRule not defined example: <Markdown rules={renderRules}>`
34+
);
2535
}
2636
return renderFunction;
2737
};
2838

2939
/**
30-
*
31-
* @param node
32-
* @param parentNodes
33-
* @return {*}
34-
*/
40+
*
41+
* @param node
42+
* @param parentNodes
43+
* @return {*}
44+
*/
3545
renderNode = (node, parentNodes) => {
3646
const renderFunction = this.getRenderFunction(node.type);
3747

3848
const parents = [...parentNodes];
3949
parents.unshift(node);
4050

41-
if (node.type === 'text') {
51+
if (node.type === "text") {
4252
return renderFunction(node, [], parentNodes, this._style);
4353
}
4454

@@ -55,10 +65,8 @@ export default class AstRenderer {
5565
* @return {*}
5666
*/
5767
render = nodes => {
58-
const children = nodes.map(value => {
59-
return this.renderNode(value, []);
60-
});
61-
const renderFunction = this.getRenderFunction('root');
62-
return renderFunction(children);
68+
console.log(nodes);
69+
const children = nodes.map(value => this.renderNode(value, []));
70+
return rootRenderRule(children);
6371
};
6472
}

src/lib/parser.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from "react";
2+
import { View } from "react-native";
3+
import tokensToAST from "./util/tokensToAST";
4+
import removeInlineTokens from "./util/removeInlineTokens";
5+
import groupTextTokens from "./util/groupTextTokens";
6+
import getTokenTypeByToken from "./util/getTokenTypeByToken";
7+
8+
export function stringToTokens(source, markdownIt) {
9+
let result = [];
10+
try {
11+
result = markdownIt.parse(source, {});
12+
} catch (err) {
13+
console.warn(err);
14+
}
15+
16+
result.forEach(token => (token.type = getTokenTypeByToken(token)));
17+
18+
return result;
19+
}
20+
21+
/**
22+
*
23+
* @param {string} source
24+
* @param {function} [renderer]
25+
* @param {AstRenderer} [markdownIt]
26+
* @return {View}
27+
*/
28+
export function parser(source, renderer, markdownIt) {
29+
const tokens = groupTextTokens(
30+
removeInlineTokens(stringToTokens(source, markdownIt))
31+
);
32+
const asttree = tokensToAST(tokens);
33+
34+
return renderer(asttree);
35+
}
Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ const renderRules = {
1111
unknown: (node, children, parent, styles) => {
1212
return (
1313
<View key={node.key}>
14-
<Text>unknown {node.type}</Text> {children}
14+
<Text>{node.type}</Text>
1515
</View>
1616
);
1717
},
1818

19-
// `root` is a special case.
20-
root: children => <View key={getUniqueID()}>{children}</View>,
21-
2219
textgroup: (node, children, parent, styles) => {
2320
return <Text key={node.key}>{children}</Text>;
2421
},
@@ -114,40 +111,61 @@ const renderRules = {
114111
{children}
115112
</View>
116113
),
117-
code: (node, children, parent, styles) => {
118-
switch (node.sourceType) {
119-
case "code_inline": {
120-
return (
121-
<Text key={node.key} style={styles.codeInline}>
122-
{node.content}
123-
</Text>
124-
);
125-
break;
126-
}
127-
case "code_block": {
128-
return (
129-
<Text key={node.key} style={styles.codeBlock}>
130-
{node.content}
131-
</Text>
132-
);
133-
break;
134-
}
135-
case "fence": {
136-
return (
137-
<Text key={node.key} style={styles.codeBlock}>
138-
{node.content}
139-
</Text>
140-
);
141-
break;
142-
}
143-
}
144-
114+
code_inline: (node, children, parent, styles) => {
145115
return (
146-
<View key={node.key} style={styles.codeInline}>
147-
{children}
148-
</View>
116+
<Text key={node.key} style={styles.codeInline}>
117+
{node.content}
118+
</Text>
119+
);
120+
},
121+
code_block: (node, children, parent, styles) => {
122+
return (
123+
<Text key={node.key} style={styles.codeBlock}>
124+
{node.content}
125+
</Text>
126+
);
127+
},
128+
fence: (node, children, parent, styles) => {
129+
return (
130+
<Text key={node.key} style={styles.codeBlock}>
131+
{node.content}
132+
</Text>
149133
);
150134
},
135+
// code: (node, children, parent, styles) => {
136+
// switch (node.sourceType) {
137+
// case "code_inline": {
138+
// return (
139+
// <Text key={node.key} style={styles.codeInline}>
140+
// {node.content}
141+
// </Text>
142+
// );
143+
// break;
144+
// }
145+
// case "code_block": {
146+
// return (
147+
// <Text key={node.key} style={styles.codeBlock}>
148+
// {node.content}
149+
// </Text>
150+
// );
151+
// break;
152+
// }
153+
// case "fence": {
154+
// return (
155+
// <Text key={node.key} style={styles.codeBlock}>
156+
// {node.content}
157+
// </Text>
158+
// );
159+
// break;
160+
// }
161+
// }
162+
//
163+
// return (
164+
// <View key={node.key} style={styles.codeInline}>
165+
// {children}
166+
// </View>
167+
// );
168+
// },
151169
pre: (node, children, parent, styles) => (
152170
<View key={node.key} style={styles.pre}>
153171
{children}
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default class Token {
22
constructor(type, nesting = 0, children = null) {
3-
this.tag = type;
3+
this.type = type;
44
this.nesting = nesting;
55
this.children = children;
66
}

0 commit comments

Comments
 (0)