mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-21 21:30:47 +02:00
Update MD041/first-line-heading to recognize HTML headings (fixes #366).
This commit is contained in:
parent
c7d2416f95
commit
1d042aa3fd
7 changed files with 69 additions and 4 deletions
|
@ -3558,11 +3558,24 @@ module.exports = {
|
||||||
var tag = "h" + level;
|
var tag = "h" + level;
|
||||||
var foundFrontMatterTitle = frontMatterHasTitle(params.frontMatterLines, params.config.front_matter_title);
|
var foundFrontMatterTitle = frontMatterHasTitle(params.frontMatterLines, params.config.front_matter_title);
|
||||||
if (!foundFrontMatterTitle) {
|
if (!foundFrontMatterTitle) {
|
||||||
|
var htmlHeadingRe_1 = new RegExp("^<h" + level + "[ />]", "i");
|
||||||
params.tokens.every(function (token) {
|
params.tokens.every(function (token) {
|
||||||
|
var isError = false;
|
||||||
if (token.type === "html_block") {
|
if (token.type === "html_block") {
|
||||||
return true;
|
if (token.content.startsWith("<!--")) {
|
||||||
|
// Ignore leading HTML comments
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (!htmlHeadingRe_1.test(token.content)) {
|
||||||
|
// Something other than an HTML heading
|
||||||
|
isError = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ((token.type !== "heading_open") || (token.tag !== tag)) {
|
else if ((token.type !== "heading_open") || (token.tag !== tag)) {
|
||||||
|
// Something other than a Markdown heading
|
||||||
|
isError = true;
|
||||||
|
}
|
||||||
|
if (isError) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line);
|
addErrorContext(onError, token.lineNumber, token.line);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
10
doc/Rules.md
10
doc/Rules.md
|
@ -1588,6 +1588,16 @@ To fix this, add a top-level heading to the beginning of the file:
|
||||||
This is a file with a top-level heading
|
This is a file with a top-level heading
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Because it is common for projects on GitHub to use an image for the heading of
|
||||||
|
`README.md` and that is not well-supported by Markdown, HTML headings are also
|
||||||
|
permitted by this rule. For example:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
<h1 align="center"><img src="https://placekitten.com/300/150"/></h1>
|
||||||
|
|
||||||
|
This is a file with a top-level HTML heading
|
||||||
|
```
|
||||||
|
|
||||||
Note: The `level` parameter can be used to change the top-level (ex: to h2) in cases
|
Note: The `level` parameter can be used to change the top-level (ex: to h2) in cases
|
||||||
where an h1 is added externally.
|
where an h1 is added externally.
|
||||||
|
|
||||||
|
|
15
lib/md041.js
15
lib/md041.js
|
@ -17,11 +17,22 @@ module.exports = {
|
||||||
params.config.front_matter_title
|
params.config.front_matter_title
|
||||||
);
|
);
|
||||||
if (!foundFrontMatterTitle) {
|
if (!foundFrontMatterTitle) {
|
||||||
|
const htmlHeadingRe = new RegExp(`^<h${level}[ />]`, "i");
|
||||||
params.tokens.every((token) => {
|
params.tokens.every((token) => {
|
||||||
|
let isError = false;
|
||||||
if (token.type === "html_block") {
|
if (token.type === "html_block") {
|
||||||
return true;
|
if (token.content.startsWith("<!--")) {
|
||||||
|
// Ignore leading HTML comments
|
||||||
|
return true;
|
||||||
|
} else if (!htmlHeadingRe.test(token.content)) {
|
||||||
|
// Something other than an HTML heading
|
||||||
|
isError = true;
|
||||||
|
}
|
||||||
|
} else if ((token.type !== "heading_open") || (token.tag !== tag)) {
|
||||||
|
// Something other than a Markdown heading
|
||||||
|
isError = true;
|
||||||
}
|
}
|
||||||
if ((token.type !== "heading_open") || (token.tag !== tag)) {
|
if (isError) {
|
||||||
addErrorContext(onError, token.lineNumber, token.line);
|
addErrorContext(onError, token.lineNumber, token.line);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
7
test/h1-as-top-level-heading.md
Normal file
7
test/h1-as-top-level-heading.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<h1>H1 as Top-Level Heading</h1>
|
||||||
|
|
||||||
|
Text
|
||||||
|
|
||||||
|
<!-- markdownlint-configure-file {
|
||||||
|
"no-inline-html": false
|
||||||
|
} -->
|
7
test/h1-image-as-top-level-heading.md
Normal file
7
test/h1-image-as-top-level-heading.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<h1 align="center"><img src="https://placekitten.com/300/150"/></h1>
|
||||||
|
|
||||||
|
Text
|
||||||
|
|
||||||
|
<!-- markdownlint-configure-file {
|
||||||
|
"no-inline-html": false
|
||||||
|
} -->
|
10
test/h2-as-top-level-heading.md
Normal file
10
test/h2-as-top-level-heading.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<H2>H2 as Top-Level Heading</H2>
|
||||||
|
|
||||||
|
Text
|
||||||
|
|
||||||
|
<!-- markdownlint-configure-file {
|
||||||
|
"first-line-heading": {
|
||||||
|
"level": 2
|
||||||
|
},
|
||||||
|
"no-inline-html": false
|
||||||
|
} -->
|
7
test/h3-as-top-level-heading.md
Normal file
7
test/h3-as-top-level-heading.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<h3>H3 as Top-Level Heading {MD041}</h3>
|
||||||
|
|
||||||
|
Text
|
||||||
|
|
||||||
|
<!-- markdownlint-configure-file {
|
||||||
|
"no-inline-html": false
|
||||||
|
} -->
|
Loading…
Add table
Add a link
Reference in a new issue