mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
feature(config): adding config.ignore to replace metadata (e.g. frontmatter) before linting
This commit is contained in:
parent
a958a33860
commit
cae451967a
4 changed files with 41 additions and 0 deletions
|
@ -77,6 +77,7 @@ 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.
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
5
test/ignore_frontmatter.json
Normal file
5
test/ignore_frontmatter.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"default": true,
|
||||
"ignore": "frontmatter",
|
||||
"MD012": false
|
||||
}
|
7
test/ignore_frontmatter.md
Normal file
7
test/ignore_frontmatter.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
front: matter
|
||||
---
|
||||
|
||||
# Header 1 #
|
||||
|
||||
## Header 2 ##
|
Loading…
Add table
Add a link
Reference in a new issue