Skip to content

Commit

Permalink
Refactor custom scripts/styles handling
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 21, 2017
1 parent d507df2 commit 73d1476
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
20 changes: 10 additions & 10 deletions lib/options.js
Expand Up @@ -61,14 +61,14 @@ function parseThemeArg(theme) {
}
}

function parseScriptsArg(scripts) {
return (typeof scripts === 'string' ? scripts.split(',') : []).map(script => path.join('scripts', script));
function parseAssetsArg(assets) {
return (typeof assets === 'string' ? assets.split(',') : []).map(asset => `assets/${asset}`);
}

function parseScriptsPath(scripts) {
return (typeof scripts === 'string' ? scripts.split(',') : []).map(script => ({
path: path.resolve(process.cwd(), script),
name: script
function parseAssetsPath(assets) {
return (typeof assets === 'string' ? assets.split(',') : []).map(asset => ({
path: path.resolve(process.cwd(), asset),
name: asset
}));
}

Expand Down Expand Up @@ -101,10 +101,10 @@ function parseOptions(args) {
if(_.has(args, 'args.0')) _.extend(options, parsePath(args.args[0]));

options.themePath = parseThemeArg(args.theme || defaults.theme);
options.scriptPaths = parseScriptsArg(args.scripts);
options.scriptSources = parseScriptsPath(args.scripts);
options.cssPaths = parseScriptsArg(args.css);
options.cssSources = parseScriptsPath(args.css);
options.scriptPaths = parseAssetsArg(args.scripts);
options.scriptSources = parseAssetsPath(args.scripts);
options.cssPaths = parseAssetsArg(args.css);
options.cssSources = parseAssetsPath(args.css);
options.title = args.title || defaults.title;
options.separator = args.separator || defaults.separator;
options.verticalSeparator = args.verticalSeparator || defaults.verticalSeparator;
Expand Down
6 changes: 3 additions & 3 deletions lib/serve.js
Expand Up @@ -19,8 +19,8 @@ function updatePathOptions(req, res, next) {
next();
}

function getScript(req, res) {
res.sendFile(path.resolve(process.cwd(), req.url.replace(/^\/scripts\//, '')));
function getAsset(req, res) {
res.sendFile(path.resolve(process.cwd(), req.url.replace(/^\/assets\//, '')));
}

module.exports = function startServer(options, cb) {
Expand All @@ -44,7 +44,7 @@ module.exports = function startServer(options, cb) {

app.get('/', updatePathOptions, render.renderMarkdownFileListing);
app.get(/(\w+\.md)$/, updatePathOptions, render.renderMarkdownAsSlides);
app.get('/scripts/*', getScript);
app.get('/assets/*', getAsset);
app.get('/*', staticDir(process.cwd()));

const server = app.listen(options.port);
Expand Down
15 changes: 7 additions & 8 deletions lib/static.js
Expand Up @@ -9,6 +9,7 @@ module.exports = function renderStaticMarkup(options) {

const staticPath = options.static === true ? '_static' : options.static;
const targetPath = path.resolve(process.cwd(), staticPath);
const assetsDir = path.join(targetPath, 'assets');

const awaits = ['css', 'js', 'plugin', 'lib'].map(dir => fs.copyAsync(path.join(options.revealBasePath, dir), path.join(targetPath, dir)));

Expand All @@ -22,17 +23,15 @@ module.exports = function renderStaticMarkup(options) {
awaits.push(highlightAwait);

if(!_.isEmpty(options.scripts)) {
const scriptsDir = path.join(targetPath, 'scripts');
fs.ensureDirSync(scriptsDir);
const scriptAwaits = options.scriptSources.map(scriptFile => fs.copyAsync(scriptFile.path, path.join(scriptsDir, scriptFile.name)));
awaits.concat(scriptAwaits);
fs.ensureDirSync(assetsDir);
const assetAwaits = options.scriptSources.map(asset => fs.copyAsync(asset.path, path.join(assetsDir, asset.name)));
awaits.concat(assetAwaits);
}

if(!_.isEmpty(options.css)) {
const scriptsDir = path.join(targetPath, 'scripts');
fs.ensureDirSync(scriptsDir);
const scriptAwaits = options.cssSources.map(scriptFile => fs.copyAsync(scriptFile.path, path.join(scriptsDir, scriptFile.name)));
awaits.concat(scriptAwaits);
fs.ensureDirSync(assetsDir);
const assetAwaits = options.cssSources.map(asset => fs.copyAsync(asset.path, path.join(assetsDir, asset.name)));
awaits.concat(assetAwaits);
}

Promise.all(awaits).then(() => console.log(`Wrote static site to ${targetPath}`)).catch(console.error);
Expand Down
8 changes: 4 additions & 4 deletions test/render.spec.js
Expand Up @@ -25,15 +25,15 @@ describe('render', () => {

it('should render custom scripts', () => {
const actual = render.render('# header', {scripts: 'custom.js,also.js'});
expect(actual).toInclude('<script src="./scripts/custom.js"></script>');
expect(actual).toInclude('<script src="./scripts/also.js"></script>');
expect(actual).toInclude('<script src="./assets/custom.js"></script>');
expect(actual).toInclude('<script src="./assets/also.js"></script>');
});

it('should render custom css after theme', () => {
const actual = render.render('# header', {css: 'style1.css,style2.css'});
const themeLink = '<link rel="stylesheet" href="./css/highlight/zenburn.css">';
const style1Link = '<link rel="stylesheet" href="./scripts/style1.css">';
const style2Link = '<link rel="stylesheet" href="./scripts/style2.css">';
const style1Link = '<link rel="stylesheet" href="./assets/style1.css">';
const style2Link = '<link rel="stylesheet" href="./assets/style2.css">';
expect(actual).toInclude(themeLink);
expect(actual).toInclude(style1Link);
expect(actual).toInclude(style2Link);
Expand Down

0 comments on commit 73d1476

Please sign in to comment.