Permalink
Browse files

eslint > prettier

  • Loading branch information...
webpro committed Oct 5, 2017
1 parent d801f60 commit 45dd6817cf05019e678a40fb13448dbf742cc42b
Showing with 95 additions and 110 deletions.
  1. +4 −0 .prettierrc
  2. +8 −9 lib/index.js
  3. +18 −19 lib/options.js
  4. +7 −11 lib/print.js
  5. +16 −24 lib/render.js
  6. +15 −19 lib/serve.js
  7. +24 −23 lib/static.js
  8. +3 −5 package.json
@@ -0,0 +1,4 @@
{
"printWidth": 120,
"singleQuote": true
}
@@ -1,22 +1,21 @@
'use strict';
const debug = require('debug')('reveal-md'),
bluebird = require('bluebird'),
parseOptions = require('./options').parseOptions,
startServer = require('./serve'),
print = require('./print'),
renderStaticMarkup = require('./static');
const debug = require('debug')('reveal-md');
const bluebird = require('bluebird');
const parseOptions = require('./options').parseOptions;
const startServer = require('./serve');
const print = require('./print');
const renderStaticMarkup = require('./static');

bluebird.promisifyAll(require('fs-extra'));

module.exports = function revealMarkdown(args) {

const options = parseOptions(args);

debug('Parsed options %O', options);

if(options.static) {
if (options.static) {
renderStaticMarkup(options);
} else if(options.print) {
} else if (options.print) {
print(options);
} else {
startServer(options);
@@ -1,39 +1,39 @@
const fs = require('fs'),
path = require('path'),
glob = require('glob'),
url = require('url'),
_ = require('lodash'),
defaults = require('./defaults.json');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const url = require('url');
const _ = require('lodash');
const defaults = require('./defaults.json');

const revealBasePath = path.resolve(require.resolve('reveal.js'), '..', '..');
const highlightThemePath = path.resolve(require.resolve('highlight.js'), '..', '..', 'styles');
const templatePath = path.join(__dirname, 'template', 'reveal.html');
const templateListingPath = path.join(__dirname, 'template', 'listing.html');

const revealThemes = glob.sync('css/theme/*.css', {cwd: revealBasePath});
const localThemes = glob.sync('theme/*.css', {cwd: process.cwd()});
const revealThemes = glob.sync('css/theme/*.css', { cwd: revealBasePath });
const localThemes = glob.sync('theme/*.css', { cwd: process.cwd() });
const availableThemes = revealThemes.concat(localThemes);

function parsePath(pathArg, baseDir) {
const opts = {};
if(pathArg === 'demo') {
if (pathArg === 'demo') {
opts.basePath = path.join(__dirname, '..', 'demo');
opts.baseDir = opts.basePath;
} else if(pathArg) {
} else if (pathArg) {
const parsedUrl = url.parse(pathArg, true, true);
const filePath = path.join(baseDir || process.cwd(), parsedUrl.pathname);
opts.query = parsedUrl.query;
if(fs.existsSync(filePath)) {
if (fs.existsSync(filePath)) {
const stat = fs.statSync(filePath);
if(stat.isFile()) {
if (stat.isFile()) {
opts.basePath = path.dirname(filePath);
opts.baseName = path.basename(filePath);
opts.relativePath = path.relative(process.cwd(), filePath);
} else if(stat.isDirectory()) {
} else if (stat.isDirectory()) {
opts.baseDir = filePath;
}
} else {
if(parsedUrl.host) {
if (parsedUrl.host) {
opts.url = parsedUrl.href;
} else {
opts.basePath = process.cwd();
@@ -45,7 +45,7 @@ function parsePath(pathArg, baseDir) {

function getThemeUrl(options) {
const parsedUrl = typeof options.themePath === 'string' && url.parse(options.themePath);
if(parsedUrl.host) {
if (parsedUrl.host) {
return parsedUrl.href;
} else {
return `${options.base()}/${options.themePath}`;
@@ -54,7 +54,7 @@ function getThemeUrl(options) {

function parseThemeArg(theme) {
const parsedUrl = url.parse(theme);
if(parsedUrl.host) {
if (parsedUrl.host) {
return theme;
} else {
return availableThemes.find(themePath => path.basename(themePath).replace(path.extname(themePath), '') === theme);
@@ -97,10 +97,9 @@ const optionList = [
];

function parseOptions(args) {

const options = _.pick(args, optionList);

if(_.has(args, 'args.0')) _.extend(options, parsePath(args.args[0]));
if (_.has(args, 'args.0')) _.extend(options, parsePath(args.args[0]));

options.themePath = parseThemeArg(args.theme || defaults.theme);
options.scriptPaths = parseAssetsArg(args.scripts);
@@ -116,7 +115,7 @@ function parseOptions(args) {
options.revealBasePath = revealBasePath;
options.highlightThemePath = highlightThemePath;

options.base = () => options.static ? '.' : '';
options.base = () => (options.static ? '.' : '');
options.template = () => fs.readFileSync(options.revealTemplate || templatePath).toString();
options.templateListing = () => fs.readFileSync(options.listingTemplate || templateListingPath).toString();
options.themeUrl = () => getThemeUrl(options);
@@ -1,28 +1,24 @@
const path = require('path'),
_ = require('lodash'),
startServer = require('./serve'),
exec = require('child_process').exec;
const path = require('path');
const _ = require('lodash');
const startServer = require('./serve');
const exec = require('child_process').exec;

module.exports = function print(options) {

const printPluginPath = path.join(options.revealBasePath, 'plugin', 'print-pdf', 'print-pdf.js');

const opts = _.extend({}, options, {
disableAutoOpen: true
});

startServer(opts, function(server) {

/* eslint-disable no-console */

const url = `http://${options.host}:${options.port}/${options.relativePath}`;
const pdfFilename = options.print === true ? options.baseName.replace(/\.md$/, '') : options.print.replace(/\.pdf$/, '');
const pdfFilename =
options.print === true ? options.baseName.replace(/\.md$/, '') : options.print.replace(/\.pdf$/, '');

console.log(`Attempting to print "${options.relativePath}" to filename "${pdfFilename}.pdf" as PDF.`);
console.log('Make sure to have PhantomJS installed (and in your path).');

exec(`phantomjs ${printPluginPath} "${url}?print-pdf" "${pdfFilename}.pdf"`, function(err, stdout, stderr) {
if(err) {
if (err) {
console.error(`[Error while generating PDF for "${options.relativePath}"]\n${stderr}\n${err.toString()}`);
} else {
console.log(stdout);
@@ -1,12 +1,12 @@
const fs = require('fs-extra'),
debug = require('debug')('reveal-md'),
Mustache = require('mustache'),
got = require('got'),
glob = require('glob'),
yamlFrontMatter = require('yaml-front-matter'),
md = require('reveal.js/plugin/markdown/markdown'),
_ = require('lodash'),
parseOptions = require('./options').parseOptions;
const fs = require('fs-extra');
const debug = require('debug')('reveal-md');
const Mustache = require('mustache');
const got = require('got');
const glob = require('glob');
const yamlFrontMatter = require('yaml-front-matter');
const md = require('reveal.js/plugin/markdown/markdown');
const _ = require('lodash');
const parseOptions = require('./options').parseOptions;

function parseYamlFrontMatter(content) {
const document = yamlFrontMatter.loadFront(content.replace(/^\uFEFF/, ''));
@@ -17,12 +17,7 @@ function parseYamlFrontMatter(content) {
}

function getSlidifyOptions(options) {
return _.pick(options, [
'attributes',
'notesSeparator',
'separator',
'verticalSeparator'
]);
return _.pick(options, ['attributes', 'notesSeparator', 'separator', 'verticalSeparator']);
}

function parseSlides(markdown, options) {
@@ -37,9 +32,7 @@ function parseSlides(markdown, options) {
}

function render(markdown, options) {

const slides = parseSlides(markdown, options);

const view = _.extend(slides.view, {
slides: slides.slides,
scripts: slides.view.scripts
@@ -51,9 +44,7 @@ function render(markdown, options) {
}

function renderMarkdownAsSlides(req, res) {

const options = res.locals.options;

const readMarkdown = options.url
? got(options.url).then(response => response.body)
: fs.readFileAsync(options.relativePath).then(markdown => markdown.toString());
@@ -62,12 +53,13 @@ function renderMarkdownAsSlides(req, res) {
}

function renderMarkdownFileListing(req, res) {

const options = res.locals.options;
const list = glob.sync('**/*.md', {
cwd: options.baseDir,
ignore: 'node_modules/**'
}).map(filePath => `<a href="${filePath}">${filePath}</a>`);
const list = glob
.sync('**/*.md', {
cwd: options.baseDir,
ignore: 'node_modules/**'
})
.map(filePath => `<a href="${filePath}">${filePath}</a>`);

const markup = Mustache.to_html(options.templateListing(), {
themeUrl: options.themeUrl,
@@ -1,13 +1,13 @@
'use strict';
const path = require('path'),
got = require('got'),
debug = require('debug')('reveal-md'),
express = require('express'),
_ = require('lodash'),
liveReload = require('livereload'),
open = require('open'),
render = require('./render'),
parsePath = require('./options').parsePath;
const path = require('path');
const got = require('got');
const debug = require('debug')('reveal-md');
const express = require('express');
const _ = require('lodash');
const liveReload = require('livereload');
const open = require('open');
const render = require('./render');
const parsePath = require('./options').parsePath;

const staticDir = express.static;

@@ -25,7 +25,6 @@ function getAsset(req, res) {
}

module.exports = function startServer(options, cb) {

initialOptions = options;

const app = express();
@@ -36,7 +35,7 @@ module.exports = function startServer(options, cb) {

app.use('/css/highlight', staticDir(options.highlightThemePath));

if(options.watch) {
if (options.watch) {
const liveReloadServer = liveReload.createServer({
exts: ['md']
});
@@ -46,27 +45,24 @@ module.exports = function startServer(options, cb) {
app.get('/', updatePathOptions, render.renderMarkdownFileListing);
app.get(/(\w+\.md)$/, updatePathOptions, render.renderMarkdownAsSlides);
app.get('/assets/*', getAsset);
if(options.basePath || options.baseDir) {
if (options.basePath || options.baseDir) {
const virtualPath = options.relativePath ? path.dirname(options.relativePath) : '.';
app.use(virtualPath !== '.' ? '/' + virtualPath : '/', staticDir(options.basePath || options.baseDir));
} else if(options.url) {
} else if (options.url) {
app.use('/*', (req, res) => got.stream(req.params[0]).pipe(res));
}

const server = app.listen(options.port);

const host = `http://${options.host}:${options.port}`;
const link = options.relativePath || options.url;
const url = `${host}${link ? '/' + link : ''}`;

if(!options.print) {
/* eslint-disable no-console */
if (!options.print) {
console.log(`Reveal-server started at ${host}`);
if(!options.disableAutoOpen) {
if (!options.disableAutoOpen) {
open(url);
}
}

if(cb) cb(server);

if (cb) cb(server);
};
@@ -1,43 +1,43 @@
'use strict';
const fs = require('fs-extra'),
path = require('path'),
render = require('./render').render,
imageDataURI = require('image-data-uri'),
_ = require('lodash');
const fs = require('fs-extra');
const path = require('path');
const render = require('./render').render;
const imageDataURI = require('image-data-uri');
const _ = require('lodash');

const mdImageRE = /!\[([^\]]*)\]\(([^)]+)\)/gi;

function embedImages(markdown, options) {

/* eslint-disable no-console, no-cond-assign */

let image;
const awaits = [];

while(image = mdImageRE.exec(markdown)) {
while ((image = mdImageRE.exec(markdown))) {
const imgMarkdown = image[0],
imgTitle = image[1],
imgPath = image[2];
awaits.push(imageDataURI.encodeFromFile(path.join(options.basePath, imgPath)).then(dataUri => {
markdown = markdown.replace(imgMarkdown, `![${imgTitle}](${dataUri})`);
}));
awaits.push(
imageDataURI.encodeFromFile(path.join(options.basePath, imgPath)).then(dataUri => {
markdown = markdown.replace(imgMarkdown, `![${imgTitle}](${dataUri})`);
})
);
}

return Promise.all(awaits).then(() => markdown).catch(console.error);

return Promise.all(awaits)
.then(() => markdown)
.catch(console.error);
}

module.exports = function renderStaticMarkup(options) {

/* eslint-disable no-console */

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)));
const awaits = ['css', 'js', 'plugin', 'lib'].map(dir =>
fs.copyAsync(path.join(options.revealBasePath, dir), path.join(targetPath, dir))
);

const markupAwait = fs.readFileAsync(options.relativePath)
const markupAwait = fs
.readFileAsync(options.relativePath)
.then(markdown => markdown.toString())
.then(markdown => embedImages(markdown, options))
.then(markdown => render(markdown, options))
@@ -48,18 +48,19 @@ module.exports = function renderStaticMarkup(options) {
awaits.push(markupAwait);
awaits.push(highlightAwait);

if(!_.isEmpty(options.scripts)) {
if (!_.isEmpty(options.scripts)) {
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)) {
if (!_.isEmpty(options.css)) {
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);

Promise.all(awaits)
.then(() => console.log(`Wrote static site to ${targetPath}`))
.catch(console.error);
};
Oops, something went wrong.

0 comments on commit 45dd681

Please sign in to comment.