markdown.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // From https://gist.github.com/1343518
  2. // Modified by Hakim to handle Markdown indented with tabs
  3. (function(){
  4. if( typeof Showdown === 'undefined' ) {
  5. throw 'The reveal.js Markdown plugin requires Showdown 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 slidifyMarkdown = function(markdown, separator, vertical) {
  25. separator = separator || '^\n---\n$';
  26. var reSeparator = new RegExp(separator + (vertical ? '|' + vertical : ''), 'mg'),
  27. reHorSeparator = new RegExp(separator),
  28. matches,
  29. lastIndex = 0,
  30. isHorizontal,
  31. wasHorizontal = true,
  32. content,
  33. sectionStack = [],
  34. markdownSections = '';
  35. // iterate until all blocks between separators are stacked up
  36. while( matches = reSeparator.exec(markdown) ) {
  37. // determine direction (horizontal by default)
  38. isHorizontal = reHorSeparator.test(matches[0]);
  39. if( !isHorizontal && wasHorizontal ) {
  40. // create vertical stack
  41. sectionStack.push([]);
  42. }
  43. // pluck slide content from markdown input
  44. content = markdown.substring(lastIndex, matches.index);
  45. if( isHorizontal && wasHorizontal ) {
  46. // add to horizontal stack
  47. sectionStack.push(content);
  48. } else {
  49. // add to vertical stack
  50. sectionStack[sectionStack.length-1].push(content);
  51. }
  52. lastIndex = reSeparator.lastIndex;
  53. wasHorizontal = isHorizontal;
  54. }
  55. // add the remaining slide
  56. (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex));
  57. // flatten the hierarchical stack, and insert <section data-markdown> tags
  58. for( var k = 0, klen = sectionStack.length; k < klen; k++ ) {
  59. markdownSections += typeof sectionStack[k] === 'string'
  60. ? '<section data-markdown>' + twrap( sectionStack[k] ) + '</section>'
  61. : '<section><section data-markdown>' + sectionStack[k].map(twrap).join('</section><section data-markdown>') + '</section></section>';
  62. }
  63. return markdownSections;
  64. };
  65. var querySlidingMarkdown = function() {
  66. var sections = document.querySelectorAll( '[data-markdown]'),
  67. section;
  68. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  69. section = sections[j];
  70. if( section.getAttribute('data-markdown').length ) {
  71. var xhr = new XMLHttpRequest(),
  72. url = section.getAttribute('data-markdown');
  73. xhr.onreadystatechange = function () {
  74. if( xhr.readyState === 4 ) {
  75. if (xhr.status >= 200 && xhr.status < 300) {
  76. section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical') );
  77. } else {
  78. section.outerHTML = '<section data-state="alert">ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status +
  79. '. Check your browser\'s JavaScript console for more details.' +
  80. '<p>Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.</p></section>';
  81. }
  82. }
  83. };
  84. xhr.open('GET', url, false);
  85. try {
  86. xhr.send();
  87. } catch (e) {
  88. 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);
  89. }
  90. } else if( section.getAttribute('data-separator') ) {
  91. var markdown = stripLeadingWhitespace(section);
  92. section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical') );
  93. }
  94. }
  95. };
  96. var queryMarkdownSlides = function() {
  97. var sections = document.querySelectorAll( '[data-markdown]');
  98. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  99. makeHtml(sections[j]);
  100. }
  101. };
  102. var makeHtml = function(section) {
  103. var notes = section.querySelector( 'aside.notes' );
  104. var markdown = stripLeadingWhitespace(section);
  105. section.innerHTML = (new Showdown.converter()).makeHtml(markdown);
  106. if( notes ) {
  107. section.appendChild( notes );
  108. }
  109. };
  110. querySlidingMarkdown();
  111. queryMarkdownSlides();
  112. })();