From a88e8f5a28fb2a11b1a881bd3edfd364130d8210 Mon Sep 17 00:00:00 2001 From: Karim Alibhai Date: Thu, 9 Jun 2016 13:30:50 -0400 Subject: [PATCH] add support for injecting scripts --- bin/cli.js | 6 +++++- bin/server.js | 16 ++++++++++++++-- template/reveal.html | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 4122848..f25bb21 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -26,6 +26,7 @@ program .option('-r, --print [filename]', 'Print') .option('-s, --separator [separator]', 'Slide separator') .option('-v, --verticalSeparator [vertical separator]', 'Vertical slide separator') + .option('-i, --scripts [list of scripts]', 'Scripts to inject into the page') .option('--disableAutoOpen', 'Disable to automatically open your web browser') .option('--static', 'Export static html to stdout. Save to reveal.js/index.html to' + ' match dependencies. HINT: printing does not work properly in this mode') @@ -128,7 +129,10 @@ if (program.static) { verticalSeparator: program.verticalSeparator, printFile: program.print, revealOptions: revealOptions, - openWebBrowser: !program.disableAutoOpen + openWebBrowser: !program.disableAutoOpen, + scripts: (program.scripts || '').split(',').map(function (script) { + return script[0] === '/' ? script : path.resolve(process.cwd(), script); + }) }); } diff --git a/bin/server.js b/bin/server.js index 50fc98a..7bdb01c 100644 --- a/bin/server.js +++ b/bin/server.js @@ -28,7 +28,8 @@ var opts = { title: 'reveal-md', separator: '^(\r\n?|\n)---(\r\n?|\n)$', verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$', - revealOptions: {} + revealOptions: {}, + scripts: {} }; var printPluginPath = path.join(serverBasePath, 'node_modules', 'reveal.js', 'plugin', 'print-pdf', 'print-pdf.js'); @@ -49,6 +50,11 @@ var fillOpts = function(options) { opts.printMode = typeof options.printFile !== 'undefined' && options.printFile || opts.printMode; opts.revealOptions = options.revealOptions || {}; opts.openWebBrowser = options.openWebBrowser; + + opts.scripts = {}; + options.scripts.forEach(function (script) { + opts.scripts[ path.basename(script) ] = script; + }); }; @@ -63,6 +69,7 @@ var startMarkdownServer = function(options) { staticDir(path.join(serverBasePath, 'node_modules', 'highlight.js', 'styles', opts.highlightTheme + '.css'))); app.get(/(\w+\.md)$/, renderMarkdownAsSlides); + app.get('/scripts/*', getScript); app.get('/', renderMarkdownFileListing); app.get('/*', staticDir(opts.userBasePath)); @@ -142,6 +149,10 @@ var renderMarkdownAsSlides = function(req, res) { } }; +var getScript = function (req, res) { + res.sendFile( opts.scripts[req.url.substr( req.url.indexOf('/scripts/') + 9 )] ); +}; + var render = function(res, markdown) { var slides = md.slidify(markdown, opts); @@ -150,7 +161,8 @@ var render = function(res, markdown) { highlightTheme: opts.highlightTheme, title: opts.title, slides: slides, - options: JSON.stringify(opts.revealOptions, null, 2) + options: JSON.stringify(opts.revealOptions, null, 2), + scripts: Object.keys(opts.scripts) })); }; diff --git a/template/reveal.html b/template/reveal.html index 61ccb3d..ece11a9 100644 --- a/template/reveal.html +++ b/template/reveal.html @@ -12,6 +12,10 @@ + + {{#scripts}} + + {{/scripts}}