Browse Source

support markdown in comments

Lucas Stadler 11 years ago
parent
commit
52d1b31722
2 changed files with 21 additions and 8 deletions
  1. 20 8
      js/react/entry.js
  2. 1 0
      js/react/package.json

+ 20 - 8
js/react/entry.js

@ -1,6 +1,7 @@
1 1
// following along http://facebook.github.io/react/docs/tutorial.html
2 2
3 3
import React from "react";
4
import marked from "marked";
4 5
5 6
require('./style.css');
6 7
@ -16,13 +17,17 @@ class CommentForm extends React.Component {
16 17
17 18
class CommentList extends React.Component {
18 19
	render() {
20
		let commentNodes = this.props.data.map((comment) => {
21
			return (
22
				<Comment author={comment.author}>
23
					{comment.text}
24
				</Comment>
25
			);
26
		});
27
19 28
		return (
20 29
			<div className="comment-list">
21
				<Comment author="Alice">Down the... halfpipe!</Comment>
22
				<Comment author="PP">Alice, what blasphemy!</Comment>
23
				<Comment author="Maybe Not Lewis">Let her be herself, Peter,
24
					we let you jump around all the time as well...
25
				</Comment>
30
				{commentNodes}
26 31
			</div>
27 32
		);
28 33
	}
@ -33,7 +38,7 @@ class CommentBox extends React.Component {
33 38
		return (
34 39
			<div className="comment-box">
35 40
				<h1>Comments</h1>
36
				<CommentList />
41
				<CommentList data={this.props.data} />
37 42
				<CommentForm />
38 43
			</div>
39 44
		);
@ -42,13 +47,20 @@ class CommentBox extends React.Component {
42 47
43 48
class Comment extends React.Component {
44 49
	render() {
50
		let rawMarkup = marked(this.props.children.toString());
45 51
		return (
46 52
			<div className="comment">
47 53
				<h2 className="comment-author">{this.props.author}</h2>
48
				{this.props.children}
54
				<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
49 55
			</div>
50 56
		);
51 57
	}
52 58
}
53 59
54
React.render(<CommentBox />, document.getElementById("content"));
60
var data = [
61
	{author: "Alice", text: "Down the... halfpipe!"},
62
	{author: "PP", text: "Alice, what **blasphemy**!"},
63
	{author: "Maybe Not Lewis", text: "Let her be herself, Peter, we let *you* jump around all the time as well..."}
64
];
65
66
React.render(<CommentBox data={data} />, document.getElementById("content"));

+ 1 - 0
js/react/package.json

@ -6,6 +6,7 @@
6 6
    "6to5-core": "^3.5.3",
7 7
    "6to5-loader": "^3.0.0",
8 8
    "css-loader": "^0.9.1",
9
    "marked": "^0.3.3",
9 10
    "react": "^0.13.0-beta.1",
10 11
    "style-loader": "^0.8.3",
11 12
    "webpack": "^1.5.3"