Skip to content

Commit 90b301d

Browse files
committed
Enable passing options for parsing Markdown.
Closes #1163, #1495.
1 parent 2bd6b56 commit 90b301d

File tree

5 files changed

+99
-14
lines changed

5 files changed

+99
-14
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ Special syntax (in html comment) is available for adding attributes to the slide
160160
</section>
161161
```
162162

163+
#### Configuring `marked`
164+
165+
We use [marked](https://github.com/chjj/marked) to parse Markdown. To customise marked's rendering, you can pass in options when [configuring Reveal](#configuration):
166+
167+
```javascript
168+
Reveal.initialize({
169+
// Options which are passed into marked
170+
// See https://github.com/chjj/marked#options-1
171+
markdown: {
172+
smartypants: true
173+
}
174+
});
163175

164176
### Configuration
165177

plugin/markdown/markdown.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@
1717
}
1818
}( this, function( marked ) {
1919

20-
if( typeof marked === 'undefined' ) {
21-
throw 'The reveal.js Markdown plugin requires marked to be loaded';
22-
}
23-
24-
if( typeof hljs !== 'undefined' ) {
25-
marked.setOptions({
26-
highlight: function( code, lang ) {
27-
return hljs.highlightAuto( code, [lang] ).value;
28-
}
29-
});
30-
}
31-
3220
var DEFAULT_SLIDE_SEPARATOR = '^\r?\n---\r?\n$',
3321
DEFAULT_NOTES_SEPARATOR = 'note:',
3422
DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '\\\.element\\\s*?(.+?)$',
@@ -189,7 +177,7 @@
189177
markdownSections += '<section '+ options.attributes +'>';
190178

191179
sectionStack[i].forEach( function( child ) {
192-
markdownSections += '<section data-markdown>' + createMarkdownSlide( child, options ) + '</section>';
180+
markdownSections += '<section data-markdown>' + createMarkdownSlide( child, options ) + '</section>';
193181
} );
194182

195183
markdownSections += '</section>';
@@ -391,6 +379,24 @@
391379
return {
392380

393381
initialize: function() {
382+
if( typeof marked === 'undefined' ) {
383+
throw 'The reveal.js Markdown plugin requires marked to be loaded';
384+
}
385+
386+
if( typeof hljs !== 'undefined' ) {
387+
marked.setOptions({
388+
highlight: function( code, lang ) {
389+
return hljs.highlightAuto( code, [lang] ).value;
390+
}
391+
});
392+
}
393+
394+
var options = Reveal.getConfig().markdown;
395+
396+
if ( options ) {
397+
marked.setOptions( options );
398+
}
399+
394400
processSlides();
395401
convertSlides();
396402
},

test/test-markdown-options.html

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
7+
<title>reveal.js - Test Markdown Options</title>
8+
9+
<link rel="stylesheet" href="../css/reveal.css">
10+
<link rel="stylesheet" href="qunit-1.12.0.css">
11+
</head>
12+
13+
<body style="overflow: auto;">
14+
15+
<div id="qunit"></div>
16+
<div id="qunit-fixture"></div>
17+
18+
<div class="reveal" style="display: none;">
19+
20+
<div class="slides">
21+
22+
<section data-markdown>
23+
<script type="text/template">
24+
## Testing Markdown Options
25+
26+
This "slide" should contain 'smart' quotes.
27+
</script>
28+
</section>
29+
30+
</div>
31+
32+
</div>
33+
34+
<script src="../lib/js/head.min.js"></script>
35+
<script src="../js/reveal.js"></script>
36+
<script src="qunit-1.12.0.js"></script>
37+
38+
<script src="test-markdown-options.js"></script>
39+
40+
</body>
41+
</html>

test/test-markdown-options.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Reveal.addEventListener( 'ready', function() {
2+
3+
QUnit.module( 'Markdown' );
4+
5+
test( 'Options are set', function() {
6+
strictEqual( marked.defaults.smartypants, true );
7+
});
8+
9+
test( 'Smart quotes are activated', function() {
10+
var text = document.querySelector( '.reveal .slides>section>p' ).textContent;
11+
12+
strictEqual( /['"]/.test( text ), false );
13+
strictEqual( /[]/.test( text ), true );
14+
});
15+
16+
} );
17+
18+
Reveal.initialize({
19+
dependencies: [
20+
{ src: '../plugin/markdown/marked.js' },
21+
{ src: '../plugin/markdown/markdown.js' },
22+
],
23+
markdown: {
24+
smartypants: true
25+
}
26+
});

test/test-markdown.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<body style="overflow: auto;">
1414

1515
<div id="qunit"></div>
16-
<div id="qunit-fixture"></div>
16+
<div id="qunit-fixture"></div>
1717

1818
<div class="reveal" style="display: none;">
1919

0 commit comments

Comments
 (0)