mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Expose shared.js helper code for custom rule authors (fixes #134).
This commit is contained in:
parent
f614f3e1ce
commit
7e980401b8
52 changed files with 283 additions and 184 deletions
|
|
@ -7,7 +7,7 @@ const path = require("path");
|
|||
const { URL } = require("url");
|
||||
const markdownIt = require("markdown-it");
|
||||
const rules = require("./rules");
|
||||
const shared = require("./shared");
|
||||
const helpers = require("../helpers");
|
||||
const cache = require("./cache");
|
||||
|
||||
const deprecatedRuleNames = [ "MD002" ];
|
||||
|
|
@ -31,7 +31,7 @@ function validateRuleList(ruleList) {
|
|||
const value = rule[property];
|
||||
if (!result &&
|
||||
(!value || !Array.isArray(value) || (value.length === 0) ||
|
||||
!value.every(shared.isString) || value.some(shared.isEmptyString))) {
|
||||
!value.every(helpers.isString) || value.some(helpers.isEmptyString))) {
|
||||
result = newError(property);
|
||||
}
|
||||
});
|
||||
|
|
@ -134,7 +134,7 @@ function removeFrontMatter(content, frontMatter) {
|
|||
if (frontMatterMatch && !frontMatterMatch.index) {
|
||||
const contentMatched = frontMatterMatch[0];
|
||||
content = content.slice(contentMatched.length);
|
||||
frontMatterLines = contentMatched.split(shared.newLineRe);
|
||||
frontMatterLines = contentMatched.split(helpers.newLineRe);
|
||||
if (frontMatterLines.length &&
|
||||
(frontMatterLines[frontMatterLines.length - 1] === "")) {
|
||||
frontMatterLines.length--;
|
||||
|
|
@ -269,12 +269,12 @@ function getEnabledRulesPerLineNumber(
|
|||
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
||||
lines.forEach(function forLine(line) {
|
||||
if (!noInlineConfig) {
|
||||
let match = shared.inlineCommentRe.exec(line);
|
||||
let match = helpers.inlineCommentRe.exec(line);
|
||||
if (match) {
|
||||
enabledRules = shared.clone(enabledRules);
|
||||
enabledRules = helpers.clone(enabledRules);
|
||||
while (match) {
|
||||
forMatch(match);
|
||||
match = shared.inlineCommentRe.exec(line);
|
||||
match = helpers.inlineCommentRe.exec(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -310,10 +310,10 @@ function lintContent(
|
|||
const removeFrontMatterResult = removeFrontMatter(content, frontMatter);
|
||||
const frontMatterLines = removeFrontMatterResult.frontMatterLines;
|
||||
// Ignore the content of HTML comments
|
||||
content = shared.clearHtmlCommentText(removeFrontMatterResult.content);
|
||||
content = helpers.clearHtmlCommentText(removeFrontMatterResult.content);
|
||||
// Parse content into tokens and lines
|
||||
const tokens = md.parse(content, {});
|
||||
const lines = content.split(shared.newLineRe);
|
||||
const lines = content.split(helpers.newLineRe);
|
||||
annotateTokens(tokens, lines);
|
||||
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
|
||||
const effectiveConfig =
|
||||
|
|
@ -328,8 +328,8 @@ function lintContent(
|
|||
lines,
|
||||
frontMatterLines
|
||||
};
|
||||
cache.lineMetadata(shared.getLineMetadata(params));
|
||||
cache.flattenedLists(shared.flattenLists(params));
|
||||
cache.lineMetadata(helpers.getLineMetadata(params));
|
||||
cache.flattenedLists(helpers.flattenLists(params));
|
||||
// Function to run for each rule
|
||||
const result = (resultVersion === 0) ? {} : [];
|
||||
function forRule(rule) {
|
||||
|
|
@ -345,22 +345,22 @@ function lintContent(
|
|||
function onError(errorInfo) {
|
||||
if (!errorInfo ||
|
||||
!errorInfo.lineNumber ||
|
||||
!shared.isNumber(errorInfo.lineNumber)) {
|
||||
!helpers.isNumber(errorInfo.lineNumber)) {
|
||||
throwError("lineNumber");
|
||||
}
|
||||
if (errorInfo.detail &&
|
||||
!shared.isString(errorInfo.detail)) {
|
||||
!helpers.isString(errorInfo.detail)) {
|
||||
throwError("detail");
|
||||
}
|
||||
if (errorInfo.context &&
|
||||
!shared.isString(errorInfo.context)) {
|
||||
!helpers.isString(errorInfo.context)) {
|
||||
throwError("context");
|
||||
}
|
||||
if (errorInfo.range &&
|
||||
(!Array.isArray(errorInfo.range) ||
|
||||
(errorInfo.range.length !== 2) ||
|
||||
!shared.isNumber(errorInfo.range[0]) ||
|
||||
!shared.isNumber(errorInfo.range[1]))) {
|
||||
!helpers.isNumber(errorInfo.range[0]) ||
|
||||
!helpers.isNumber(errorInfo.range[1]))) {
|
||||
throwError("range");
|
||||
}
|
||||
errors.push({
|
||||
|
|
@ -440,9 +440,9 @@ function lintFile(
|
|||
}
|
||||
// Make a/synchronous call to read file
|
||||
if (synchronous) {
|
||||
lintContentWrapper(null, fs.readFileSync(file, shared.utf8Encoding));
|
||||
lintContentWrapper(null, fs.readFileSync(file, helpers.utf8Encoding));
|
||||
} else {
|
||||
fs.readFile(file, shared.utf8Encoding, lintContentWrapper);
|
||||
fs.readFile(file, helpers.utf8Encoding, lintContentWrapper);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -466,7 +466,7 @@ function lintInput(options, synchronous, callback) {
|
|||
const stringsKeys = Object.keys(strings);
|
||||
const config = options.config || { "default": true };
|
||||
const frontMatter = (options.frontMatter === undefined) ?
|
||||
shared.frontMatterRe : options.frontMatter;
|
||||
helpers.frontMatterRe : options.frontMatter;
|
||||
const noInlineConfig = !!options.noInlineConfig;
|
||||
const resultVersion = (options.resultVersion === undefined) ?
|
||||
2 : options.resultVersion;
|
||||
|
|
@ -590,7 +590,7 @@ function readConfig(file, parsers, callback) {
|
|||
parsers = null;
|
||||
}
|
||||
// Read file
|
||||
fs.readFile(file, shared.utf8Encoding, (err, content) => {
|
||||
fs.readFile(file, helpers.utf8Encoding, (err, content) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
|
@ -608,7 +608,7 @@ function readConfig(file, parsers, callback) {
|
|||
if (errr) {
|
||||
return callback(errr);
|
||||
}
|
||||
return callback(null, shared.assign(extendsConfig, config));
|
||||
return callback(null, helpers.assign(extendsConfig, config));
|
||||
});
|
||||
}
|
||||
return callback(null, config);
|
||||
|
|
@ -624,7 +624,7 @@ function readConfig(file, parsers, callback) {
|
|||
*/
|
||||
function readConfigSync(file, parsers) {
|
||||
// Read file
|
||||
const content = fs.readFileSync(file, shared.utf8Encoding);
|
||||
const content = fs.readFileSync(file, helpers.utf8Encoding);
|
||||
// Try to parse file
|
||||
const { config, message } = parseConfiguration(file, content, parsers);
|
||||
if (!config) {
|
||||
|
|
@ -634,7 +634,7 @@ function readConfigSync(file, parsers) {
|
|||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
delete config.extends;
|
||||
return shared.assign(
|
||||
return helpers.assign(
|
||||
readConfigSync(path.resolve(path.dirname(file), configExtends), parsers),
|
||||
config);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue