Skip to content

Commit

Permalink
Added support for json5 config files (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartenBE authored Nov 5, 2023
1 parent 393f1f4 commit b1bb17d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
14 changes: 10 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const path = require('path');
const tryRequire = require('try-require');
const _ = require('lodash');
const fs = require('fs-extra');
const defaults = require('./defaults.json');
const localConfig = tryRequire(path.join(process.cwd(), 'reveal-md.json'));
const revealConfig = tryRequire(path.join(process.cwd(), 'reveal.json'));
const parseArgs = require('yargs-parser');
const url = require('url');
const glob = require('glob');
const { isDirectory, isFile, isAbsoluteURL } = require('./util');
const { isDirectory, isFile, isAbsoluteURL, tryReadJson5Configs } = require('./util');

const localConfig = tryReadJson5Configs(
path.join(process.cwd(), 'reveal-md.json5'),
path.join(process.cwd(), 'reveal-md.json')
);
const revealConfig = tryReadJson5Configs(
path.join(process.cwd(), 'reveal.json5'),
path.join(process.cwd(), 'reveal.json')
);

const revealBasePath = path.resolve(require.resolve('reveal.js'), '..', '..');

Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const { promisify } = require('util');
const yamlFrontMatter = require('yaml-front-matter');
const glob = require('glob');
const json5 = require('json5');

const stat = promisify(fs.stat);

Expand Down Expand Up @@ -41,3 +42,17 @@ module.exports.getFilePaths = (workingDir, globPattern) => {
};

module.exports.isAbsoluteURL = path => path.indexOf('://') > 0 || path.indexOf('//') === 0;

module.exports.tryReadJson5Configs = (...possibleConfigFiles) => {
for (const configFile of possibleConfigFiles) {
try {
return json5.parse(fs.readFileSync(configFile));
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}

return undefined;
};
58 changes: 33 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
"fs-extra": "11.1.1",
"glob": "8.1.0",
"highlight.js": "11.9.0",
"json5": "2.2.3",
"livereload": "0.9.3",
"lodash": "4.17.21",
"mustache": "4.2.0",
"open": "8.4.2",
"reveal.js": "4.5.0",
"serve-favicon": "2.5.0",
"try-require": "1.2.1",
"update-notifier": "5.1.0",
"yaml-front-matter": "4.1.1",
"yargs-parser": "21.1.1"
Expand Down

0 comments on commit b1bb17d

Please sign in to comment.