mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
// @ts-check
|
|
|
|
/**
|
|
* Result of a call to parseConfiguration.
|
|
*
|
|
* @typedef {Object} ParseConfigurationResult
|
|
* @property {import("markdownlint").Configuration | null} config Configuration object if successful.
|
|
* @property {string | null} message Error message if an error occurred.
|
|
*/
|
|
|
|
/**
|
|
* Parse the content of a configuration file.
|
|
*
|
|
* @param {string} name Name of the configuration file.
|
|
* @param {string} content Configuration content.
|
|
* @param {import("markdownlint").ConfigurationParser[]} [parsers] Parsing function(s).
|
|
* @returns {ParseConfigurationResult} Parse configuration result.
|
|
*/
|
|
export default function parseConfiguration(name, content, parsers) {
|
|
let config = null;
|
|
let message = null;
|
|
const errors = [];
|
|
let index = 0;
|
|
// Try each parser
|
|
const failed = (parsers || [ JSON.parse ]).every((parser) => {
|
|
try {
|
|
const result = parser(content);
|
|
config = (result && (typeof result === "object") && !Array.isArray(result)) ? result : {};
|
|
// Succeeded
|
|
return false;
|
|
// eslint-disable-next-line jsdoc/reject-any-type
|
|
} catch(/** @type {any} */ error) {
|
|
errors.push(`Parser ${index++}: ${error?.message}`);
|
|
}
|
|
// Failed, try the next parser
|
|
return true;
|
|
});
|
|
// Message if unable to parse
|
|
if (failed) {
|
|
errors.unshift(`Unable to parse '${name}'`);
|
|
message = errors.join("; ");
|
|
}
|
|
return {
|
|
config,
|
|
message
|
|
};
|
|
}
|