mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Update MD041/first-line-h1 to ignore leading HTML comments (fixes #37).
This commit is contained in:
parent
15b37d51ff
commit
acd36d5802
8 changed files with 56 additions and 15 deletions
|
|
@ -1037,17 +1037,13 @@ Parameters: level (number; default 1)
|
|||
This rule is triggered when the first line in the file isn't a top level (h1)
|
||||
header:
|
||||
|
||||
```
|
||||
This is a file without a header
|
||||
```
|
||||
|
||||
To fix this, add a header to the top of your file:
|
||||
|
||||
```
|
||||
# File with header
|
||||
|
||||
This is a file with a top level header
|
||||
```
|
||||
|
||||
Note: The `level` parameter can be used to change the top level (ex: to h2) in
|
||||
cases where an h1 is added externally.
|
||||
|
|
|
|||
20
lib/rules.js
20
lib/rules.js
|
|
@ -1019,21 +1019,19 @@ module.exports = [
|
|||
"func": function MD041(params, errors) {
|
||||
var level = params.options.level || 1;
|
||||
var tag = "h" + level;
|
||||
var firstHeader = null;
|
||||
params.tokens.every(function forToken(token) {
|
||||
params.tokens.every(function forToken(token, index) {
|
||||
if (token.type === "heading_open") {
|
||||
firstHeader = token;
|
||||
return false;
|
||||
} else if (token.lineNumber > 1) {
|
||||
return false;
|
||||
if (!((token.lineNumber === 1) || (index > 0)) ||
|
||||
(token.tag !== tag)) {
|
||||
errors.addContext(token.lineNumber, token.line);
|
||||
}
|
||||
return false;
|
||||
} else if (token.type === "html_block") {
|
||||
return true;
|
||||
});
|
||||
if (!firstHeader ||
|
||||
(firstHeader.lineNumber !== 1) ||
|
||||
(firstHeader.tag !== tag)) {
|
||||
errors.addContext(1, params.lines[0]);
|
||||
}
|
||||
errors.addContext(token.lineNumber, token.line);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
|||
4
test/md041-ignore-leading-comments-combined.json
Normal file
4
test/md041-ignore-leading-comments-combined.json
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"default": true,
|
||||
"first-line-h1": true
|
||||
}
|
||||
11
test/md041-ignore-leading-comments-combined.md
Normal file
11
test/md041-ignore-leading-comments-combined.md
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Heading # <!-- markdownlint-disable no-hard-tabs -->
|
||||
|
||||
Text text text
|
||||
|
||||
Embedded tab
|
||||
|
||||
Text text text
|
||||
|
||||
Trailing space {MD009}
|
||||
|
||||
Text text text
|
||||
4
test/md041-ignore-leading-comments-violation.json
Normal file
4
test/md041-ignore-leading-comments-violation.json
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"default": true,
|
||||
"first-line-h1": true
|
||||
}
|
||||
11
test/md041-ignore-leading-comments-violation.md
Normal file
11
test/md041-ignore-leading-comments-violation.md
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<!-- markdownlint-disable no-hard-tabs -->
|
||||
|
||||
Text text text {MD041}
|
||||
|
||||
Embedded tab
|
||||
|
||||
Text text text
|
||||
|
||||
Trailing space {MD009}
|
||||
|
||||
Text text text
|
||||
4
test/md041-ignore-leading-comments.json
Normal file
4
test/md041-ignore-leading-comments.json
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"default": true,
|
||||
"first-line-h1": true
|
||||
}
|
||||
13
test/md041-ignore-leading-comments.md
Normal file
13
test/md041-ignore-leading-comments.md
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<!-- markdownlint-disable no-hard-tabs -->
|
||||
|
||||
# Heading
|
||||
|
||||
Text text text
|
||||
|
||||
Embedded tab
|
||||
|
||||
Text text text
|
||||
|
||||
Trailing space {MD009}
|
||||
|
||||
Text text text
|
||||
Loading…
Add table
Add a link
Reference in a new issue