Skip to content

Commit be03fa7

Browse files
committed
Merge pull request #3663 from spicyj/san-md
[docs] Use marked instead of Showdown and escape HTML
2 parents d8117d0 + 36aefcb commit be03fa7

File tree

6 files changed

+22
-1322
lines changed

6 files changed

+22
-1322
lines changed

docs/_js/examples/markdown.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
var MARKDOWN_COMPONENT = `
2-
var converter = new Showdown.converter();
3-
42
var MarkdownEditor = React.createClass({
53
getInitialState: function() {
64
return {value: 'Type some *markdown* here!'};
@@ -20,7 +18,7 @@ var MarkdownEditor = React.createClass({
2018
<div
2119
className="content"
2220
dangerouslySetInnerHTML={{
23-
__html: converter.makeHtml(this.state.value)
21+
__html: marked(this.state.value, {sanitize: true})
2422
}}
2523
/>
2624
</div>

docs/_layouts/default.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
<script type="text/javascript" src="/react/js/react.js"></script>
3434
<script type="text/javascript" src="/react/js/JSXTransformer.js"></script>
3535
<script type="text/javascript" src="/react/js/live_editor.js"></script>
36-
<script type="text/javascript" src="/react/js/showdown.js"></script>
3736
</head>
3837
<body>
3938

docs/docs/tutorial.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Note that we have passed some data from the parent `CommentList` component to th
212212

213213
Markdown is a simple way to format your text inline. For example, surrounding text with asterisks will make it emphasized.
214214

215-
First, add the third-party **Showdown** library to your application. This is a JavaScript library which takes Markdown text and converts it to raw HTML. This requires a script tag in your head (which we have already included in the React playground):
215+
First, add the third-party library **marked** to your application. This is a JavaScript library which takes Markdown text and converts it to raw HTML. This requires a script tag in your head (which we have already included in the React playground):
216216

217217
```html{7}
218218
<!-- index.html -->
@@ -221,41 +221,39 @@ First, add the third-party **Showdown** library to your application. This is a J
221221
<script src="https://fb.me/react-{{site.react_version}}.js"></script>
222222
<script src="https://fb.me/JSXTransformer-{{site.react_version}}.js"></script>
223223
<script src="https://code.jquery.com/jquery-1.10.0.min.js"></script>
224-
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
224+
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.2/marked.min.js"></script>
225225
</head>
226226
```
227227

228228
Next, let's convert the comment text to Markdown and output it:
229229

230-
```javascript{2,10}
230+
```javascript{9}
231231
// tutorial6.js
232-
var converter = new Showdown.converter();
233232
var Comment = React.createClass({
234233
render: function() {
235234
return (
236235
<div className="comment">
237236
<h2 className="commentAuthor">
238237
{this.props.author}
239238
</h2>
240-
{converter.makeHtml(this.props.children.toString())}
239+
{marked(this.props.children.toString())}
241240
</div>
242241
);
243242
}
244243
});
245244
```
246245

247-
All we're doing here is calling the Showdown library. We need to convert `this.props.children` from React's wrapped text to a raw string that Showdown will understand so we explicitly call `toString()`.
246+
All we're doing here is calling the marked library. We need to convert `this.props.children` from React's wrapped text to a raw string that marked will understand so we explicitly call `toString()`.
248247

249248
But there's a problem! Our rendered comments look like this in the browser: "`<p>`This is `<em>`another`</em>` comment`</p>`". We want those tags to actually render as HTML.
250249

251250
That's React protecting you from an XSS attack. There's a way to get around it but the framework warns you not to use it:
252251

253-
```javascript{5,11}
252+
```javascript{4,10}
254253
// tutorial7.js
255-
var converter = new Showdown.converter();
256254
var Comment = React.createClass({
257255
render: function() {
258-
var rawMarkup = converter.makeHtml(this.props.children.toString());
256+
var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
259257
return (
260258
<div className="comment">
261259
<h2 className="commentAuthor">
@@ -268,9 +266,9 @@ var Comment = React.createClass({
268266
});
269267
```
270268

271-
This is a special API that intentionally makes it difficult to insert raw HTML, but for Showdown we'll take advantage of this backdoor.
269+
This is a special API that intentionally makes it difficult to insert raw HTML, but for marked we'll take advantage of this backdoor.
272270

273-
**Remember:** by using this feature you're relying on Showdown to be secure.
271+
**Remember:** by using this feature you're relying on marked to be secure. In this case, we pass `sanitize: true` which tells marked to escape any HTML markup in the source instead of passing it through unchanged.
274272

275273
### Hook up the data model
276274

docs/index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,17 @@ id: home
7272
<h3>A Component Using External Plugins</h3>
7373
<p>
7474
React is flexible and provides hooks that allow you to interface with
75-
other libraries and frameworks. This example uses Showdown, an external
75+
other libraries and frameworks. This example uses **marked**, an external
7676
Markdown library, to convert the textarea's value in real-time.
7777
</p>
7878
<div id="markdownExample"></div>
7979
</div>
8080
</div>
81-
<script type="text/javascript" src="js/examples/hello.js"></script>
82-
<script type="text/javascript" src="js/examples/timer.js"></script>
83-
<script type="text/javascript" src="js/examples/todo.js"></script>
84-
<script type="text/javascript" src="js/examples/markdown.js"></script>
81+
<script type="text/javascript" src="/react/js/marked.min.js"></script>
82+
<script type="text/javascript" src="/react/js/examples/hello.js"></script>
83+
<script type="text/javascript" src="/react/js/examples/timer.js"></script>
84+
<script type="text/javascript" src="/react/js/examples/todo.js"></script>
85+
<script type="text/javascript" src="/react/js/examples/markdown.js"></script>
8586
</section>
8687
<hr class="home-divider" />
8788
<section class="home-bottom-section">

0 commit comments

Comments
 (0)