markdown.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // From https://gist.github.com/1343518
  2. // Modified by Hakim to handle Markdown indented with tabs
  3. (function(){
  4. if( typeof marked === 'undefined' ) {
  5. throw 'The reveal.js Markdown plugin requires marked to be loaded';
  6. }
  7. var stripLeadingWhitespace = function(section) {
  8. var template = section.querySelector( 'script' );
  9. // strip leading whitespace so it isn't evaluated as code
  10. var text = ( template || section ).textContent;
  11. var leadingWs = text.match(/^\n?(\s*)/)[1].length,
  12. leadingTabs = text.match(/^\n?(\t*)/)[1].length;
  13. if( leadingTabs > 0 ) {
  14. text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' );
  15. }
  16. else if( leadingWs > 1 ) {
  17. text = text.replace( new RegExp('\\n? {' + leadingWs + '}','g'), '\n' );
  18. }
  19. return text;
  20. };
  21. var twrap = function(el) {
  22. var cnt = el.content || el;
  23. cnt += el.asideContent ? ('<aside class="notes" data-markdown>' + el.asideContent + '</aside>') : '';
  24. return '<script type="text/template">' + cnt + '</script>';
  25. };
  26. var getForwardedAttributes = function(section) {
  27. var attributes = section.attributes;
  28. var result = [];
  29. for( var i = 0, len = attributes.length; i < len; i++ ) {
  30. var name = attributes[i].name,
  31. value = attributes[i].value;
  32. // disregard attributes that are used for markdown loading/parsing
  33. if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue;
  34. if( value ) {
  35. result.push( name + '=' + value );
  36. }
  37. else {
  38. result.push( name );
  39. }
  40. }
  41. return result.join( ' ' );
  42. }
  43. var slidifyMarkdown = function(markdown, separator, vertical, notes, attributes) {
  44. separator = separator || '^\n---\n$';
  45. var reSeparator = new RegExp(separator + (vertical ? '|' + vertical : ''), 'mg'),
  46. reHorSeparator = new RegExp(separator),
  47. notesSeparator = new RegExp(notes, 'mg'),
  48. matches,
  49. noteMatch,
  50. lastIndex = 0,
  51. isHorizontal,
  52. wasHorizontal = true,
  53. content,
  54. asideContent,
  55. slide,
  56. sectionStack = [],
  57. markdownSections = '';
  58. // iterate until all blocks between separators are stacked up
  59. while( matches = reSeparator.exec(markdown) ) {
  60. asideContent = null;
  61. // determine direction (horizontal by default)
  62. isHorizontal = reHorSeparator.test(matches[0]);
  63. if( !isHorizontal && wasHorizontal ) {
  64. // create vertical stack
  65. sectionStack.push([]);
  66. }
  67. // pluck slide content from markdown input
  68. content = markdown.substring(lastIndex, matches.index);
  69. noteMatch = content.split(notesSeparator);
  70. if(noteMatch.length === 2) {
  71. content = noteMatch[0];
  72. asideContent = noteMatch[1].trim();
  73. }
  74. slide = {
  75. content: content,
  76. asideContent: asideContent || ""
  77. };
  78. if( isHorizontal && wasHorizontal ) {
  79. // add to horizontal stack
  80. sectionStack.push(slide);
  81. } else {
  82. // add to vertical stack
  83. sectionStack[sectionStack.length-1].push(slide);
  84. }
  85. lastIndex = reSeparator.lastIndex;
  86. wasHorizontal = isHorizontal;
  87. }
  88. // add the remaining slide
  89. (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex));
  90. // flatten the hierarchical stack, and insert <section data-markdown> tags
  91. for( var k = 0, klen = sectionStack.length; k < klen; k++ ) {
  92. // vertical
  93. if(sectionStack[k].propertyIsEnumerable(length) && typeof sectionStack[k].splice === "function") {
  94. markdownSections += '<section '+ attributes +'>' +
  95. '<section data-markdown>' + sectionStack[k].map(twrap).join('</section><section data-markdown>') + '</section>' +
  96. '</section>';
  97. } else {
  98. markdownSections += '<section '+ attributes +' data-markdown>' + twrap( sectionStack[k] ) + '</section>';
  99. }
  100. }
  101. return markdownSections;
  102. };
  103. var querySlidingMarkdown = function() {
  104. var sections = document.querySelectorAll( '[data-markdown]'),
  105. section;
  106. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  107. section = sections[j];
  108. if( section.getAttribute('data-markdown').length ) {
  109. var xhr = new XMLHttpRequest(),
  110. url = section.getAttribute('data-markdown');
  111. xhr.onreadystatechange = function () {
  112. if( xhr.readyState === 4 ) {
  113. if (xhr.status >= 200 && xhr.status < 300) {
  114. section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), section.getAttribute('data-notes'), getForwardedAttributes(section) );
  115. } else {
  116. section.outerHTML = '<section data-state="alert">ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status +
  117. '. Check your browser\'s JavaScript console for more details.' +
  118. '<p>Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.</p></section>';
  119. }
  120. }
  121. };
  122. xhr.open('GET', url, false);
  123. try {
  124. xhr.send();
  125. } catch (e) {
  126. 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);
  127. }
  128. } else if( section.getAttribute('data-separator') ) {
  129. var markdown = stripLeadingWhitespace(section);
  130. section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), section.getAttribute('data-notes'), getForwardedAttributes(section) );
  131. }
  132. }
  133. };
  134. var queryMarkdownSlides = function() {
  135. var sections = document.querySelectorAll( '[data-markdown]');
  136. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  137. makeHtml(sections[j]);
  138. }
  139. };
  140. var makeHtml = function(section) {
  141. var notes = section.querySelector( 'aside.notes' );
  142. var markdown = stripLeadingWhitespace(section);
  143. section.innerHTML = marked(markdown);
  144. if( notes ) {
  145. section.appendChild( notes );
  146. }
  147. };
  148. querySlidingMarkdown();
  149. queryMarkdownSlides();
  150. })();