From 90b301d0a03b8b23ce755c31ec6dac15cf69dc49 Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Thu, 8 Dec 2016 17:41:55 +0800 Subject: Enable passing options for parsing Markdown. Closes #1163, #1495. --- README.md | 12 ++++++++++++ plugin/markdown/markdown.js | 32 +++++++++++++++++++------------- test/test-markdown-options.html | 41 +++++++++++++++++++++++++++++++++++++++++ test/test-markdown-options.js | 26 ++++++++++++++++++++++++++ test/test-markdown.html | 2 +- 5 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 test/test-markdown-options.html create mode 100644 test/test-markdown-options.js diff --git a/README.md b/README.md index b0257cc..1c55623 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,18 @@ Special syntax (in html comment) is available for adding attributes to the slide ``` +#### Configuring `marked` + +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): + +```javascript +Reveal.initialize({ + // Options which are passed into marked + // See https://github.com/chjj/marked#options-1 + markdown: { + smartypants: true + } +}); ### Configuration diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 124aa75..29aabf5 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -17,18 +17,6 @@ } }( this, function( marked ) { - if( typeof marked === 'undefined' ) { - throw 'The reveal.js Markdown plugin requires marked to be loaded'; - } - - if( typeof hljs !== 'undefined' ) { - marked.setOptions({ - highlight: function( code, lang ) { - return hljs.highlightAuto( code, [lang] ).value; - } - }); - } - var DEFAULT_SLIDE_SEPARATOR = '^\r?\n---\r?\n$', DEFAULT_NOTES_SEPARATOR = 'note:', DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '\\\.element\\\s*?(.+?)$', @@ -189,7 +177,7 @@ markdownSections += '
'; sectionStack[i].forEach( function( child ) { - markdownSections += '
' + createMarkdownSlide( child, options ) + '
'; + markdownSections += '
' + createMarkdownSlide( child, options ) + '
'; } ); markdownSections += '
'; @@ -391,6 +379,24 @@ return { initialize: function() { + if( typeof marked === 'undefined' ) { + throw 'The reveal.js Markdown plugin requires marked to be loaded'; + } + + if( typeof hljs !== 'undefined' ) { + marked.setOptions({ + highlight: function( code, lang ) { + return hljs.highlightAuto( code, [lang] ).value; + } + }); + } + + var options = Reveal.getConfig().markdown; + + if ( options ) { + marked.setOptions( options ); + } + processSlides(); convertSlides(); }, diff --git a/test/test-markdown-options.html b/test/test-markdown-options.html new file mode 100644 index 0000000..5b3be97 --- /dev/null +++ b/test/test-markdown-options.html @@ -0,0 +1,41 @@ + + + + + + + reveal.js - Test Markdown Options + + + + + + + +
+
+ + + + + + + + + + + diff --git a/test/test-markdown-options.js b/test/test-markdown-options.js new file mode 100644 index 0000000..3ae1350 --- /dev/null +++ b/test/test-markdown-options.js @@ -0,0 +1,26 @@ +Reveal.addEventListener( 'ready', function() { + + QUnit.module( 'Markdown' ); + + test( 'Options are set', function() { + strictEqual( marked.defaults.smartypants, true ); + }); + + test( 'Smart quotes are activated', function() { + var text = document.querySelector( '.reveal .slides>section>p' ).textContent; + + strictEqual( /['"]/.test( text ), false ); + strictEqual( /[“”‘’]/.test( text ), true ); + }); + +} ); + +Reveal.initialize({ + dependencies: [ + { src: '../plugin/markdown/marked.js' }, + { src: '../plugin/markdown/markdown.js' }, + ], + markdown: { + smartypants: true + } +}); diff --git a/test/test-markdown.html b/test/test-markdown.html index 7ff0efe..52b39ff 100644 --- a/test/test-markdown.html +++ b/test/test-markdown.html @@ -13,7 +13,7 @@
-
+