feature(config): adding config.ignore to replace metadata (e.g. frontmatter) before linting

This commit is contained in:
Rodney Rehm 2015-07-20 14:16:52 +02:00
parent eabe2387bc
commit 414e774480
4 changed files with 41 additions and 0 deletions

View file

@ -76,6 +76,7 @@ playground for learning and exploring.
* **MD038** - Spaces inside code span elements * **MD038** - Spaces inside code span elements
* **MD039** - Spaces inside link text * **MD039** - Spaces inside link text
* **MD040** - Fenced code blocks should have a language specified * **MD040** - Fenced code blocks should have a language specified
* **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. See [Rules.md](doc/Rules.md) for more details.

View file

@ -53,8 +53,24 @@ function uniqueFilterForSorted(value, index, array) {
return (index === 0) || (value > array[index - 1]); 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 // Lints a single string
function lintContent(content, config) { function lintContent(content, config) {
// ignore portions of content
if (config.ignore) {
content = ignoreContent(content, config);
}
// Parse content into tokens and lines // Parse content into tokens and lines
var tokens = md.parse(content, {}); var tokens = md.parse(content, {});
var lines = content.split(shared.newLineRe); var lines = content.split(shared.newLineRe);
@ -170,6 +186,18 @@ function markdownlint(options, callback) {
var config = options.config || { "default": true }; var config = options.config || { "default": true };
var synchronous = (callback === markdownlintSynchronousCallback); var synchronous = (callback === markdownlintSynchronousCallback);
var results = new Results(); 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 // Helper to lint the next file in the array
function lintFilesArray() { function lintFilesArray() {
var file = files.shift(); var file = files.shift();

View file

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

View file

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