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

@ -141,7 +141,7 @@
"max-depth": [2, 4],
"max-len": [2, 80, 2],
"max-nested-callbacks": [2, 3],
"max-params": [2, 4],
"max-params": [2, 5],
"max-statements": [2, 20],
"new-cap": 2,
"new-parens": 2,

View file

@ -77,7 +77,6 @@ playground for learning and exploring.
* **MD039** - Spaces inside link text
* **MD040** - Fenced code blocks should have a language specified
* **MD041** - First line in file should be a top level header
* **ignore** - Ignore certain parts of the markdown file (can be `RegExp`, `Function`, `"frontmatter"`), should be used with disabled `MD012`
See [Rules.md](doc/Rules.md) for more details.
@ -169,6 +168,34 @@ Example:
}
```
#### options.frontMatter
Type: `RegExp`
Matches any [front matter](http://jekyllrb.com/docs/frontmatter/) found at the
beginning of a file.
Some Markdown content begins with metadata; the default `RegExp` for this option
ignores common forms of "front matter". To match differently, specify a custom
`RegExp` or use the value `null` to disable the feature.
Note: Matches must occur at the start of the file.
Default:
```js
/^---$[^]*?^---$(\r\n|\r|\n)/m
```
Ignores:
```text
---
layout: post
title: Title
---
```
#### options.config
Type: `Object` mapping `String` to `Boolean | Object`

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

View file

@ -3,5 +3,8 @@
// Regular expression for matching common newline characters
module.exports.newLineRe = /\r\n|\r|\n/;
// Regular expression for matching common front matter
module.exports.frontMatterRe = /^---$[^]*?^---$(\r\n|\r|\n)/m;
// readFile options for reading with the UTF-8 encoding
module.exports.utf8Encoding = { "encoding": "utf8" };

View file

@ -0,0 +1,9 @@
Text text text
---
layout: post
hard: tab {MD010}
title: embedded
---
Text text text

View file

@ -0,0 +1,5 @@
---
---
# Header
# Another {MD025}

View file

@ -0,0 +1,10 @@
---
layout: post
title: Title with ---
tags: front matter
---
## Header {MD002}
---
Hard tab {MD010}

7
test/front-matter.md Normal file
View file

@ -0,0 +1,7 @@
---
front: matter
---
# Header 1
## Header 2

View file

@ -1,5 +0,0 @@
{
"default": true,
"ignore": "frontmatter",
"MD012": false
}

View file

@ -1,7 +0,0 @@
---
front: matter
---
# Header 1 #
## Header 2 ##

View file

@ -505,6 +505,48 @@ module.exports.styleRelaxed = function styleRelaxed(test) {
});
};
module.exports.nullFrontMatter = function nullFrontMatter(test) {
test.expect(2);
markdownlint({
"strings": {
"content": "---\n\t\n---\n# Header\n"
},
"frontMatter": null,
"config": {
"default": false,
"MD010": true
}
}, function callback(err, result) {
test.ifError(err);
var expectedResult = {
"content": { "MD010": [ 2 ] }
};
test.deepEqual(result, expectedResult, "Undetected issues.");
test.done();
});
};
module.exports.customFrontMatter = function customFrontMatter(test) {
test.expect(2);
markdownlint({
"strings": {
"content": "<head>\n\t\n</head>\n# Header\n"
},
"frontMatter": /<head>[^]*<\/head>/,
"config": {
"default": false,
"MD010": true
}
}, function callback(err, result) {
test.ifError(err);
var expectedResult = {
"content": {}
};
test.deepEqual(result, expectedResult, "Did not get empty results.");
test.done();
});
};
module.exports.filesArrayNotModified = function filesArrayNotModified(test) {
test.expect(2);
var files = [