Skip to content

Commit 7f85c21

Browse files
committed
markdown refactoring; comments, renamed functions, shorter argument lists
1 parent 1c5da37 commit 7f85c21

1 file changed

Lines changed: 93 additions & 56 deletions

File tree

plugin/markdown/markdown.js

Lines changed: 93 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
});
1818
}
1919

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 ) {
2125

2226
var template = section.querySelector( 'script' );
2327

@@ -36,20 +40,14 @@
3640

3741
return text;
3842

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+
}
5244

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+
*/
5351
function getForwardedAttributes( section ) {
5452

5553
var attributes = section.attributes;
@@ -72,26 +70,47 @@
7270

7371
return result.join( ' ' );
7472

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 ) {
7695

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 || '';
78100

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' );
81104

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,
86106
noteMatch,
87107
lastIndex = 0,
88108
isHorizontal,
89109
wasHorizontal = true,
90110
content,
91111
notes,
92112
slide,
93-
sectionStack = [],
94-
markdownSections = '';
113+
sectionStack = [];
95114

96115
// iterate until all blocks between separators are stacked up
97116
while( matches = separatorRegex.exec( markdown ) ) {
@@ -122,7 +141,8 @@
122141
if( isHorizontal && wasHorizontal ) {
123142
// add to horizontal stack
124143
sectionStack.push( slide );
125-
} else {
144+
}
145+
else {
126146
// add to vertical stack
127147
sectionStack[sectionStack.length-1].push( slide );
128148
}
@@ -134,22 +154,26 @@
134154
// add the remaining slide
135155
( wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1] ).push( markdown.substring( lastIndex ) );
136156

157+
var markdownSections = '';
158+
137159
// flatten the hierarchical stack, and insert <section data-markdown> tags
138160
for( var k = 0, klen = sectionStack.length; k < klen; k++ ) {
139161
// vertical
140162
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>' +
143165
'</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>';
146169
}
147170
}
148171

149172
return markdownSections;
150-
};
151173

152-
function queryExternalMarkdown() {
174+
}
175+
176+
function loadExternalMarkdown() {
153177

154178
var sections = document.querySelectorAll( '[data-markdown]'),
155179
section;
@@ -173,12 +197,23 @@
173197
xhr.onreadystatechange = function() {
174198
if( xhr.readyState === 4 ) {
175199
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+
177208
}
178209
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+
182217
}
183218
}
184219
};
@@ -192,43 +227,45 @@
192227
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 );
193228
}
194229

195-
} else if( section.getAttribute( 'data-separator' ) ) {
230+
}
231+
else if( section.getAttribute( 'data-separator' ) ) {
196232

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+
});
199239

200240
}
201241
}
202242

203-
};
243+
}
204244

205-
function queryMarkdownSlides() {
245+
function convertMarkdownToHTML() {
206246

207247
var sections = document.querySelectorAll( '[data-markdown]');
208248

209-
for( var j = 0, jlen = sections.length; j < jlen; j++ ) {
249+
for( var i = 0, len = sections.length; i < len; i++ ) {
210250

211-
makeHtml( sections[j] );
251+
var section = sections[i];
212252

213-
}
253+
var notes = section.querySelector( 'aside.notes' );
254+
var markdown = getMarkdownFromSlide( section );
214255

215-
};
256+
section.innerHTML = marked( markdown );
216257

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+
}
224263

225-
if( notes ) {
226-
section.appendChild( notes );
227264
}
228265

229-
};
266+
}
230267

231-
queryExternalMarkdown();
232-
queryMarkdownSlides();
268+
loadExternalMarkdown();
269+
convertMarkdownToHTML();
233270

234271
})();

0 commit comments

Comments
 (0)