@@ -127,6 +127,7 @@ const Plugin = () => {
127
127
// with parsing
128
128
content = content . replace ( / < \/ s c r i p t > / g, SCRIPT_END_PLACEHOLDER ) ;
129
129
130
+ // render the template with the content only if there is metadata
130
131
if ( options . metadata ) {
131
132
content = renderTemplate ( content , options )
132
133
}
@@ -153,6 +154,7 @@ const Plugin = () => {
153
154
content ,
154
155
sectionStack = [ ] ;
155
156
157
+ // separates default metadata from the markdown file
156
158
[ markdown , options ] = parseFrontMatter ( markdown , options )
157
159
158
160
// iterate until all blocks between separators are stacked up
@@ -190,22 +192,23 @@ const Plugin = () => {
190
192
191
193
// flatten the hierarchical stack, and insert <section data-markdown> tags
192
194
for ( let i = 0 , len = sectionStack . length ; i < len ; i ++ ) {
193
- let newOptions = { ...options }
195
+ // slideOptions is created to avoid mutating the original options object with default metadata
196
+ let slideOptions = { ...options }
194
197
195
198
// vertical
196
199
if ( sectionStack [ i ] instanceof Array ) {
197
- markdownSections += '<section ' + newOptions . attributes + '>' ;
200
+ markdownSections += '<section ' + slideOptions . attributes + '>' ;
198
201
199
202
sectionStack [ i ] . forEach ( function ( child ) {
200
- [ content , newOptions ] = parseMarkdown ( child , newOptions )
201
- markdownSections += '<section ' + newOptions . attributes + ' data-markdown>' + createMarkdownSlide ( content , newOptions ) + '</section>' ;
203
+ [ content , slideOptions ] = separateInlineMetadataAndMarkdown ( child , slideOptions )
204
+ markdownSections += '<section ' + slideOptions . attributes + ' data-markdown>' + createMarkdownSlide ( content , slideOptions ) + '</section>' ;
202
205
} ) ;
203
206
204
207
markdownSections += '</section>' ;
205
208
}
206
209
else {
207
- [ content , newOptions ] = parseMarkdown ( sectionStack [ i ] , newOptions )
208
- markdownSections += '<section ' + newOptions . attributes + ' data-markdown>' + createMarkdownSlide ( content , newOptions ) + '</section>' ;
210
+ [ content , slideOptions ] = separateInlineMetadataAndMarkdown ( sectionStack [ i ] , slideOptions )
211
+ markdownSections += '<section ' + slideOptions . attributes + ' data-markdown>' + createMarkdownSlide ( content , slideOptions ) + '</section>' ;
209
212
}
210
213
}
211
214
@@ -428,6 +431,12 @@ const Plugin = () => {
428
431
429
432
}
430
433
434
+ /**
435
+ * Parse the front matter from the Markdown document
436
+ *
437
+ * Returns updated options with the default metadata
438
+ * and updated content without the front matter
439
+ */
431
440
function parseFrontMatter ( content , options ) {
432
441
options = getSlidifyOptions ( options )
433
442
@@ -445,7 +454,13 @@ const Plugin = () => {
445
454
return [ content , options ] ;
446
455
}
447
456
448
- function parseMarkdown ( markdown , options ) {
457
+ /**
458
+ * Separates the inline metadata and content for each slide
459
+ *
460
+ * Returns updated options with the inline metadata and
461
+ * updated markdown without the inline metadata for each slide
462
+ */
463
+ function separateInlineMetadataAndMarkdown ( markdown , options ) {
449
464
const yamlRegex = / ` ` ` ( y a m l | y m l ) \n ( [ \s \S ] * ?) ` ` ` ( \n [ \s \S ] * ) ? / g;
450
465
if ( yamlRegex . test ( markdown ) ) {
451
466
yamlRegex . lastIndex = 0 ;
@@ -473,6 +488,11 @@ const Plugin = () => {
473
488
return [ markdown , options ]
474
489
}
475
490
491
+ /**
492
+ * Renders the template for each slide
493
+ *
494
+ * Returns the rendered template with the content
495
+ */
476
496
function renderTemplate ( content , options ) {
477
497
try {
478
498
options = getSlidifyOptions ( options )
0 commit comments