Skip to content

Commit

Permalink
Add option to whitelist Markdown files for conversion (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnv committed Jun 28, 2020
1 parent 25aeb43 commit 3afb09d
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -315,6 +315,12 @@ Providing a directory will result in a stand-alone overview page with links to t
reveal-md dir/ --static
```

By default all `*.md` files in all subdirectories are included in the generated website. You can provide custom [glob pattern](https://github.com/isaacs/node-glob) using `--glob` parameter to generate slides only from some files:

```console
reveal-md dir/ --static --glob '**/slides.md'
```

Additional `--absolute-url` and `--featured-slide` parameters could be used to generate [OpenGraph](http://ogp.me)
metadata enabling more attractive rendering for slide deck links when shared in some social sites.

Expand Down
1 change: 1 addition & 0 deletions bin/help.txt
Expand Up @@ -15,6 +15,7 @@ Options:
--preprocessor <script> Markdown preprocessor script
--template <filename> Template file for reveal.js
--listing-template <filename> Template file for listing
--glob <pattern> Glob pattern to select markdown files for listing and conversion [default: **/*.md]
--print [filename] Print to PDF file
--static [dir] Export static html to directory [_static]. Incompatible with --print.
--static-dirs <dirs> Extra directories to copy into static directory. Only used in conjunction with --static.
Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Expand Up @@ -48,6 +48,7 @@ module.exports.getStaticDir = () => (mergedConfig.static === true ? mergedConfig
module.exports.getHost = () => mergedConfig.host;
module.exports.getPort = () => mergedConfig.port;
module.exports.getWatch = () => Boolean(mergedConfig.watch);
module.exports.getFilesGlob = () => mergedConfig.glob;

module.exports.getOptions = () => mergedConfig;

Expand Down
3 changes: 2 additions & 1 deletion lib/defaults.json
Expand Up @@ -13,5 +13,6 @@
"template": "template/reveal.html",
"theme": "black",
"title": "reveal-md",
"verticalSeparator": "\r?\n----\r?\n"
"verticalSeparator": "\r?\n----\r?\n",
"glob": "**/*.md"
}
10 changes: 3 additions & 7 deletions lib/listing.js
@@ -1,6 +1,6 @@
const glob = require('glob');
const Mustache = require('mustache');
const { getInitialDir, getOptions, getListingTemplate, getThemeUrl } = require('./config');
const { getInitialDir, getOptions, getListingTemplate, getThemeUrl, getFilesGlob } = require('./config');
const { listFiles } = require('./util');

const renderListFile = async files => {
const { title, listingTemplate, theme } = getOptions();
Expand All @@ -16,11 +16,7 @@ const renderListFile = async files => {
};

module.exports = async (req, res) => {
const list = glob.sync('**/*.md', {
cwd: await getInitialDir(),
ignore: '**/node_modules/**'
});

const list = listFiles(await getInitialDir(), getFilesGlob());
const markup = await renderListFile(list);

res.send(markup);
Expand Down
10 changes: 3 additions & 7 deletions lib/static.js
@@ -1,11 +1,10 @@
/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const _ = require('lodash');
const url = require('url');
const { getOptions, getAssetsDir, getPath, getStaticDir, getSlideOptions } = require('./config');
const { isDirectory, parseYamlFrontMatter } = require('./util');
const { getOptions, getAssetsDir, getPath, getStaticDir, getSlideOptions, getFilesGlob } = require('./config');
const { isDirectory, parseYamlFrontMatter, listFiles } = require('./util');
const { revealBasePath, highlightThemePath } = require('./constants');
const { renderFile } = require('./render');
const { renderListFile } = require('./listing');
Expand Down Expand Up @@ -76,10 +75,7 @@ const copyAssetsAndWriteFile = async (sourceDir, file, targetDir) => {

const writeMarkupFiles = async (sourceDir, targetDir) => {
if (await isDirectory(sourceDir)) {
const list = glob.sync('**/*.md', {
cwd: sourceDir,
ignore: 'node_modules/**'
});
const list = listFiles(sourceDir, getFilesGlob());
const listMarkup = await renderListFile(list.map(file => file.replace(/\.md$/, '.html')));
return Promise.all(
_.flatten([
Expand Down
8 changes: 8 additions & 0 deletions lib/util.js
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const _ = require('lodash');
const { promisify } = require('util');
const yamlFrontMatter = require('yaml-front-matter');
const glob = require('glob');

const stat = promisify(fs.stat);

Expand Down Expand Up @@ -31,3 +32,10 @@ module.exports.parseYamlFrontMatter = content => {
markdown: document.__content || content
};
};

module.exports.listFiles = (workingDir, globPattern) => {
return glob.sync(globPattern, {
cwd: workingDir,
ignore: '**/node_modules/**'
});
};
15 changes: 15 additions & 0 deletions test/fixtures/a.md
@@ -0,0 +1,15 @@
Foo

Note: test note

---

Bar

----

Sub Bar

---

The End.
1 change: 1 addition & 0 deletions test/fixtures/slides.md
@@ -0,0 +1 @@
Slides.md file
26 changes: 26 additions & 0 deletions test/fixtures/sub/c.md
@@ -0,0 +1,26 @@
---
title: Foobar
separator: <!--s-->
verticalSeparator: <!--v-->
theme: solarized
revealOptions:
transition: fade
---
Foo

Note: test note

<!--s-->

# Bar

<!--v-->

Sub Bar

* Frag 1 <!-- .element: class="fragment" -->
* Frag 2 <!-- .element: class="fragment" -->

<!--s-->

The End.
1 change: 1 addition & 0 deletions test/fixtures/sub/slides.md
@@ -0,0 +1 @@
Slides.md file in subfolder
19 changes: 19 additions & 0 deletions test/util.spec.js
@@ -0,0 +1,19 @@
const test = require('bron');
const assert = require('assert').strict;
const { join } = require('path');
const { listFiles } = require('../lib/util');
const { getFilesGlob } = require('../lib/config');

const FIXTURES_DIR = join(__dirname, 'fixtures');

test('listFiles should list all MD files with default config', async () => {
const expected = ['a.md', 'slides.md', 'sub/c.md', 'sub/slides.md'];
const actual = listFiles(FIXTURES_DIR, getFilesGlob());
assert.deepEqual(expected, actual);
});

test('listFiles should list only files matching the glob', async () => {
const expected = ['slides.md', 'sub/slides.md'];
const actual = listFiles(FIXTURES_DIR, '**/slides.md');
assert.deepEqual(expected, actual);
});

0 comments on commit 3afb09d

Please sign in to comment.