Add attributes in markdown for slide generation.
authorVonC <vonc@laposte.net>
Thu, 10 Oct 2013 06:47:32 +0000 (08:47 +0200)
committerVonC <vonc@laposte.net>
Mon, 4 Nov 2013 15:31:15 +0000 (16:31 +0100)
By default, look for <!-- slide-attributes: xxxx -->.
Whatever 'xxx' is will be added to the section attributes.

You can define your own pattern with 'data-attributes'.
For instance 'data-attributes="^\s*?-- (.*?)$"':
that will be added to the options.

The 'attributes' section is removed from the content of the slide,
so the final markdown doesn't reflect them.
That also means you can add those attributes *anywhere* in the slide

But that allows for *any* attribute to be added for a specifc slide,
like:

- id="plan", for allowing internal link (issue #430)
- data-background="#ff0000"
- data-transition="fade"

You list those attributes on a single line, like

- (default):
` <!-- id="plan" data-background="#ff0000" -->`
- or, with an alternative data-attributes pattern:
` -- id="plan" data-background="#ff0000"`

Again, that line is remove from the final content.

plugin/markdown/markdown.js

index d6c6c459f52dd50151a7aadbff7ebc201d1c70d3..b5476aa73eefe085defdc8b273ebd7a4555a5233 100755 (executable)
@@ -27,8 +27,9 @@
        }
 
        var DEFAULT_SLIDE_SEPARATOR = '^\n---\n$',
-               DEFAULT_NOTES_SEPARATOR = 'note:';
-               DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '{_\s*?([^}]+?)}';
+               DEFAULT_NOTES_SEPARATOR = 'note:',
+               DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '{_\s*?([^}]+?)}',
+               DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR = '^.*?<!-- slide-attributes: (.*?)-->';
 
 
        /**
@@ -72,7 +73,7 @@
                                value = attributes[i].value;
 
                        // disregard attributes that are used for markdown loading/parsing
-                       if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue;
+                       if( /data\-(markdown|separator|vertical|notes|attributes)/gi.test( name ) ) continue;
 
                        if( value ) {
                                result.push( name + '=' + value );
@@ -96,6 +97,7 @@
                options.separator = options.separator || DEFAULT_SLIDE_SEPARATOR;
                options.notesSeparator = options.notesSeparator || DEFAULT_NOTES_SEPARATOR;
                options.attributes = options.attributes || '';
+               options.slideAttributesSeparator = options.slideAttributesSeparator || DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR;
 
                return options;
 
                options = getSlidifyOptions( options );
 
                var separatorRegex = new RegExp( options.separator + ( options.verticalSeparator ? '|' + options.verticalSeparator : '' ), 'mg' ),
-                       horizontalSeparatorRegex = new RegExp( options.separator );
+                       horizontalSeparatorRegex = new RegExp( options.separator ),
+                       slideAttributesSeparatorRegex = new RegExp( options.slideAttributesSeparator, 'm');
 
                var matches,
                        lastIndex = 0,
                        isHorizontal,
                        wasHorizontal = true,
                        content,
-                       sectionStack = [];
+                       sectionStack = [],
+                       matchAttributes,
+                       slideAttributes = "";
 
                // iterate until all blocks between separators are stacked up
                while( matches = separatorRegex.exec( markdown ) ) {
                                markdownSections += '<section '+ options.attributes +'>';
 
                                sectionStack[i].forEach( function( child ) {
-                                       markdownSections += '<section data-markdown>' +  createMarkdownSlide( child, options ) + '</section>';
+                                       matchAttributes = slideAttributesSeparatorRegex.exec(child);
+                                       slideAttributes = matchAttributes ? matchAttributes[1] : "";
+                                       child = matchAttributes ? child.replace(slideAttributesSeparatorRegex,"") : child
+                                       markdownSections += '<section ' + slideAttributes + ' data-markdown>' +  createMarkdownSlide( child, options ) + '</section>';
                                } );
 
                                markdownSections += '</section>';
                        }
                        else {
-                               markdownSections += '<section '+ options.attributes +' data-markdown>' + createMarkdownSlide( sectionStack[i], options ) + '</section>';
+                               matchAttributes = slideAttributesSeparatorRegex.exec(sectionStack[i]);
+                               slideAttributes = matchAttributes ? matchAttributes[1] : "";
+                               content = matchAttributes ? sectionStack[i].replace(slideAttributesSeparatorRegex,"") : sectionStack[i]
+                               //console.log('Slide attributes ' + options.slideAttributesSeparator + ' => ' + slideAttributes)
+                               markdownSections += '<section '+ options.attributes + ' ' + slideAttributes +' data-markdown>' + createMarkdownSlide( content, options ) + '</section>';
                        }
                }
-
                return markdownSections;
 
        }
                                                                separator: section.getAttribute( 'data-separator' ),
                                                                verticalSeparator: section.getAttribute( 'data-vertical' ),
                                                                notesSeparator: section.getAttribute( 'data-notes' ),
-                                                               attributes: getForwardedAttributes( section )
+                                                               attributes: getForwardedAttributes( section ),
+                                                               slideAttributesSeparator: section.getAttribute( 'data-attributes' ),
                                                        });
 
                                                }