Skip to content

Commit

Permalink
add support for injecting scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
karimsa committed Jun 9, 2016
1 parent ddd8f4b commit a88e8f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
})
});
}

Expand Down
16 changes: 14 additions & 2 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
});
};


Expand All @@ -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));

Expand Down Expand Up @@ -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);

Expand All @@ -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)
}));
};

Expand Down
4 changes: 4 additions & 0 deletions template/reveal.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>

{{#scripts}}
<script src="/scripts/{{.}}"></script>
{{/scripts}}
</head>
<body>

Expand Down

0 comments on commit a88e8f5

Please sign in to comment.