diff --git a/README.md b/README.md index e10e0be7..cb160173 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ playground for learning and exploring. * **MD038** - Spaces inside code span elements * **MD039** - Spaces inside link text * **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. diff --git a/lib/markdownlint.js b/lib/markdownlint.js index ae7a6f56..3fac0b9a 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -53,8 +53,24 @@ 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); + } // Parse content into tokens and lines var tokens = md.parse(content, {}); var lines = content.split(shared.newLineRe); @@ -170,6 +186,18 @@ function markdownlint(options, callback) { 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(); diff --git a/test/ignore_frontmatter.json b/test/ignore_frontmatter.json new file mode 100644 index 00000000..6db88384 --- /dev/null +++ b/test/ignore_frontmatter.json @@ -0,0 +1,5 @@ +{ + "default": true, + "ignore": "frontmatter", + "MD012": false +} diff --git a/test/ignore_frontmatter.md b/test/ignore_frontmatter.md new file mode 100644 index 00000000..0b1bd05c --- /dev/null +++ b/test/ignore_frontmatter.md @@ -0,0 +1,7 @@ +--- +front: matter +--- + +# Header 1 # + +## Header 2 ##