markdown.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical') );
  76. }
  77. };
  78. xhr.open('GET', url, false);
  79. xhr.send();
  80. } else if( section.getAttribute('data-separator') ) {
  81. var markdown = stripLeadingWhitespace(section);
  82. section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical') );
  83. }
  84. }
  85. };
  86. var queryMarkdownSlides = function() {
  87. var sections = document.querySelectorAll( '[data-markdown]');
  88. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  89. makeHtml(sections[j]);
  90. }
  91. };
  92. var makeHtml = function(section) {
  93. var notes = section.querySelector( 'aside.notes' );
  94. var markdown = stripLeadingWhitespace(section);
  95. section.innerHTML = (new Showdown.converter()).makeHtml(markdown);
  96. if( notes ) {
  97. section.appendChild( notes );
  98. }
  99. };
  100. querySlidingMarkdown();
  101. queryMarkdownSlides();
  102. })();