markdown.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 slidifyMarkdown = function(markdown, separator, vertical) {
  22. separator = separator || '^\n---\n$';
  23. var reSeparator = new RegExp(separator + (vertical ? '|' + vertical : ''), 'mg'),
  24. reHorSeparator = new RegExp(separator),
  25. matches,
  26. lastIndex = 0,
  27. isHorizontal,
  28. wasHorizontal = true,
  29. content,
  30. sectionStack = [],
  31. markdownSections = '';
  32. // iterate until all blocks between separators are stacked up
  33. while( matches = reSeparator.exec(markdown) ) {
  34. // determine direction (horizontal by default)
  35. isHorizontal = reHorSeparator.test(matches[0]);
  36. if( !isHorizontal && wasHorizontal ) {
  37. // create vertical stack
  38. sectionStack.push([]);
  39. }
  40. // pluck slide content from markdown input
  41. content = markdown.substring(lastIndex, matches.index);
  42. if( isHorizontal && wasHorizontal ) {
  43. // add to horizontal stack
  44. sectionStack.push(content);
  45. } else {
  46. // add to vertical stack
  47. sectionStack[sectionStack.length-1].push(content);
  48. }
  49. lastIndex = reSeparator.lastIndex;
  50. wasHorizontal = isHorizontal;
  51. }
  52. // add the remaining slide
  53. (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex));
  54. // flatten the hierarchical stack, and insert <section data-markdown> tags
  55. for( var k = 0, klen = sectionStack.length; k < klen; k++ ) {
  56. markdownSections += typeof sectionStack[k] === 'string'
  57. ? '<section data-markdown>' + sectionStack[k] + '</section>'
  58. : '<section><section data-markdown>' + sectionStack[k].join('</section><section data-markdown>') + '</section></section>';
  59. }
  60. return markdownSections;
  61. };
  62. var querySlidingMarkdown = function() {
  63. var sections = document.querySelectorAll( '[data-markdown]'),
  64. section;
  65. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  66. section = sections[j];
  67. if( section.getAttribute('data-markdown').length ) {
  68. var xhr = new XMLHttpRequest(),
  69. url = section.getAttribute('data-markdown');
  70. xhr.onreadystatechange = function () {
  71. if( xhr.readyState === 4 ) {
  72. section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical') );
  73. }
  74. };
  75. xhr.open('GET', url, false);
  76. xhr.send();
  77. } else if( section.getAttribute('data-separator') ) {
  78. var markdown = stripLeadingWhitespace(section);
  79. section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical') );
  80. }
  81. }
  82. };
  83. var queryMarkdownSlides = function() {
  84. var sections = document.querySelectorAll( '[data-markdown]');
  85. for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
  86. makeHtml(sections[j]);
  87. }
  88. };
  89. var makeHtml = function(section) {
  90. var notes = section.querySelector( 'aside.notes' );
  91. var markdown = stripLeadingWhitespace(section);
  92. section.innerHTML = (new Showdown.converter()).makeHtml(markdown);
  93. if( notes ) {
  94. section.appendChild( notes );
  95. }
  96. };
  97. querySlidingMarkdown();
  98. queryMarkdownSlides();
  99. })();