From 245efee894311bc2cafecf9f7fa61547013ac975 Mon Sep 17 00:00:00 2001 From: Adam Hepton Date: Tue, 25 Jun 2013 12:15:22 +0100 Subject: Support for notes within markdown --- plugin/markdown/markdown.js | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 473666b..8366c50 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -28,7 +28,9 @@ }; var twrap = function(el) { - return ''; + var cnt = el.content || el; + cnt += el.asideContent ? ('') : ''; + return ''; }; var getForwardedAttributes = function(section) { @@ -40,7 +42,7 @@ value = attributes[i].value; // disregard attributes that are used for markdown loading/parsing - if( /data\-(markdown|separator|vertical)/gi.test( name ) ) continue; + if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue; if( value ) { result.push( name + '=' + value ); @@ -53,22 +55,27 @@ return result.join( ' ' ); } - var slidifyMarkdown = function(markdown, separator, vertical, attributes) { + var slidifyMarkdown = function(markdown, separator, vertical, notes, attributes) { separator = separator || '^\n---\n$'; var reSeparator = new RegExp(separator + (vertical ? '|' + vertical : ''), 'mg'), reHorSeparator = new RegExp(separator), + notesSeparator = new RegExp(notes, 'mg'), matches, + noteMatch, lastIndex = 0, isHorizontal, wasHorizontal = true, content, + asideContent, + slide, sectionStack = [], markdownSections = ''; // iterate until all blocks between separators are stacked up while( matches = reSeparator.exec(markdown) ) { + asideContent = null; // determine direction (horizontal by default) isHorizontal = reHorSeparator.test(matches[0]); @@ -80,18 +87,28 @@ // pluck slide content from markdown input content = markdown.substring(lastIndex, matches.index); + noteMatch = content.split(notesSeparator); + + if(noteMatch.length === 2) { + content = noteMatch[0]; + asideContent = noteMatch[1].trim(); + } + + slide = { + content: content, + asideContent: asideContent || "" + }; if( isHorizontal && wasHorizontal ) { // add to horizontal stack - sectionStack.push(content); + sectionStack.push(slide); } else { // add to vertical stack - sectionStack[sectionStack.length-1].push(content); + sectionStack[sectionStack.length-1].push(slide); } lastIndex = reSeparator.lastIndex; wasHorizontal = isHorizontal; - } // add the remaining slide @@ -99,15 +116,13 @@ // flatten the hierarchical stack, and insert
tags for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { - // horizontal - if( typeof sectionStack[k] === 'string' ) { - markdownSections += '
' + twrap( sectionStack[k] ) + '
'; - } // vertical - else { + if(sectionStack[k].propertyIsEnumerable(length) && typeof sectionStack[k].splice === "function") { markdownSections += '
' + '
' + sectionStack[k].map(twrap).join('
') + '
' + '
'; + } else { + markdownSections += '
' + twrap( sectionStack[k] ) + '
'; } } @@ -131,7 +146,7 @@ xhr.onreadystatechange = function () { if( xhr.readyState === 4 ) { if (xhr.status >= 200 && xhr.status < 300) { - section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), getForwardedAttributes(section) ); + section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), section.getAttribute('data-notes'), getForwardedAttributes(section) ); } else { section.outerHTML = '
ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status + '. Check your browser\'s JavaScript console for more details.' + @@ -150,7 +165,7 @@ } else if( section.getAttribute('data-separator') ) { var markdown = stripLeadingWhitespace(section); - section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), getForwardedAttributes(section) ); + section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), section.getAttribute('data-notes'), getForwardedAttributes(section) ); } } -- cgit v1.2.3 From ce0facf7b24bd43e8ca6e77f69fd2c651b840341 Mon Sep 17 00:00:00 2001 From: Emile 'iMil' Heitor Date: Tue, 23 Jul 2013 17:31:30 +0200 Subject: added custom charset support for external markdown file --- plugin/markdown/markdown.js | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 473666b..9681cf1 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -128,6 +128,12 @@ var xhr = new XMLHttpRequest(), url = section.getAttribute('data-markdown'); + datacharset = section.getAttribute('data-charset'); + // see https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute#Notes + if (datacharset != null && datacharset != '') { + xhr.overrideMimeType('text/html; charset=' + datacharset); + } + xhr.onreadystatechange = function () { if( xhr.readyState === 4 ) { if (xhr.status >= 200 && xhr.status < 300) { -- cgit v1.2.3 From efbcab57f74891a060320af1c9e2e3c039febc5a Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Wed, 31 Jul 2013 06:48:09 +0800 Subject: * [bug] fixed render markdown file error like this: ```html
``` --- plugin/markdown/markdown.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 473666b..dcab2fa 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -6,6 +6,14 @@ throw 'The reveal.js Markdown plugin requires marked to be loaded'; } + if (typeof hljs !== 'undefined') { + marked.setOptions({ + highlight: function (lang, code) { + return hljs.highlightAuto(lang, code).value; + } + }); + } + var stripLeadingWhitespace = function(section) { var template = section.querySelector( 'script' ); @@ -28,7 +36,7 @@ }; var twrap = function(el) { - return ''; + return marked(el); }; var getForwardedAttributes = function(section) { @@ -51,7 +59,7 @@ } return result.join( ' ' ); - } + }; var slidifyMarkdown = function(markdown, separator, vertical, attributes) { @@ -101,12 +109,12 @@ for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { // horizontal if( typeof sectionStack[k] === 'string' ) { - markdownSections += '
' + twrap( sectionStack[k] ) + '
'; + markdownSections += '
' + twrap( sectionStack[k] ) + '
'; } // vertical else { markdownSections += '
' + - '
' + sectionStack[k].map(twrap).join('
') + '
' + + '
' + sectionStack[k].map(twrap).join('
') + '
' + '
'; } } -- cgit v1.2.3 From b968dfef1534afb26c42d652234f32aafaeb65ff Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Fri, 23 Aug 2013 17:49:19 -0400 Subject: update markdown plugin formatting to match reveal.js core --- plugin/markdown/markdown.js | 373 +++++++++++++++++++++++--------------------- 1 file changed, 192 insertions(+), 181 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index bc771e7..68b534b 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -2,219 +2,230 @@ // Modified by Hakim to handle Markdown indented with tabs (function(){ - if( typeof marked === 'undefined' ) { - throw 'The reveal.js Markdown plugin requires marked to be loaded'; - } - - if (typeof hljs !== 'undefined') { - marked.setOptions({ - highlight: function (lang, code) { - return hljs.highlightAuto(lang, code).value; - } - }); - } - - var stripLeadingWhitespace = function(section) { - - var template = section.querySelector( 'script' ); - - // strip leading whitespace so it isn't evaluated as code - var text = ( template || section ).textContent; - - var leadingWs = text.match(/^\n?(\s*)/)[1].length, - leadingTabs = text.match(/^\n?(\t*)/)[1].length; - - if( leadingTabs > 0 ) { - text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' ); - } - else if( leadingWs > 1 ) { - text = text.replace( new RegExp('\\n? {' + leadingWs + '}','g'), '\n' ); - } - - return text; - - }; - - var twrap = function(el) { - var content = el.content || el; - content += el.asideContent ? ('') : ''; - return ''; - }; - - var getForwardedAttributes = function(section) { - var attributes = section.attributes; - var result = []; - - for( var i = 0, len = attributes.length; i < len; i++ ) { - var name = attributes[i].name, - value = attributes[i].value; - - // disregard attributes that are used for markdown loading/parsing - if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue; - - if( value ) { - result.push( name + '=' + value ); - } - else { - result.push( name ); - } - } - - return result.join( ' ' ); - }; - - var slidifyMarkdown = function(markdown, separator, vertical, notes, attributes) { - - separator = separator || '^\n---\n$'; - notes = notes || 'note:'; - - var separatorRegex = new RegExp( separator + ( vertical ? '|' + vertical : '' ), 'mg' ), - horizontalSeparatorRegex = new RegExp( separator ), - notesSeparatorRegex = new RegExp( notes, 'mgi' ), - matches, - noteMatch, - lastIndex = 0, - isHorizontal, - wasHorizontal = true, - content, - asideContent, - slide, - sectionStack = [], - markdownSections = ''; - - // iterate until all blocks between separators are stacked up - while( matches = separatorRegex.exec( markdown ) ) { - asideContent = null; - - // determine direction (horizontal by default) - isHorizontal = horizontalSeparatorRegex.test( matches[0] ); - - if( !isHorizontal && wasHorizontal ) { - // create vertical stack - sectionStack.push( [] ); - } - - // pluck slide content from markdown input - content = markdown.substring( lastIndex, matches.index ); - noteMatch = content.split( notesSeparatorRegex ); - - if( noteMatch.length === 2 ) { - content = noteMatch[0]; - asideContent = noteMatch[1].trim(); - } + if( typeof marked === 'undefined' ) { + throw 'The reveal.js Markdown plugin requires marked to be loaded'; + } - slide = { - content: content, - asideContent: asideContent || "" - }; + if( typeof hljs !== 'undefined' ) { + marked.setOptions({ + highlight: function( lang, code ) { + return hljs.highlightAuto( lang, code ).value; + } + }); + } - if( isHorizontal && wasHorizontal ) { - // add to horizontal stack - sectionStack.push(slide); - } else { - // add to vertical stack - sectionStack[sectionStack.length-1].push(slide); - } + var stripLeadingWhitespace = function( section ) { - lastIndex = separatorRegex.lastIndex; - wasHorizontal = isHorizontal; - } + var template = section.querySelector( 'script' ); - // add the remaining slide - (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex)); + // strip leading whitespace so it isn't evaluated as code + var text = ( template || section ).textContent; - // flatten the hierarchical stack, and insert
tags - for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { - // vertical - if( sectionStack[k].propertyIsEnumerable(length) && typeof sectionStack[k].splice === 'function' ) { - markdownSections += '
' + - '
' + sectionStack[k].map(twrap).join('
') + '
' + - '
'; - } else { - markdownSections += '
' + twrap( sectionStack[k] ) + '
'; - } - } + var leadingWs = text.match( /^\n?(\s*)/ )[1].length, + leadingTabs = text.match( /^\n?(\t*)/ )[1].length; - return markdownSections; - }; + if( leadingTabs > 0 ) { + text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' ); + } + else if( leadingWs > 1 ) { + text = text.replace( new RegExp('\\n? {' + leadingWs + '}','g'), '\n' ); + } - var querySlidingMarkdown = function() { + return text; - var sections = document.querySelectorAll( '[data-markdown]'), - section; + }; - for( var j = 0, jlen = sections.length; j < jlen; j++ ) { + var twrap = function( el ) { - section = sections[j]; + var content = el.content || el; + + if( el.asideContent ) { + content += ''; + } + + return ''; + + }; + + var getForwardedAttributes = function( section ) { - if( section.getAttribute('data-markdown').length ) { + var attributes = section.attributes; + var result = []; - var xhr = new XMLHttpRequest(), - url = section.getAttribute('data-markdown'); + for( var i = 0, len = attributes.length; i < len; i++ ) { + var name = attributes[i].name, + value = attributes[i].value; - datacharset = section.getAttribute('data-charset'); - // see https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute#Notes - if (datacharset != null && datacharset != '') { - xhr.overrideMimeType('text/html; charset=' + datacharset); - } + // disregard attributes that are used for markdown loading/parsing + if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue; - xhr.onreadystatechange = function () { - if( xhr.readyState === 4 ) { - if (xhr.status >= 200 && xhr.status < 300) { - section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), section.getAttribute('data-notes'), getForwardedAttributes(section) ); - } else { - section.outerHTML = '
ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status + - '. Check your browser\'s JavaScript console for more details.' + - '

Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.

'; - } - } - }; + if( value ) { + result.push( name + '=' + value ); + } + else { + result.push( name ); + } + } - xhr.open('GET', url, false); - try { - xhr.send(); - } catch (e) { - 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); - } + return result.join( ' ' ); - } else if( section.getAttribute('data-separator') ) { + }; - var markdown = stripLeadingWhitespace(section); - section.outerHTML = slidifyMarkdown( markdown, section.getAttribute('data-separator'), section.getAttribute('data-vertical'), section.getAttribute('data-notes'), getForwardedAttributes(section) ); + var slidifyMarkdown = function( markdown, separator, vertical, notes, attributes ) { - } - } + separator = separator || '^\n---\n$'; + notes = notes || 'note:'; - }; + var separatorRegex = new RegExp( separator + ( vertical ? '|' + vertical : '' ), 'mg' ), + horizontalSeparatorRegex = new RegExp( separator ), + notesSeparatorRegex = new RegExp( notes, 'mgi' ), + matches, + noteMatch, + lastIndex = 0, + isHorizontal, + wasHorizontal = true, + content, + asideContent, + slide, + sectionStack = [], + markdownSections = ''; + + // iterate until all blocks between separators are stacked up + while( matches = separatorRegex.exec( markdown ) ) { + asideContent = null; - var queryMarkdownSlides = function() { + // determine direction (horizontal by default) + isHorizontal = horizontalSeparatorRegex.test( matches[0] ); - var sections = document.querySelectorAll( '[data-markdown]'); + if( !isHorizontal && wasHorizontal ) { + // create vertical stack + sectionStack.push( [] ); + } - for( var j = 0, jlen = sections.length; j < jlen; j++ ) { + // pluck slide content from markdown input + content = markdown.substring( lastIndex, matches.index ); + noteMatch = content.split( notesSeparatorRegex ); - makeHtml(sections[j]); + if( noteMatch.length === 2 ) { + content = noteMatch[0]; + asideContent = noteMatch[1].trim(); + } - } + slide = { + content: content, + asideContent: asideContent || '' + }; - }; + if( isHorizontal && wasHorizontal ) { + // add to horizontal stack + sectionStack.push(slide); + } else { + // add to vertical stack + sectionStack[sectionStack.length-1].push(slide); + } - var makeHtml = function(section) { + lastIndex = separatorRegex.lastIndex; + wasHorizontal = isHorizontal; + } - var notes = section.querySelector( 'aside.notes' ); + // add the remaining slide + (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex)); - var markdown = stripLeadingWhitespace(section); + // flatten the hierarchical stack, and insert
tags + for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { + // vertical + if( sectionStack[k].propertyIsEnumerable( length ) && typeof sectionStack[k].splice === 'function' ) { + markdownSections += '
' + + '
' + sectionStack[k].map( twrap ).join( '
' ) + '
' + + '
'; + } else { + markdownSections += '
' + twrap( sectionStack[k] ) + '
'; + } + } - section.innerHTML = marked(markdown); + return markdownSections; + }; - if( notes ) { - section.appendChild( notes ); - } + var queryExternalMarkdown = function() { - }; + var sections = document.querySelectorAll( '[data-markdown]'), + section; - querySlidingMarkdown(); + for( var j = 0, jlen = sections.length; j < jlen; j++ ) { - queryMarkdownSlides(); + section = sections[j]; + + if( section.getAttribute( 'data-markdown' ).length ) { + + var xhr = new XMLHttpRequest(), + url = section.getAttribute( 'data-markdown' ); + + datacharset = section.getAttribute( 'data-charset' ); + + // see https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute#Notes + if( datacharset != null && datacharset != '' ) { + xhr.overrideMimeType( 'text/html; charset=' + datacharset ); + } + + xhr.onreadystatechange = function() { + if( xhr.readyState === 4 ) { + if ( xhr.status >= 200 && xhr.status < 300 ) { + section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute( 'data-separator' ), section.getAttribute( 'data-vertical' ), section.getAttribute( 'data-notes' ), getForwardedAttributes( section ) ); + } + else { + section.outerHTML = '
ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status + + '. Check your browser\'s JavaScript console for more details.' + + '

Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.

'; + } + } + }; + + xhr.open( 'GET', url, false ); + + try { + xhr.send(); + } + catch ( e ) { + 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 ); + } + + } else if( section.getAttribute( 'data-separator' ) ) { + + var markdown = stripLeadingWhitespace( section ); + section.outerHTML = slidifyMarkdown( markdown, section.getAttribute( 'data-separator' ), section.getAttribute( 'data-vertical' ), section.getAttribute( 'data-notes' ), getForwardedAttributes( section ) ); + + } + } + + }; + + var queryMarkdownSlides = function() { + + var sections = document.querySelectorAll( '[data-markdown]'); + + for( var j = 0, jlen = sections.length; j < jlen; j++ ) { + + makeHtml( sections[j] ); + + } + + }; + + var makeHtml = function( section ) { + + var notes = section.querySelector( 'aside.notes' ); + + var markdown = stripLeadingWhitespace( section ); + + section.innerHTML = marked( markdown ); + + if( notes ) { + section.appendChild( notes ); + } + + }; + + queryExternalMarkdown(); + queryMarkdownSlides(); })(); -- cgit v1.2.3 From 4350c0024fed73032c9eb77e3f8af277d30047de Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Fri, 23 Aug 2013 17:51:20 -0400 Subject: standardize way of declaring functions --- plugin/markdown/markdown.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 68b534b..e3fe0f8 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -14,7 +14,7 @@ }); } - var stripLeadingWhitespace = function( section ) { + function stripLeadingWhitespace( section ) { var template = section.querySelector( 'script' ); @@ -35,7 +35,7 @@ }; - var twrap = function( el ) { + function twrap( el ) { var content = el.content || el; @@ -47,7 +47,7 @@ }; - var getForwardedAttributes = function( section ) { + function getForwardedAttributes( section ) { var attributes = section.attributes; var result = []; @@ -71,7 +71,7 @@ }; - var slidifyMarkdown = function( markdown, separator, vertical, notes, attributes ) { + function slidifyMarkdown( markdown, separator, vertical, notes, attributes ) { separator = separator || '^\n---\n$'; notes = notes || 'note:'; @@ -146,7 +146,7 @@ return markdownSections; }; - var queryExternalMarkdown = function() { + function queryExternalMarkdown() { var sections = document.querySelectorAll( '[data-markdown]'), section; @@ -199,7 +199,7 @@ }; - var queryMarkdownSlides = function() { + function queryMarkdownSlides() { var sections = document.querySelectorAll( '[data-markdown]'); @@ -211,7 +211,7 @@ }; - var makeHtml = function( section ) { + function makeHtml( section ) { var notes = section.querySelector( 'aside.notes' ); -- cgit v1.2.3 From 1c5da37ca633699e82e4f1b94506adb26b32250d Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Sat, 24 Aug 2013 10:03:34 -0400 Subject: more markdown plugin refactoring --- plugin/markdown/markdown.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index e3fe0f8..a5852c0 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -1,5 +1,8 @@ -// From https://gist.github.com/1343518 -// Modified by Hakim to handle Markdown indented with tabs +/** + * The reveal.js markdown plugin. Handles parsing of + * markdown inside of presentations as well as loading + * of external markdown documents. + */ (function(){ if( typeof marked === 'undefined' ) { @@ -35,12 +38,12 @@ }; - function twrap( el ) { + function createSlide( data ) { - var content = el.content || el; + var content = data.content || data; - if( el.asideContent ) { - content += ''; + if( data.notes ) { + content += ''; } return ''; @@ -71,28 +74,28 @@ }; - function slidifyMarkdown( markdown, separator, vertical, notes, attributes ) { + function slidifyMarkdown( markdown, separator, verticalSeparator, noteSeparator, attributes ) { separator = separator || '^\n---\n$'; - notes = notes || 'note:'; + noteSeparator = noteSeparator || 'note:'; - var separatorRegex = new RegExp( separator + ( vertical ? '|' + vertical : '' ), 'mg' ), + var separatorRegex = new RegExp( separator + ( verticalSeparator ? '|' + verticalSeparator : '' ), 'mg' ), horizontalSeparatorRegex = new RegExp( separator ), - notesSeparatorRegex = new RegExp( notes, 'mgi' ), + notesSeparatorRegex = new RegExp( noteSeparator, 'mgi' ), matches, noteMatch, lastIndex = 0, isHorizontal, wasHorizontal = true, content, - asideContent, + notes, slide, sectionStack = [], markdownSections = ''; // iterate until all blocks between separators are stacked up while( matches = separatorRegex.exec( markdown ) ) { - asideContent = null; + notes = null; // determine direction (horizontal by default) isHorizontal = horizontalSeparatorRegex.test( matches[0] ); @@ -108,20 +111,20 @@ if( noteMatch.length === 2 ) { content = noteMatch[0]; - asideContent = noteMatch[1].trim(); + notes = noteMatch[1].trim(); } slide = { content: content, - asideContent: asideContent || '' + notes: notes || '' }; if( isHorizontal && wasHorizontal ) { // add to horizontal stack - sectionStack.push(slide); + sectionStack.push( slide ); } else { // add to vertical stack - sectionStack[sectionStack.length-1].push(slide); + sectionStack[sectionStack.length-1].push( slide ); } lastIndex = separatorRegex.lastIndex; @@ -129,17 +132,17 @@ } // add the remaining slide - (wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1]).push(markdown.substring(lastIndex)); + ( wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1] ).push( markdown.substring( lastIndex ) ); // flatten the hierarchical stack, and insert
tags for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { // vertical if( sectionStack[k].propertyIsEnumerable( length ) && typeof sectionStack[k].splice === 'function' ) { markdownSections += '
' + - '
' + sectionStack[k].map( twrap ).join( '
' ) + '
' + + '
' + sectionStack[k].map( createSlide ).join( '
' ) + '
' + '
'; } else { - markdownSections += '
' + twrap( sectionStack[k] ) + '
'; + markdownSections += '
' + createSlide( sectionStack[k] ) + '
'; } } -- cgit v1.2.3 From 7f85c2138603d5c869c3c9a3713bfa0b1e7a08f8 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Sat, 24 Aug 2013 10:39:15 -0400 Subject: markdown refactoring; comments, renamed functions, shorter argument lists --- plugin/markdown/markdown.js | 149 +++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 56 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index a5852c0..552b32c 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -17,7 +17,11 @@ }); } - function stripLeadingWhitespace( section ) { + /** + * Retrieves the markdown contents of a slide section + * element. Normalizes leading tabs/whitespace. + */ + function getMarkdownFromSlide( section ) { var template = section.querySelector( 'script' ); @@ -36,20 +40,14 @@ return text; - }; - - function createSlide( data ) { - - var content = data.content || data; - - if( data.notes ) { - content += ''; - } - - return ''; - - }; + } + /** + * Given a markdown slide section element, this will + * return all arguments that aren't related to markdown + * parsing. Used to forward any other user-defined arguments + * to the output markdown slide. + */ function getForwardedAttributes( section ) { var attributes = section.attributes; @@ -72,17 +70,39 @@ return result.join( ' ' ); - }; + } + + /** + * Helper function for constructing a markdown slide. + */ + function createMarkdownSlide( data ) { + + var content = data.content || data; + + if( data.notes ) { + content += ''; + } + + return ''; + + } + + /** + * Parses a data string into multiple slides based + * on the passed in separator arguments. + */ + function slidifyMarkdown( markdown, options ) { - function slidifyMarkdown( markdown, separator, verticalSeparator, noteSeparator, attributes ) { + options = options || {}; + options.separator = options.separator || '^\n---\n$'; + options.notesSeparator = options.notesSeparator || 'note:'; + options.attributes = options.attributes || ''; - separator = separator || '^\n---\n$'; - noteSeparator = noteSeparator || 'note:'; + var separatorRegex = new RegExp( options.separator + ( options.verticalSeparator ? '|' + options.verticalSeparator : '' ), 'mg' ), + horizontalSeparatorRegex = new RegExp( options.separator ), + notesSeparatorRegex = new RegExp( options.notesSeparator, 'mgi' ); - var separatorRegex = new RegExp( separator + ( verticalSeparator ? '|' + verticalSeparator : '' ), 'mg' ), - horizontalSeparatorRegex = new RegExp( separator ), - notesSeparatorRegex = new RegExp( noteSeparator, 'mgi' ), - matches, + var matches, noteMatch, lastIndex = 0, isHorizontal, @@ -90,8 +110,7 @@ content, notes, slide, - sectionStack = [], - markdownSections = ''; + sectionStack = []; // iterate until all blocks between separators are stacked up while( matches = separatorRegex.exec( markdown ) ) { @@ -122,7 +141,8 @@ if( isHorizontal && wasHorizontal ) { // add to horizontal stack sectionStack.push( slide ); - } else { + } + else { // add to vertical stack sectionStack[sectionStack.length-1].push( slide ); } @@ -134,22 +154,26 @@ // add the remaining slide ( wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1] ).push( markdown.substring( lastIndex ) ); + var markdownSections = ''; + // flatten the hierarchical stack, and insert
tags for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { // vertical if( sectionStack[k].propertyIsEnumerable( length ) && typeof sectionStack[k].splice === 'function' ) { - markdownSections += '
' + - '
' + sectionStack[k].map( createSlide ).join( '
' ) + '
' + + markdownSections += '
' + + '
' + sectionStack[k].map( createMarkdownSlide ).join( '
' ) + '
' + '
'; - } else { - markdownSections += '
' + createSlide( sectionStack[k] ) + '
'; + } + else { + markdownSections += '
' + createMarkdownSlide( sectionStack[k] ) + '
'; } } return markdownSections; - }; - function queryExternalMarkdown() { + } + + function loadExternalMarkdown() { var sections = document.querySelectorAll( '[data-markdown]'), section; @@ -173,12 +197,23 @@ xhr.onreadystatechange = function() { if( xhr.readyState === 4 ) { if ( xhr.status >= 200 && xhr.status < 300 ) { - section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute( 'data-separator' ), section.getAttribute( 'data-vertical' ), section.getAttribute( 'data-notes' ), getForwardedAttributes( section ) ); + + section.outerHTML = slidifyMarkdown( xhr.responseText, { + separator: section.getAttribute( 'data-separator' ), + verticalSeparator: section.getAttribute( 'data-vertical' ), + notesSeparator: section.getAttribute( 'data-notes' ), + attributes: getForwardedAttributes( section ) + }); + } else { - section.outerHTML = '
ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status + - '. Check your browser\'s JavaScript console for more details.' + - '

Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.

'; + + section.outerHTML = '
' + + 'ERROR: The attempt to fetch ' + url + ' failed with HTTP status ' + xhr.status + '.' + + 'Check your browser\'s JavaScript console for more details.' + + '

Remember that you need to serve the presentation HTML from a HTTP server.

' + + '
'; + } } }; @@ -192,43 +227,45 @@ 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 ); } - } else if( section.getAttribute( 'data-separator' ) ) { + } + else if( section.getAttribute( 'data-separator' ) ) { - var markdown = stripLeadingWhitespace( section ); - section.outerHTML = slidifyMarkdown( markdown, section.getAttribute( 'data-separator' ), section.getAttribute( 'data-vertical' ), section.getAttribute( 'data-notes' ), getForwardedAttributes( section ) ); + section.outerHTML = slidifyMarkdown( getMarkdownFromSlide( section ), { + separator: section.getAttribute( 'data-separator' ), + verticalSeparator: section.getAttribute( 'data-vertical' ), + notesSeparator: section.getAttribute( 'data-notes' ), + attributes: getForwardedAttributes( section ) + }); } } - }; + } - function queryMarkdownSlides() { + function convertMarkdownToHTML() { var sections = document.querySelectorAll( '[data-markdown]'); - for( var j = 0, jlen = sections.length; j < jlen; j++ ) { + for( var i = 0, len = sections.length; i < len; i++ ) { - makeHtml( sections[j] ); + var section = sections[i]; - } + var notes = section.querySelector( 'aside.notes' ); + var markdown = getMarkdownFromSlide( section ); - }; + section.innerHTML = marked( markdown ); - function makeHtml( section ) { - - var notes = section.querySelector( 'aside.notes' ); - - var markdown = stripLeadingWhitespace( section ); - - section.innerHTML = marked( markdown ); + // If there were notes, we need to re-add them after + // having overwritten the section's HTML + if( notes ) { + section.appendChild( notes ); + } - if( notes ) { - section.appendChild( notes ); } - }; + } - queryExternalMarkdown(); - queryMarkdownSlides(); + loadExternalMarkdown(); + convertMarkdownToHTML(); })(); -- cgit v1.2.3 From 7e629936e576ea3205d391070d10bbc26ed8e120 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Sat, 24 Aug 2013 11:06:52 -0400 Subject: always use 'i' as iterator --- plugin/markdown/markdown.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'plugin/markdown/markdown.js') diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 552b32c..31d22f1 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -157,15 +157,15 @@ var markdownSections = ''; // flatten the hierarchical stack, and insert
tags - for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { + for( var i = 0, len = sectionStack.length; i < len; i++ ) { // vertical - if( sectionStack[k].propertyIsEnumerable( length ) && typeof sectionStack[k].splice === 'function' ) { + if( sectionStack[i].propertyIsEnumerable( length ) && typeof sectionStack[i].splice === 'function' ) { markdownSections += '
' + - '
' + sectionStack[k].map( createMarkdownSlide ).join( '
' ) + '
' + + '
' + sectionStack[i].map( createMarkdownSlide ).join( '
' ) + '
' + '
'; } else { - markdownSections += '
' + createMarkdownSlide( sectionStack[k] ) + '
'; + markdownSections += '
' + createMarkdownSlide( sectionStack[i] ) + '
'; } } @@ -178,9 +178,9 @@ var sections = document.querySelectorAll( '[data-markdown]'), section; - for( var j = 0, jlen = sections.length; j < jlen; j++ ) { + for( var i = 0, len = sections.length; i < len; i++ ) { - section = sections[j]; + section = sections[i]; if( section.getAttribute( 'data-markdown' ).length ) { -- cgit v1.2.3