mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add "configParsers" option so custom parsers can be used to handle the content of markdownlint-configure-file inline comments (fixes #528).
This commit is contained in:
parent
bbec8c5c1e
commit
00082ee8a5
6 changed files with 267 additions and 108 deletions
|
@ -316,6 +316,39 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
|||
return effectiveConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the content of a configuration file.
|
||||
*
|
||||
* @param {string} name Name of the configuration file.
|
||||
* @param {string} content Configuration content.
|
||||
* @param {ConfigurationParser[]} parsers Parsing function(s).
|
||||
* @returns {Object} Configuration object and error message.
|
||||
*/
|
||||
function parseConfiguration(name, content, parsers) {
|
||||
let config = null;
|
||||
let message = "";
|
||||
const errors = [];
|
||||
let index = 0;
|
||||
// Try each parser
|
||||
(parsers || [ JSON.parse ]).every((parser) => {
|
||||
try {
|
||||
config = parser(content);
|
||||
} catch (error) {
|
||||
errors.push(`Parser ${index++}: ${error.message}`);
|
||||
}
|
||||
return !config;
|
||||
});
|
||||
// Message if unable to parse
|
||||
if (!config) {
|
||||
errors.unshift(`Unable to parse '${name}'`);
|
||||
message = errors.join("; ");
|
||||
}
|
||||
return {
|
||||
config,
|
||||
message
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a mapping of enabled rules per line.
|
||||
*
|
||||
|
@ -324,6 +357,7 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
|||
* @param {string[]} frontMatterLines List of front matter lines.
|
||||
* @param {boolean} noInlineConfig Whether to allow inline configuration.
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {ConfigurationParser[]} configParsers Configuration parsers.
|
||||
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule
|
||||
* names.
|
||||
* @returns {Object} Effective configuration and enabled rules per line number.
|
||||
|
@ -334,6 +368,7 @@ function getEnabledRulesPerLineNumber(
|
|||
frontMatterLines,
|
||||
noInlineConfig,
|
||||
config,
|
||||
configParsers,
|
||||
aliasToRuleNames) {
|
||||
// Shared variables
|
||||
let enabledRules = {};
|
||||
|
@ -365,14 +400,14 @@ function getEnabledRulesPerLineNumber(
|
|||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function configureFile(action, parameter) {
|
||||
if (action === "CONFIGURE-FILE") {
|
||||
try {
|
||||
const json = JSON.parse(parameter);
|
||||
const { "config": parsed } = parseConfiguration(
|
||||
"CONFIGURE-FILE", parameter, configParsers
|
||||
);
|
||||
if (parsed) {
|
||||
config = {
|
||||
...config,
|
||||
...json
|
||||
...parsed
|
||||
};
|
||||
} catch {
|
||||
// Ignore parse errors for inline configuration
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -452,6 +487,7 @@ function getEnabledRulesPerLineNumber(
|
|||
* @param {string} content Markdown content.
|
||||
* @param {Object} md Instance of markdown-it.
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {ConfigurationParser[]} configParsers Configuration parsers.
|
||||
* @param {RegExp} frontMatter Regular expression for front matter.
|
||||
* @param {boolean} handleRuleFailures Whether to handle exceptions in rules.
|
||||
* @param {boolean} noInlineConfig Whether to allow inline configuration.
|
||||
|
@ -465,6 +501,7 @@ function lintContent(
|
|||
content,
|
||||
md,
|
||||
config,
|
||||
configParsers,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
|
@ -484,6 +521,7 @@ function lintContent(
|
|||
frontMatterLines,
|
||||
noInlineConfig,
|
||||
config,
|
||||
configParsers,
|
||||
mapAliasToRuleNames(ruleList)
|
||||
);
|
||||
// Hide the content of HTML comments from rules, etc.
|
||||
|
@ -720,6 +758,7 @@ function lintContent(
|
|||
* @param {string} file Path of file to lint.
|
||||
* @param {Object} md Instance of markdown-it.
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {ConfigurationParser[]} configParsers Configuration parsers.
|
||||
* @param {RegExp} frontMatter Regular expression for front matter.
|
||||
* @param {boolean} handleRuleFailures Whether to handle exceptions in rules.
|
||||
* @param {boolean} noInlineConfig Whether to allow inline configuration.
|
||||
|
@ -734,6 +773,7 @@ function lintFile(
|
|||
file,
|
||||
md,
|
||||
config,
|
||||
configParsers,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
|
@ -746,8 +786,19 @@ function lintFile(
|
|||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return lintContent(ruleList, file, content, md, config, frontMatter,
|
||||
handleRuleFailures, noInlineConfig, resultVersion, callback);
|
||||
return lintContent(
|
||||
ruleList,
|
||||
file,
|
||||
content,
|
||||
md,
|
||||
config,
|
||||
configParsers,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
resultVersion,
|
||||
callback
|
||||
);
|
||||
}
|
||||
// Make a/synchronous call to read file
|
||||
if (synchronous) {
|
||||
|
@ -784,6 +835,7 @@ function lintInput(options, synchronous, callback) {
|
|||
const strings = options.strings || {};
|
||||
const stringsKeys = Object.keys(strings);
|
||||
const config = options.config || { "default": true };
|
||||
const configParsers = options.configParsers || null;
|
||||
const frontMatter = (options.frontMatter === undefined) ?
|
||||
helpers.frontMatterRe : options.frontMatter;
|
||||
const handleRuleFailures = !!options.handleRuleFailures;
|
||||
|
@ -827,6 +879,7 @@ function lintInput(options, synchronous, callback) {
|
|||
currentItem,
|
||||
md,
|
||||
config,
|
||||
configParsers,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
|
@ -845,6 +898,7 @@ function lintInput(options, synchronous, callback) {
|
|||
strings[currentItem] || "",
|
||||
md,
|
||||
config,
|
||||
configParsers,
|
||||
frontMatter,
|
||||
handleRuleFailures,
|
||||
noInlineConfig,
|
||||
|
@ -918,39 +972,6 @@ function markdownlintSync(options) {
|
|||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the content of a configuration file.
|
||||
*
|
||||
* @param {string} name Name of the configuration file.
|
||||
* @param {string} content Configuration content.
|
||||
* @param {ConfigurationParser[]} parsers Parsing function(s).
|
||||
* @returns {Object} Configuration object and error message.
|
||||
*/
|
||||
function parseConfiguration(name, content, parsers) {
|
||||
let config = null;
|
||||
let message = "";
|
||||
const errors = [];
|
||||
let index = 0;
|
||||
// Try each parser
|
||||
(parsers || [ JSON.parse ]).every((parser) => {
|
||||
try {
|
||||
config = parser(content);
|
||||
} catch (error) {
|
||||
errors.push(`Parser ${index++}: ${error.message}`);
|
||||
}
|
||||
return !config;
|
||||
});
|
||||
// Message if unable to parse
|
||||
if (!config) {
|
||||
errors.unshift(`Unable to parse '${name}'`);
|
||||
message = errors.join("; ");
|
||||
}
|
||||
return {
|
||||
config,
|
||||
message
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve referenced "extends" path in a configuration file
|
||||
* using path.resolve() with require.resolve() as a fallback.
|
||||
|
@ -1237,16 +1258,17 @@ module.exports = markdownlint;
|
|||
* Configuration options.
|
||||
*
|
||||
* @typedef {Object} Options
|
||||
* @property {string[] | string} [files] Files to lint.
|
||||
* @property {Object.<string, string>} [strings] Strings to lint.
|
||||
* @property {Configuration} [config] Configuration object.
|
||||
* @property {ConfigurationParser[]} [configParsers] Configuration parsers.
|
||||
* @property {Rule[] | Rule} [customRules] Custom rules.
|
||||
* @property {string[] | string} [files] Files to lint.
|
||||
* @property {RegExp} [frontMatter] Front matter pattern.
|
||||
* @property {Object} [fs] File system implementation.
|
||||
* @property {boolean} [handleRuleFailures] True to catch exceptions.
|
||||
* @property {Plugin[]} [markdownItPlugins] Additional plugins.
|
||||
* @property {boolean} [noInlineConfig] True to ignore HTML directives.
|
||||
* @property {number} [resultVersion] Results object version.
|
||||
* @property {Plugin[]} [markdownItPlugins] Additional plugins.
|
||||
* @property {Object} [fs] File system implementation.
|
||||
* @property {Object.<string, string>} [strings] Strings to lint.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue