mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Merge branch 'rodneyrehm-feature/ignore-frontmatter'
This commit is contained in:
commit
c2aff32460
9 changed files with 135 additions and 14 deletions
|
|
@ -141,7 +141,7 @@
|
||||||
"max-depth": [2, 4],
|
"max-depth": [2, 4],
|
||||||
"max-len": [2, 80, 2],
|
"max-len": [2, 80, 2],
|
||||||
"max-nested-callbacks": [2, 3],
|
"max-nested-callbacks": [2, 3],
|
||||||
"max-params": [2, 4],
|
"max-params": [2, 5],
|
||||||
"max-statements": [2, 20],
|
"max-statements": [2, 20],
|
||||||
"new-cap": 2,
|
"new-cap": 2,
|
||||||
"new-parens": 2,
|
"new-parens": 2,
|
||||||
|
|
|
||||||
28
README.md
28
README.md
|
|
@ -168,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
|
#### options.config
|
||||||
|
|
||||||
Type: `Object` mapping `String` to `Boolean | Object`
|
Type: `Object` mapping `String` to `Boolean | Object`
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,17 @@ function uniqueFilterForSorted(value, index, array) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lints a single string
|
// Lints a single string
|
||||||
function lintContent(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
|
// 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);
|
||||||
|
|
@ -122,7 +132,11 @@ function lintContent(content, config) {
|
||||||
// Record any errors
|
// Record any errors
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
errors.sort(numberComparison);
|
errors.sort(numberComparison);
|
||||||
result[rule.name] = errors.filter(uniqueFilterForSorted);
|
result[rule.name] = errors
|
||||||
|
.filter(uniqueFilterForSorted)
|
||||||
|
.map(function adjustLineNumbers(error) {
|
||||||
|
return error + frontMatterLines;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -130,12 +144,12 @@ function lintContent(content, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lints a single file
|
// Lints a single file
|
||||||
function lintFile(file, config, synchronous, callback) {
|
function lintFile(file, config, frontMatter, synchronous, callback) {
|
||||||
function lintContentWrapper(err, content) {
|
function lintContentWrapper(err, content) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
var result = lintContent(content, config);
|
var result = lintContent(content, config, frontMatter);
|
||||||
callback(null, result);
|
callback(null, result);
|
||||||
}
|
}
|
||||||
// Make a/synchronous call to read file
|
// Make a/synchronous call to read file
|
||||||
|
|
@ -167,6 +181,8 @@ function markdownlint(options, callback) {
|
||||||
callback = callback || function noop() {};
|
callback = callback || function noop() {};
|
||||||
var files = (options.files || []).slice();
|
var files = (options.files || []).slice();
|
||||||
var strings = options.strings || {};
|
var strings = options.strings || {};
|
||||||
|
var frontMatter = (options.frontMatter === undefined) ?
|
||||||
|
shared.frontMatterRe : options.frontMatter;
|
||||||
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();
|
||||||
|
|
@ -174,7 +190,8 @@ function markdownlint(options, callback) {
|
||||||
function lintFilesArray() {
|
function lintFilesArray() {
|
||||||
var file = files.shift();
|
var file = files.shift();
|
||||||
if (file) {
|
if (file) {
|
||||||
lintFile(file, config, synchronous, function lintedFile(err, result) {
|
lintFile(file, config, frontMatter, synchronous,
|
||||||
|
function lintedFile(err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
@ -188,7 +205,7 @@ function markdownlint(options, callback) {
|
||||||
}
|
}
|
||||||
// Lint strings
|
// Lint strings
|
||||||
Object.keys(strings).forEach(function forKey(key) {
|
Object.keys(strings).forEach(function forKey(key) {
|
||||||
var result = lintContent(strings[key] || "", config);
|
var result = lintContent(strings[key] || "", config, frontMatter);
|
||||||
results[key] = result;
|
results[key] = result;
|
||||||
});
|
});
|
||||||
// Lint files
|
// Lint files
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,8 @@
|
||||||
// Regular expression for matching common newline characters
|
// Regular expression for matching common newline characters
|
||||||
module.exports.newLineRe = /\r\n|\r|\n/;
|
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
|
// readFile options for reading with the UTF-8 encoding
|
||||||
module.exports.utf8Encoding = { "encoding": "utf8" };
|
module.exports.utf8Encoding = { "encoding": "utf8" };
|
||||||
|
|
|
||||||
9
test/front-matter-embedded.md
Normal file
9
test/front-matter-embedded.md
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
Text text text
|
||||||
|
|
||||||
|
---
|
||||||
|
layout: post
|
||||||
|
hard: tab {MD010}
|
||||||
|
title: embedded
|
||||||
|
---
|
||||||
|
|
||||||
|
Text text text
|
||||||
5
test/front-matter-empty.md
Normal file
5
test/front-matter-empty.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
---
|
||||||
|
# Header
|
||||||
|
|
||||||
|
# Another {MD025}
|
||||||
10
test/front-matter-with-dashes.md
Normal file
10
test/front-matter-with-dashes.md
Normal 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
7
test/front-matter.md
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
front: matter
|
||||||
|
---
|
||||||
|
|
||||||
|
# Header 1
|
||||||
|
|
||||||
|
## Header 2
|
||||||
|
|
@ -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) {
|
module.exports.filesArrayNotModified = function filesArrayNotModified(test) {
|
||||||
test.expect(2);
|
test.expect(2);
|
||||||
var files = [
|
var files = [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue