Skip to content

Commit c9fbf65

Browse files
committed
Completely refactored code, now using a new markdown engine.
1 parent c657a83 commit c9fbf65

File tree

5 files changed

+83
-157
lines changed

5 files changed

+83
-157
lines changed

Markdown.js

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
11
var React = require('react-native');
22
var {
3-
Image,
4-
Text,
5-
View,
3+
View
64
} = React;
75
var _ = require('lodash');
8-
var Buffer = require('buffer');
9-
var marked = require('marked');
10-
var renderer = require('./renderer');
11-
12-
function transform(jsxString, scope) {
13-
var header = '/** @jsx React.DOM */';
14-
jsxString = header + '\n' + jsxString;
15-
return require('react-tools').transform(jsxString);
16-
}
17-
18-
function exec(jsxString, lexicalScope) {
19-
var jsx = transform(jsxString);
20-
21-
var thunkTemplate = [
22-
'(function () { ',
23-
' <%- vars %> ',
24-
' return ( ',
25-
' <%= code %> ',
26-
' ); ',
27-
'}) '
28-
].join('\n');
29-
30-
var lexicalVarTemplate = 'var <%- key %> = lexicalScope.<%- key %>;';
31-
var lexicalVars = _.map(lexicalScope, function (val, key) {
32-
return _.template(lexicalVarTemplate)({ key: key });
33-
});
34-
35-
var thunkCode = _.template(thunkTemplate)({
36-
vars: lexicalVars.join('\n'),
37-
code: jsx
38-
});
39-
40-
var thunk = eval(thunkCode);
41-
42-
return thunk.apply(null);
43-
}
6+
var SimpleMarkdown = require('simple-markdown');
447

458
var styles = {
46-
emphasis: {
9+
u: {
4710
fontFamily: 'HelveticaNeue-Italic'
4811
},
4912
paragraph: {
@@ -54,16 +17,25 @@ var styles = {
5417
}
5518
};
5619

57-
var Markdown = React.createClass({
5820

59-
render: function() {
21+
var Markdown = React.createClass({
6022

61-
var children = this.props.children.toString();
23+
componentWillMount: function() {
24+
var rules = require('./rules')(_.merge(styles, this.props.style));
25+
rules = _.merge(SimpleMarkdown.defaultRules, rules);
6226

63-
var jsxString = marked(children, { renderer: renderer })
64-
var jsx = exec(jsxString, { styles: _.merge(styles, this.props.style) });
27+
var parser = SimpleMarkdown.parserFor(rules);
28+
this.parse = function(source) {
29+
var blockSource = source + '\n\n';
30+
return parser(blockSource, {inline: false});
31+
};
32+
this.renderer = SimpleMarkdown.outputFor(SimpleMarkdown.ruleOutput(rules, 'react'));
33+
},
6534

66-
return jsx;
35+
render: function() {
36+
var child = this.props.children.toString();
37+
var tree = this.parse(child);
38+
return <View>{this.renderer(tree)}</View>;
6739
}
6840
});
6941

README.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
A component for rendering Markdown in React Native. This component is still in early development.
44

5-
## Known issues
6-
7-
**~~Warning: This module currently uses `eval` and is susceptible to JS injection. Working on a fix for this.~~**
8-
9-
**I'm not convinced we can avoid using `eval`, though I believe I have resolved the injection issue.**
10-
115
## Getting started
126

137
1. `npm install react-native-markdown --save`
@@ -49,24 +43,26 @@ Default style properties will be applied to the markdown. You will likely want t
4943

5044
*Note: The text inside the parentheses denotes the element type.*
5145

52-
- `emphasis` (`<Text>`)
53-
- `paragraph` (`<Text>`)
54-
- `strong` (`<Text>`)
55-
- `strong` (`<Text>`)
56-
- `emphasis` (`<Text>`)
57-
- `codespan` (`<Text>`)
58-
- `break` (`<View>`)
59-
- `deleted` (`<Text>`)
60-
- `link` (`Not implemented`)
46+
- `autolink` (`Not implemented`)
47+
- `blockQuote` (`Not implemented`)
48+
- `br` (`Not implemented`)
49+
- `codeBlock` (`Not implemented`)
50+
- `def` (`Not implemented`)
51+
- `del` (`Not implemented`)
52+
- `em` (`Not implemented`)
53+
- `fence` (`Not implemented`)
54+
- `heading` (`Not implemented`) - Also `heading1` through `heading6`
55+
- `hr` (`Not implemented`)
6156
- `image` (`<Image>`)
62-
- `code` (`<Text>`)
63-
- `blockquote` (`Not implemented`)
64-
- `html` (`<Text>`)
65-
- `heading` (`<Text>`) - Also `heading1` through `heading6`
66-
- `horizontalRule` (`<View>`)
57+
- `inlineCode` (`Not implemented`)
58+
- `link` (`Not implemented`)
6759
- `list` (`Not implemented`)
68-
- `listitem` (`Not implemented`)
60+
- `mailto` (`Not implemented`)
61+
- `newline` (`Not implemented`)
6962
- `paragraph` (`<Text>`)
70-
- `table` (`Not implemented`)
71-
- `tablerow` (`Not implemented`)
72-
- `tablecell` (`Not implemented`)
63+
- `refimage` (`Not implemented`)
64+
- `reflink` (`Not implemented`)
65+
- `strong` (`<Text>`)
66+
- `text` (`<Text>`)
67+
- `u` (`<Text>`)
68+
- `url` (`Not implemented`)

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
"main": "Markdown.js",
1010
"author": "Lochlan Wansbrough <lochie@live.com> (http://lwansbrough.com)",
1111
"dependencies": {
12-
"buffer": "^3.1.2",
1312
"lodash": "^3.6.0",
14-
"marked": "https://github.com/lwansbrough/marked.git",
15-
"react-native": "^0.3.1"
13+
"react-native": "^0.3.1",
14+
"simple-markdown": "git://github.com/lwansbrough/simple-markdown.git"
1615
}
1716
}

renderer.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

rules.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var React = require('react-native');
2+
var {
3+
Image,
4+
Text,
5+
View,
6+
} = React;
7+
var SimpleMarkdown = require('simple-markdown');
8+
9+
module.exports = function(styles) {
10+
return {
11+
image: {
12+
react: function(node, output) {
13+
return React.createElement(Image, {
14+
source: { uri: node.target },
15+
style: styles.image
16+
}, output(node.content));
17+
}
18+
},
19+
paragraph: {
20+
// Finally transform this syntax node into a
21+
// React element
22+
react: function(node, output) {
23+
return React.createElement(Text, { style: styles.paragraph }, output(node.content));
24+
}
25+
},
26+
text: {
27+
28+
// Finally transform this syntax node into a
29+
// React element
30+
react: function(node, output) {
31+
return React.createElement(Text, { style: styles.text }, node.content);
32+
}
33+
},
34+
u: {
35+
36+
// Finally transform this syntax node into a
37+
// React element
38+
react: function(node, output) {
39+
return React.createElement(Text, { style: styles.u }, output(node.content));
40+
}
41+
},
42+
}
43+
};

0 commit comments

Comments
 (0)