mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40:13 +01:00
Add parsers parameter to readConfig/Sync to support non-JSON formats like YAML (fixes #118).
This commit is contained in:
parent
2b4ecdced8
commit
101edd8496
10 changed files with 252 additions and 31 deletions
|
|
@ -422,6 +422,7 @@ function lintFile(
|
|||
}
|
||||
}
|
||||
|
||||
// Lints files and strings
|
||||
function lintInput(options, synchronous, callback) {
|
||||
// Normalize inputs
|
||||
options = options || {};
|
||||
|
|
@ -517,34 +518,64 @@ function markdownlintSync(options) {
|
|||
return results;
|
||||
}
|
||||
|
||||
// Parses the content of a configuration file
|
||||
function parseConfiguration(name, content, parsers) {
|
||||
let config = null;
|
||||
let message = "";
|
||||
const errors = [];
|
||||
// Try each parser
|
||||
(parsers || [ JSON.parse ]).every((parser) => {
|
||||
try {
|
||||
config = parser(content);
|
||||
} catch (ex) {
|
||||
errors.push(ex.message);
|
||||
}
|
||||
return !config;
|
||||
});
|
||||
// Message if unable to parse
|
||||
if (!config) {
|
||||
errors.unshift(`Unable to parse '${name}'`);
|
||||
message = errors.join("; ");
|
||||
}
|
||||
return {
|
||||
config,
|
||||
message
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Read specified configuration file.
|
||||
*
|
||||
* @param {String} file Configuration file name/path.
|
||||
* @param {Array} [parsers] Optional parsing function(s).
|
||||
* @param {Function} callback Callback (err, result) function.
|
||||
* @returns {void}
|
||||
*/
|
||||
function readConfig(file, callback) {
|
||||
function readConfig(file, parsers, callback) {
|
||||
if (!callback) {
|
||||
// @ts-ignore
|
||||
callback = parsers;
|
||||
parsers = null;
|
||||
}
|
||||
// Read file
|
||||
fs.readFile(file, shared.utf8Encoding, function handleFile(err, content) {
|
||||
fs.readFile(file, shared.utf8Encoding, (err, content) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
// Parse file
|
||||
let config = null;
|
||||
try {
|
||||
config = JSON.parse(content);
|
||||
} catch (ex) {
|
||||
return callback(ex);
|
||||
// Try to parse file
|
||||
const { config, message } = parseConfiguration(file, content, parsers);
|
||||
if (!config) {
|
||||
return callback(new Error(message));
|
||||
}
|
||||
if (config.extends) {
|
||||
// Extend configuration
|
||||
const extendsFile = path.resolve(path.dirname(file), config.extends);
|
||||
readConfig(extendsFile, function handleConfig(errr, extendsConfig) {
|
||||
// Extend configuration
|
||||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
delete config.extends;
|
||||
const extendsFile = path.resolve(path.dirname(file), configExtends);
|
||||
readConfig(extendsFile, parsers, (errr, extendsConfig) => {
|
||||
if (errr) {
|
||||
return callback(errr);
|
||||
}
|
||||
delete config.extends;
|
||||
callback(null, shared.assign(extendsConfig, config));
|
||||
});
|
||||
} else {
|
||||
|
|
@ -557,17 +588,24 @@ function readConfig(file, callback) {
|
|||
* Read specified configuration file synchronously.
|
||||
*
|
||||
* @param {String} file Configuration file name/path.
|
||||
* @param {Array} [parsers] Optional parsing function(s).
|
||||
* @returns {Object} Configuration object.
|
||||
*/
|
||||
function readConfigSync(file) {
|
||||
// Parse file
|
||||
let config = JSON.parse(fs.readFileSync(file, shared.utf8Encoding));
|
||||
if (config.extends) {
|
||||
// Extend configuration
|
||||
config = shared.assign(
|
||||
readConfigSync(path.resolve(path.dirname(file), config.extends)),
|
||||
config);
|
||||
function readConfigSync(file, parsers) {
|
||||
// Read file
|
||||
const content = fs.readFileSync(file, shared.utf8Encoding);
|
||||
// Try to parse file
|
||||
const { config, message } = parseConfiguration(file, content, parsers);
|
||||
if (!config) {
|
||||
throw new Error(message);
|
||||
}
|
||||
// Extend configuration
|
||||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
delete config.extends;
|
||||
return shared.assign(
|
||||
readConfigSync(path.resolve(path.dirname(file), configExtends), parsers),
|
||||
config);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue