Automatically ignore common front matter; provide option for customization (fixes #2).

This commit is contained in:
David Anson 2015-07-25 22:18:30 -07:00
parent cae451967a
commit a467f8b3e6
11 changed files with 134 additions and 54 deletions

View file

@ -53,23 +53,17 @@ function uniqueFilterForSorted(value, index, array) {
return (index === 0) || (value > array[index - 1]);
}
function ignoreContent(content, config) {
if (typeof config.ignore === "function") {
// allow custom ignore callback
return config.ignore(content);
}
return content.replace(config.ignore, function removeNonLineBreaks(m) {
// maintain line breaks
return m.replace(/[^\n]+/g, "");
});
}
// Lints a single string
function lintContent(content, config) {
// ignore portions of content
if (config.ignore) {
content = ignoreContent(content, config);
function lintContent(content, config, frontMatter) {
// Remove front matter (if present at beginning of content)
var frontMatterLines = 0;
if (frontMatter) {
var frontMatterMatch = frontMatter.exec(content);
if (frontMatterMatch && !frontMatterMatch.index) {
var contentMatched = frontMatterMatch[0];
content = content.slice(contentMatched.length);
frontMatterLines = contentMatched.split(shared.newLineRe).length - 1;
}
}
// Parse content into tokens and lines
var tokens = md.parse(content, {});
@ -138,7 +132,11 @@ function lintContent(content, config) {
// Record any errors
if (errors.length) {
errors.sort(numberComparison);
result[rule.name] = errors.filter(uniqueFilterForSorted);
result[rule.name] = errors
.filter(uniqueFilterForSorted)
.map(function adjustLineNumbers(error) {
return error + frontMatterLines;
});
}
}
});
@ -146,12 +144,12 @@ function lintContent(content, config) {
}
// Lints a single file
function lintFile(file, config, synchronous, callback) {
function lintFile(file, config, frontMatter, synchronous, callback) {
function lintContentWrapper(err, content) {
if (err) {
return callback(err);
}
var result = lintContent(content, config);
var result = lintContent(content, config, frontMatter);
callback(null, result);
}
// Make a/synchronous call to read file
@ -183,40 +181,31 @@ function markdownlint(options, callback) {
callback = callback || function noop() {};
var files = (options.files || []).slice();
var strings = options.strings || {};
var frontMatter = (options.frontMatter === undefined) ?
shared.frontMatterRe : options.frontMatter;
var config = options.config || { "default": true };
var synchronous = (callback === markdownlintSynchronousCallback);
var results = new Results();
if (config.ignore) {
if (config.ignore === "frontmatter") {
// see http://jekyllrb.com/docs/frontmatter/
config.ignore = /^---([\s\S]+?)---/;
}
if (typeof config.ignore !== "function" &&
!config.ignore.test &&
!config.ignore.exec
) {
throw new TypeError("config.ignore must be a RegExp or Function");
}
}
// Helper to lint the next file in the array
function lintFilesArray() {
var file = files.shift();
if (file) {
lintFile(file, config, synchronous, function lintedFile(err, result) {
if (err) {
return callback(err);
}
// Record errors and lint next file
results[file] = result;
lintFilesArray();
});
lintFile(file, config, frontMatter, synchronous,
function lintedFile(err, result) {
if (err) {
return callback(err);
}
// Record errors and lint next file
results[file] = result;
lintFilesArray();
});
} else {
callback(null, results);
}
}
// Lint strings
Object.keys(strings).forEach(function forKey(key) {
var result = lintContent(strings[key] || "", config);
var result = lintContent(strings[key] || "", config, frontMatter);
results[key] = result;
});
// Lint files