markdown.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * The reveal.js markdown plugin. Handles parsing of
  3. * markdown inside of presentations as well as loading
  4. * of external markdown documents.
  5. */
  6. (function(){
  7. if( typeof marked === 'undefined' ) {
  8. throw 'The reveal.js Markdown plugin requires marked to be loaded';
  9. }
  10. if( typeof hljs !== 'undefined' ) {
  11. marked.setOptions({
  12. highlight: function( lang, code ) {
  13. return hljs.highlightAuto( lang, code ).value;
  14. }
  15. });
  16. }
  17. /**
  18. * Retrieves the markdown contents of a slide section
  19. * element. Normalizes leading tabs/whitespace.
  20. */
  21. function getMarkdownFromSlide( section ) {
  22. var template = section.querySelector( 'script' );
  23. // strip leading whitespace so it isn't evaluated as code
  24. var text = ( template || section ).textContent;
  25. var leadingWs = text.match( /^\n?(\s*)/ )[1].length,
  26. leadingTabs = text.match( /^\n?(\t*)/ )[1].length;
  27. if( leadingTabs > 0 ) {
  28. text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' );
  29. }
  30. else if( leadingWs > 1 ) {
  31. text = text.replace( new RegExp('\\n? {' + leadingWs + '}','g'), '\n' );
  32. }
  33. return text;
  34. }
  35. /**
  36. * Given a markdown slide section element, this will
  37. * return all arguments that aren't related to markdown
  38. * parsing. Used to forward any other user-defined arguments
  39. * to the output markdown slide.
  40. */
  41. function getForwardedAttributes( section ) {
  42. var attributes = section.attributes;
  43. var result = [];
  44. for( var i = 0, len = attributes.length; i < len; i++ ) {
  45. var name = attributes[i].name,
  46. value = attributes[i].value;
  47. // disregard attributes that are used for markdown loading/parsing
  48. if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue;
  49. if( value ) {
  50. result.push( name + '=' + value );
  51. }
  52. else {
  53. result.push( name );
  54. }
  55. }
  56. return result.join( ' ' );
  57. }
  58. /**
  59. * Helper function for constructing a markdown slide.
  60. */
  61. function createMarkdownSlide( data ) {
  62. var content = data.content || data;
  63. if( data.notes ) {
  64. content += '<aside class="notes" data-markdown>' + data.notes + '</aside>';
  65. }
  66. return '<script type="text/template">' + content + '</script>';
  67. }
  68. /**
  69. * Parses a data string into multiple slides based
  70. * on the passed in separator arguments.
  71. */
  72. function slidifyMarkdown( markdown, options ) {
  73. options = options || {};
  74. options.separator = options.separator || '^\n---\n$';
  75. options.notesSeparator = options.notesSeparator || 'note:';
  76. options.attributes = options.attributes || '';
  77. var separatorRegex = new RegExp( options.separator + ( options.verticalSeparator ? '|' + options.verticalSeparator : '' ), 'mg' ),
  78. horizontalSeparatorRegex = new RegExp( options.separator ),
  79. notesSeparatorRegex = new RegExp( options.notesSeparator, 'mgi' );
  80. var matches,
  81. noteMatch,
  82. lastIndex = 0,
  83. isHorizontal,
  84. wasHorizontal = true,
  85. content,
  86. notes,
  87. slide,
  88. sectionStack = [];
  89. // iterate until all blocks between separators are stacked up
  90. while( matches = separatorRegex.exec( markdown ) ) {
  91. notes = null;
  92. // determine direction (horizontal by default)
  93. isHorizontal = horizontalSeparatorRegex.test( matches[0] );
  94. if( !isHorizontal && wasHorizontal ) {
  95. // create vertical stack
  96. sectionStack.push( [] );
  97. }
  98. // pluck slide content from markdown input
  99. content = markdown.substring( lastIndex, matches.index );
  100. noteMatch = content.split( notesSeparatorRegex );
  101. if( noteMatch.length === 2 ) {
  102. content = noteMatch[0];
  103. notes = noteMatch[1].trim();
  104. }
  105. slide = {
  106. content: content,
  107. notes: notes || ''
  108. };
  109. if( isHorizontal && wasHorizontal ) {
  110. // add to horizontal stack
  111. sectionStack.push( slide );
  112. }
  113. else {
  114. // add to vertical stack
  115. sectionStack[sectionStack.length-1].push( slide );
  116. }
  117. lastIndex = separatorRegex.lastIndex;
  118. wasHorizontal = isHorizontal;
  119. }
  120. // add the remaining slide
  121. ( wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1] ).push( markdown.substring( lastIndex ) );
  122. var markdownSections = '';
  123. // flatten the hierarchical stack, and insert <section data-markdown> tags
  124. for( var i = 0, len = sectionStack.length; i < len; i++ ) {
  125. // vertical
  126. if( sectionStack[i].propertyIsEnumerable( length ) && typeof sectionStack[i].splice === 'function' ) {
  127. markdownSections += '<section '+ options.attributes +'>' +
  128. '<section data-markdown>' + sectionStack[i].map( createMarkdownSlide ).join( '</section><section data-markdown>' ) + '</section>' +
  129. '</section>';
  130. }
  131. else {
  132. markdownSections += '<section '+ options.attributes +' data-markdown>' + createMarkdownSlide( sectionStack[i] ) + '</section>';
  133. }
  134. }
  135. return markdownSections;
  136. }
  137. function loadExternalMarkdown() {
  138. var sections = document.querySelectorAll( '[data-markdown]'),
  139. section;
  140. for( var i = 0, len = sections.length; i < len; i++ ) {
  141. section = sections[i];
  142. if( section.getAttribute( 'data-markdown' ).length ) {
  143. var xhr = new XMLHttpRequest(),
  144. url = section.getAttribute( 'data-markdown' );
  145. datacharset = section.getAttribute( 'data-charset' );
  146. // see https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute#Notes
  147. if( datacharset != null && datacharset != '' ) {
  148. xhr.overrideMimeType( 'text/html; charset=' + datacharset );
  149. }
  150. xhr.onreadystatechange = function() {
  151. if( xhr.readyState === 4 ) {
  152. if ( xhr.status >= 200 && xhr.status < 300 ) {
  153. section.outerHTML = slidifyMarkdown( xhr.responseText, {
  154. separator: section.getAttribute( 'data-separator' ),
  155. verticalSeparator: section.getAttribute( 'data-vertical' ),
  156. notesSeparator: section.getAttribute( 'data-notes' ),
  157. attributes: getForwardedAttributes( section )
  158. });
  159. }
  160. else {
  161. section.outerHTML = '<section data-state="alert">' +
  162. 'ERROR: The attempt to fetch ' + url + ' failed with HTTP status ' + xhr.status + '.' +
  163. 'Check your browser\'s JavaScript console for more details.' +
  164. '<p>Remember that you need to serve the presentation HTML from a HTTP server.</p>' +
  165. '</section>';
  166. }
  167. }
  168. };
  169. xhr.open( 'GET', url, false );
  170. try {
  171. xhr.send();
  172. }
  173. catch ( e ) {
  174. alert( 'Failed to get the Markdown file ' + url + '. Make sure that the presentation and the file are served by a HTTP server and the file can be found there. ' + e );
  175. }
  176. }
  177. else if( section.getAttribute( 'data-separator' ) ) {
  178. section.outerHTML = slidifyMarkdown( getMarkdownFromSlide( section ), {
  179. separator: section.getAttribute( 'data-separator' ),
  180. verticalSeparator: section.getAttribute( 'data-vertical' ),
  181. notesSeparator: section.getAttribute( 'data-notes' ),
  182. attributes: getForwardedAttributes( section )
  183. });
  184. }
  185. }
  186. }
  187. function convertMarkdownToHTML() {
  188. var sections = document.querySelectorAll( '[data-markdown]');
  189. for( var i = 0, len = sections.length; i < len; i++ ) {
  190. var section = sections[i];
  191. var notes = section.querySelector( 'aside.notes' );
  192. var markdown = getMarkdownFromSlide( section );
  193. section.innerHTML = marked( markdown );
  194. // If there were notes, we need to re-add them after
  195. // having overwritten the section's HTML
  196. if( notes ) {
  197. section.appendChild( notes );
  198. }
  199. }
  200. }
  201. loadExternalMarkdown();
  202. convertMarkdownToHTML();
  203. })();