markdown.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. return '<script type="text/template">' + el + '</script>';
  23. };
  24. var getForwardedAttributes = function(section) {
  25. var attributes = section.attributes;
  26. var result = [];
  27. for( var i = 0, len = attributes.length; i < len; i++ ) {
  28. var name = attributes[i].name,
  29. value = attributes[i].value;
  30. // disregard attributes that are used for markdown loading/parsing
  31. if( /data\-(markdown|separator|vertical)/gi.test( name ) ) continue;
  32. if( value ) {
  33. result.push( name + '=' + value );
  34. }
  35. else {
  36. result.push( name );
  37. }
  38. }
  39. return result.join( ' ' );
  40. }
  41. var slidifyMarkdown = function(markdown, separator, vertical, attributes) {
  42. separator = separator || '^\n---\n$';
  43. var reSeparator = new RegExp(separator + (vertical ? '|' + vertical : ''), 'mg'),
  44. reHorSeparator = new RegExp(separator),
  45. matches,
  46. lastIndex = 0,
  47. isHorizontal,
  48. wasHorizontal = true,
  49. content,
  50. sectionStack = [],
  51. markdownSections = '';
  52. // iterate until all blocks between separators are stacked up
  53. while( matches = reSeparator.exec(markdown) ) {
  54. // determine direction (horizontal by default)
  55. isHorizontal = reHorSeparator.test(matches[0]);
  56. if( !isHorizontal && wasHorizontal ) {
  57. // create vertical stack
  58. sectionStack.push([]);
  59. }
  60. // pluck slide content from markdown input
  61. content = markdown.substring(lastIndex, matches.index);
  62. if( isHorizontal && wasHorizontal ) {
  63. // add to horizontal stack
  64. sectionStack.push(content);
  65. } else {
  66. // add to vertical stack
  67. sectionStack[sectionStack.length-1].push(content);
  68. }
  69. lastIndex = reSeparator.lastIndex;
  70. wasHorizontal = isHorizontal;
  71. }
  72. // add the remaining slide
  73. (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex));
  74. // flatten the hierarchical stack, and insert <section data-markdown> tags
  75. for( var k = 0, klen = sectionStack.length; k < klen; k++ ) {
  76. // horizontal
  77. if( typeof sectionStack[k] === 'string' ) {
  78. markdownSections += '<section '+ attributes +' data-markdown>' + twrap( sectionStack[k] ) + '</section>';
  79. }
  80. // vertical
  81. else {
  82. markdownSections += '<section '+ attributes +'>' +
  83. '<section data-markdown>' + sectionStack[k].map(twrap).join('</section><section data-markdown>') + '</section>' +
  84. '</section>';
  85. }
  86. }
  87. return markdownSections;
  88. };
  89. var querySlidingMarkdown = function() {
  90. var sections = document.querySelectorAll( '[data-markdown]'),
  91. section;
  92. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  93. section = sections[j];
  94. if( section.getAttribute('data-markdown').length ) {
  95. var xhr = new XMLHttpRequest(),
  96. url = section.getAttribute('data-markdown');
  97. xhr.onreadystatechange = function () {
  98. if( xhr.readyState === 4 ) {
  99. if (xhr.status >= 200 && xhr.status < 300) {
  100. section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), getForwardedAttributes(section) );
  101. } else {
  102. section.outerHTML = '<section data-state="alert">ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status +
  103. '. Check your browser\'s JavaScript console for more details.' +
  104. '<p>Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.</p></section>';
  105. }
  106. }
  107. };
  108. xhr.open('GET', url, false);
  109. try {
  110. xhr.send();
  111. } catch (e) {
  112. 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);
  113. }
  114. } else if( section.getAttribute('data-separator') ) {
  115. var markdown = stripLeadingWhitespace(section);
  116. section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), getForwardedAttributes(section) );
  117. }
  118. }
  119. };
  120. var queryMarkdownSlides = function() {
  121. var sections = document.querySelectorAll( '[data-markdown]');
  122. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  123. makeHtml(sections[j]);
  124. }
  125. };
  126. var makeHtml = function(section) {
  127. var notes = section.querySelector( 'aside.notes' );
  128. var markdown = stripLeadingWhitespace(section);
  129. section.innerHTML = marked(markdown);
  130. if( notes ) {
  131. section.appendChild( notes );
  132. }
  133. };
  134. querySlidingMarkdown();
  135. queryMarkdownSlides();
  136. })();