Skip to content

Instantly share code, notes, and snippets.

@horacioh
Created August 28, 2018 13:38
Show Gist options
  • Save horacioh/504cac444ef3d621264fe0ce44b9436f to your computer and use it in GitHub Desktop.
Save horacioh/504cac444ef3d621264fe0ce44b9436f to your computer and use it in GitHub Desktop.
open mdx presentation from list
// by default it will look for `.mdx` files into the `./presentations` folder
const testFolder = './presentations';
const fs = require('fs');
const path = require('path');
const prompts = require('prompts');
const cmd = require('node-cmd');
if (!fs.existsSync(testFolder)) {
// testFolder does not exist
console.log('no "presentations" file found.');
} else {
fs.readdir(testFolder, async (err, files) => {
// filter files in testFolder that its extension is `.mdx`
const selectedFiles = files.reduce((acc, file) => {
const extension = path.extname(file);
const filename = file.split(extension)[0];
if (extension === '.mdx') {
acc.push({ title: filename, value: file });
}
return acc;
}, []);
// prompt a list of all presentations so you can select one of them.
const response = await prompts({
type: 'select',
name: 'name',
message: 'What presentation you want to load?',
choices: selectedFiles
});
// run `mdx-deck` command.
cmd.run(`mdx-deck ./presentations/${response.name}`);
console.log(`Loading ${response.name} presentation....`);
});
}
/*
TODO:
- accepts options (folder...)
- use case when you want only to select from firs-child directory files. maybe you import another .mdx file from the source...
- pack this as an npm package so you can call this as another package and also to manage versions.
- what else?? let me know!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment