Add extras plugin
To make loading of extras/ addons simpler, and remove cruft from presentation html files, I created a new plugin src/plugins/extras/. If any of the extra addons (highlight.js, markdown.js, mathjax.js or mermaid.js) are added to the html file with a regular <script> tag, then this module will discover and know how to init the module for you, so it is not necessary to do that in html. If you're not using the extras, this plugin does nothing. Note that in the branch history where you are reading this commit, the extras/ directory doesn't actually exist yet, nor do the examples/ that use them. But they will hopefully join this branch soon.
This commit is contained in:
26
src/plugins/extras/README.md
Normal file
26
src/plugins/extras/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
Extras Plugin
|
||||
=============
|
||||
|
||||
The Extras plugin will initialize the optional addon plugins from
|
||||
[extras/](../../../extras/) directory, if they were loaded.
|
||||
|
||||
Generally, for an extras plugin to have been loaded, 2 things must have happened:
|
||||
|
||||
1. The extras plugins must be present in extras/ directory, for example after
|
||||
running `git submodule update`
|
||||
2. One or more extras plugins are added to the impress.js presentation (the HTML
|
||||
file) by the author using a regular `<script>` tag.
|
||||
|
||||
If one or more extras plugins were so added, this plugin will automatically
|
||||
discover them and perform initialization (such as calling
|
||||
`mermaid.initialize()`).
|
||||
|
||||
If no extras plugins are added to a presentation, this plugin does nothing.
|
||||
|
||||
Note that some extra plugins (like mathjax) initialize themselves immediately, and
|
||||
there's nothing to do here.
|
||||
|
||||
Author
|
||||
------
|
||||
|
||||
Henrik Ingo (@henrikingo), 2016
|
||||
69
src/plugins/extras/extras.js
Normal file
69
src/plugins/extras/extras.js
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Extras Plugin
|
||||
*
|
||||
* This plugin performs initialization (like calling mermaid.initialize())
|
||||
* for the extras/ plugins if they are loaded into a presentation.
|
||||
*
|
||||
* See README.md for details.
|
||||
*
|
||||
* Copyright 2016 Henrik Ingo (@henrikingo)
|
||||
* Released under the MIT license.
|
||||
*/
|
||||
/* global markdown, hljs, mermaid, impress, document, window */
|
||||
|
||||
( function( document, window ) {
|
||||
"use strict";
|
||||
|
||||
var preInit = function() {
|
||||
if ( window.markdown ) {
|
||||
|
||||
// Unlike the other extras, Markdown.js doesn't by default do anything in
|
||||
// particular. We do it ourselves here.
|
||||
// In addition, we use "-----" as a delimiter for new slide.
|
||||
|
||||
// Query all .markdown elements and translate to HTML
|
||||
var markdownDivs = document.querySelectorAll( ".markdown" );
|
||||
for ( var idx = 0; idx < markdownDivs.length; idx++ ) {
|
||||
var element = markdownDivs[ idx ];
|
||||
|
||||
var slides = element.textContent.split( /^-----$/m );
|
||||
var i = slides.length - 1;
|
||||
element.innerHTML = markdown.toHTML( slides[ i ] );
|
||||
|
||||
// If there's an id, unset it for last, and all other, elements,
|
||||
// and then set it for the first.
|
||||
var id = null;
|
||||
if ( element.id ) {
|
||||
id = element.id;
|
||||
element.id = "";
|
||||
}
|
||||
i--;
|
||||
while ( i >= 0 ) {
|
||||
var newElement = element.cloneNode( false );
|
||||
newElement.innerHTML = markdown.toHTML( slides[ i ] );
|
||||
element.parentNode.insertBefore( newElement, element );
|
||||
element = newElement;
|
||||
i--;
|
||||
}
|
||||
if ( id !== null ) {
|
||||
element.id = id;
|
||||
}
|
||||
}
|
||||
} // Markdown
|
||||
|
||||
if ( window.hljs ) {
|
||||
hljs.initHighlightingOnLoad();
|
||||
}
|
||||
|
||||
if ( window.mermaid ) {
|
||||
mermaid.initialize( { startOnLoad:true } );
|
||||
}
|
||||
};
|
||||
|
||||
// Register the plugin to be called in pre-init phase
|
||||
// Note: Markdown.js should run early/first, because it creates new div elements.
|
||||
// So add this with a lower-than-default weight.
|
||||
impress.addPreInitPlugin( preInit, 1 );
|
||||
|
||||
} )( document, window );
|
||||
|
||||
Reference in New Issue
Block a user