|
17 | 17 | }); |
18 | 18 | } |
19 | 19 |
|
20 | | - function stripLeadingWhitespace( section ) { |
| 20 | + /** |
| 21 | + * Retrieves the markdown contents of a slide section |
| 22 | + * element. Normalizes leading tabs/whitespace. |
| 23 | + */ |
| 24 | + function getMarkdownFromSlide( section ) { |
21 | 25 |
|
22 | 26 | var template = section.querySelector( 'script' ); |
23 | 27 |
|
|
36 | 40 |
|
37 | 41 | return text; |
38 | 42 |
|
39 | | - }; |
40 | | - |
41 | | - function createSlide( data ) { |
42 | | - |
43 | | - var content = data.content || data; |
44 | | - |
45 | | - if( data.notes ) { |
46 | | - content += '<aside class="notes" data-markdown>' + data.notes + '</aside>'; |
47 | | - } |
48 | | - |
49 | | - return '<script type="text/template">' + content + '</script>'; |
50 | | - |
51 | | - }; |
| 43 | + } |
52 | 44 |
|
| 45 | + /** |
| 46 | + * Given a markdown slide section element, this will |
| 47 | + * return all arguments that aren't related to markdown |
| 48 | + * parsing. Used to forward any other user-defined arguments |
| 49 | + * to the output markdown slide. |
| 50 | + */ |
53 | 51 | function getForwardedAttributes( section ) { |
54 | 52 |
|
55 | 53 | var attributes = section.attributes; |
|
72 | 70 |
|
73 | 71 | return result.join( ' ' ); |
74 | 72 |
|
75 | | - }; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Helper function for constructing a markdown slide. |
| 77 | + */ |
| 78 | + function createMarkdownSlide( data ) { |
| 79 | + |
| 80 | + var content = data.content || data; |
| 81 | + |
| 82 | + if( data.notes ) { |
| 83 | + content += '<aside class="notes" data-markdown>' + data.notes + '</aside>'; |
| 84 | + } |
| 85 | + |
| 86 | + return '<script type="text/template">' + content + '</script>'; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Parses a data string into multiple slides based |
| 92 | + * on the passed in separator arguments. |
| 93 | + */ |
| 94 | + function slidifyMarkdown( markdown, options ) { |
76 | 95 |
|
77 | | - function slidifyMarkdown( markdown, separator, verticalSeparator, noteSeparator, attributes ) { |
| 96 | + options = options || {}; |
| 97 | + options.separator = options.separator || '^\n---\n$'; |
| 98 | + options.notesSeparator = options.notesSeparator || 'note:'; |
| 99 | + options.attributes = options.attributes || ''; |
78 | 100 |
|
79 | | - separator = separator || '^\n---\n$'; |
80 | | - noteSeparator = noteSeparator || 'note:'; |
| 101 | + var separatorRegex = new RegExp( options.separator + ( options.verticalSeparator ? '|' + options.verticalSeparator : '' ), 'mg' ), |
| 102 | + horizontalSeparatorRegex = new RegExp( options.separator ), |
| 103 | + notesSeparatorRegex = new RegExp( options.notesSeparator, 'mgi' ); |
81 | 104 |
|
82 | | - var separatorRegex = new RegExp( separator + ( verticalSeparator ? '|' + verticalSeparator : '' ), 'mg' ), |
83 | | - horizontalSeparatorRegex = new RegExp( separator ), |
84 | | - notesSeparatorRegex = new RegExp( noteSeparator, 'mgi' ), |
85 | | - matches, |
| 105 | + var matches, |
86 | 106 | noteMatch, |
87 | 107 | lastIndex = 0, |
88 | 108 | isHorizontal, |
89 | 109 | wasHorizontal = true, |
90 | 110 | content, |
91 | 111 | notes, |
92 | 112 | slide, |
93 | | - sectionStack = [], |
94 | | - markdownSections = ''; |
| 113 | + sectionStack = []; |
95 | 114 |
|
96 | 115 | // iterate until all blocks between separators are stacked up |
97 | 116 | while( matches = separatorRegex.exec( markdown ) ) { |
|
122 | 141 | if( isHorizontal && wasHorizontal ) { |
123 | 142 | // add to horizontal stack |
124 | 143 | sectionStack.push( slide ); |
125 | | - } else { |
| 144 | + } |
| 145 | + else { |
126 | 146 | // add to vertical stack |
127 | 147 | sectionStack[sectionStack.length-1].push( slide ); |
128 | 148 | } |
|
134 | 154 | // add the remaining slide |
135 | 155 | ( wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1] ).push( markdown.substring( lastIndex ) ); |
136 | 156 |
|
| 157 | + var markdownSections = ''; |
| 158 | + |
137 | 159 | // flatten the hierarchical stack, and insert <section data-markdown> tags |
138 | 160 | for( var k = 0, klen = sectionStack.length; k < klen; k++ ) { |
139 | 161 | // vertical |
140 | 162 | if( sectionStack[k].propertyIsEnumerable( length ) && typeof sectionStack[k].splice === 'function' ) { |
141 | | - markdownSections += '<section '+ attributes +'>' + |
142 | | - '<section data-markdown>' + sectionStack[k].map( createSlide ).join( '</section><section data-markdown>' ) + '</section>' + |
| 163 | + markdownSections += '<section '+ options.attributes +'>' + |
| 164 | + '<section data-markdown>' + sectionStack[k].map( createMarkdownSlide ).join( '</section><section data-markdown>' ) + '</section>' + |
143 | 165 | '</section>'; |
144 | | - } else { |
145 | | - markdownSections += '<section '+ attributes +' data-markdown>' + createSlide( sectionStack[k] ) + '</section>'; |
| 166 | + } |
| 167 | + else { |
| 168 | + markdownSections += '<section '+ options.attributes +' data-markdown>' + createMarkdownSlide( sectionStack[k] ) + '</section>'; |
146 | 169 | } |
147 | 170 | } |
148 | 171 |
|
149 | 172 | return markdownSections; |
150 | | - }; |
151 | 173 |
|
152 | | - function queryExternalMarkdown() { |
| 174 | + } |
| 175 | + |
| 176 | + function loadExternalMarkdown() { |
153 | 177 |
|
154 | 178 | var sections = document.querySelectorAll( '[data-markdown]'), |
155 | 179 | section; |
|
173 | 197 | xhr.onreadystatechange = function() { |
174 | 198 | if( xhr.readyState === 4 ) { |
175 | 199 | if ( xhr.status >= 200 && xhr.status < 300 ) { |
176 | | - section.outerHTML = slidifyMarkdown( xhr.responseText, section.getAttribute( 'data-separator' ), section.getAttribute( 'data-vertical' ), section.getAttribute( 'data-notes' ), getForwardedAttributes( section ) ); |
| 200 | + |
| 201 | + section.outerHTML = slidifyMarkdown( xhr.responseText, { |
| 202 | + separator: section.getAttribute( 'data-separator' ), |
| 203 | + verticalSeparator: section.getAttribute( 'data-vertical' ), |
| 204 | + notesSeparator: section.getAttribute( 'data-notes' ), |
| 205 | + attributes: getForwardedAttributes( section ) |
| 206 | + }); |
| 207 | + |
177 | 208 | } |
178 | 209 | else { |
179 | | - section.outerHTML = '<section data-state="alert">ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status + |
180 | | - '. Check your browser\'s JavaScript console for more details.' + |
181 | | - '<p>Remember that you need to serve the presentation HTML from a HTTP server and the Markdown file must be there too.</p></section>'; |
| 210 | + |
| 211 | + section.outerHTML = '<section data-state="alert">' + |
| 212 | + 'ERROR: The attempt to fetch ' + url + ' failed with HTTP status ' + xhr.status + '.' + |
| 213 | + 'Check your browser\'s JavaScript console for more details.' + |
| 214 | + '<p>Remember that you need to serve the presentation HTML from a HTTP server.</p>' + |
| 215 | + '</section>'; |
| 216 | + |
182 | 217 | } |
183 | 218 | } |
184 | 219 | }; |
|
192 | 227 | 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 ); |
193 | 228 | } |
194 | 229 |
|
195 | | - } else if( section.getAttribute( 'data-separator' ) ) { |
| 230 | + } |
| 231 | + else if( section.getAttribute( 'data-separator' ) ) { |
196 | 232 |
|
197 | | - var markdown = stripLeadingWhitespace( section ); |
198 | | - section.outerHTML = slidifyMarkdown( markdown, section.getAttribute( 'data-separator' ), section.getAttribute( 'data-vertical' ), section.getAttribute( 'data-notes' ), getForwardedAttributes( section ) ); |
| 233 | + section.outerHTML = slidifyMarkdown( getMarkdownFromSlide( section ), { |
| 234 | + separator: section.getAttribute( 'data-separator' ), |
| 235 | + verticalSeparator: section.getAttribute( 'data-vertical' ), |
| 236 | + notesSeparator: section.getAttribute( 'data-notes' ), |
| 237 | + attributes: getForwardedAttributes( section ) |
| 238 | + }); |
199 | 239 |
|
200 | 240 | } |
201 | 241 | } |
202 | 242 |
|
203 | | - }; |
| 243 | + } |
204 | 244 |
|
205 | | - function queryMarkdownSlides() { |
| 245 | + function convertMarkdownToHTML() { |
206 | 246 |
|
207 | 247 | var sections = document.querySelectorAll( '[data-markdown]'); |
208 | 248 |
|
209 | | - for( var j = 0, jlen = sections.length; j < jlen; j++ ) { |
| 249 | + for( var i = 0, len = sections.length; i < len; i++ ) { |
210 | 250 |
|
211 | | - makeHtml( sections[j] ); |
| 251 | + var section = sections[i]; |
212 | 252 |
|
213 | | - } |
| 253 | + var notes = section.querySelector( 'aside.notes' ); |
| 254 | + var markdown = getMarkdownFromSlide( section ); |
214 | 255 |
|
215 | | - }; |
| 256 | + section.innerHTML = marked( markdown ); |
216 | 257 |
|
217 | | - function makeHtml( section ) { |
218 | | - |
219 | | - var notes = section.querySelector( 'aside.notes' ); |
220 | | - |
221 | | - var markdown = stripLeadingWhitespace( section ); |
222 | | - |
223 | | - section.innerHTML = marked( markdown ); |
| 258 | + // If there were notes, we need to re-add them after |
| 259 | + // having overwritten the section's HTML |
| 260 | + if( notes ) { |
| 261 | + section.appendChild( notes ); |
| 262 | + } |
224 | 263 |
|
225 | | - if( notes ) { |
226 | | - section.appendChild( notes ); |
227 | 264 | } |
228 | 265 |
|
229 | | - }; |
| 266 | + } |
230 | 267 |
|
231 | | - queryExternalMarkdown(); |
232 | | - queryMarkdownSlides(); |
| 268 | + loadExternalMarkdown(); |
| 269 | + convertMarkdownToHTML(); |
233 | 270 |
|
234 | 271 | })(); |
0 commit comments