Skip to content

Commit

Permalink
Ignore HTML blocks in MD018
Browse files Browse the repository at this point in the history
  • Loading branch information
fyc09 committed Jul 17, 2024
1 parent def6225 commit 3b80d75
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
12 changes: 9 additions & 3 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ module.exports.filterTokens = filterTokens;
*/
function getLineMetadata(params) {
const lineMetadata = params.lines.map(
(line, index) => [ line, index, false, 0, false, false, false ]
(line, index) => [ line, index, false, 0, false, false, false, false ]
);
filterTokens(params, "fence", (token) => {
lineMetadata[token.map[0]][3] = 1;
Expand Down Expand Up @@ -406,6 +406,11 @@ function getLineMetadata(params) {
filterTokens(params, "hr", (token) => {
lineMetadata[token.map[0]][6] = true;
});
filterTokens(params, "html_block", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][7] = true;
}
});
return lineMetadata;
}
module.exports.getLineMetadata = getLineMetadata;
Expand All @@ -419,6 +424,7 @@ module.exports.getLineMetadata = getLineMetadata;
* @param {boolean} inTable Iff in a table.
* @param {boolean} inItem Iff in a list item.
* @param {boolean} inBreak Iff in semantic break.
* @param {boolean} inHTML Iff in HTML.
* @returns {void}
*/

Expand Down Expand Up @@ -4100,8 +4106,8 @@ module.exports = {
"tags": [ "headings", "atx", "spaces" ],
"parser": "none",
"function": function MD018(params, onError) {
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
if (!inCode &&
forEachLine(lineMetadata(), (line, lineIndex, inCode, inFence, inTable, inItem, inBreak, inHTML) => {
if (!inCode && !inHTML &&
/^#+[^# \t]/.test(line) &&
!/#\s*$/.test(line) &&
!line.startsWith("#️⃣")) {
Expand Down
13 changes: 7 additions & 6 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ module.exports.filterTokens = filterTokens;
*/
function getLineMetadata(params) {
const lineMetadata = params.lines.map(
(line, index) => [ line, index, false, 0, false, false, false ]
(line, index) => [ line, index, false, 0, false, false, false, false ]
);
filterTokens(params, "fence", (token) => {
lineMetadata[token.map[0]][3] = 1;
Expand All @@ -379,11 +379,6 @@ function getLineMetadata(params) {
lineMetadata[i][2] = true;
}
});
filterTokens(params, "html_block", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][2] = true;
}
});
filterTokens(params, "table_open", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][4] = true;
Expand All @@ -399,6 +394,11 @@ function getLineMetadata(params) {
filterTokens(params, "hr", (token) => {
lineMetadata[token.map[0]][6] = true;
});
filterTokens(params, "html_block", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][7] = true;
}
});
return lineMetadata;
}
module.exports.getLineMetadata = getLineMetadata;
Expand All @@ -412,6 +412,7 @@ module.exports.getLineMetadata = getLineMetadata;
* @param {boolean} inTable Iff in a table.
* @param {boolean} inItem Iff in a list item.
* @param {boolean} inBreak Iff in semantic break.
* @param {boolean} inHTML Iff in HTML.
* @returns {void}
*/

Expand Down
4 changes: 2 additions & 2 deletions lib/md018.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module.exports = {
"tags": [ "headings", "atx", "spaces" ],
"parser": "none",
"function": function MD018(params, onError) {
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
if (!inCode &&
forEachLine(lineMetadata(), (line, lineIndex, inCode, inFence, inTable, inItem, inBreak, inHTML) => {
if (!inCode && !inHTML &&
/^#+[^# \t]/.test(line) &&
!/#\s*$/.test(line) &&
!line.startsWith("#️⃣")) {
Expand Down
9 changes: 9 additions & 0 deletions test/ignore-html-block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ignore-html-block.md

## Title

##Title2 {MD018}

<style> {MD033}
#foo {}
</style>
Binary file modified test/snapshots/markdownlint-test-scenarios.js.snap
Binary file not shown.

0 comments on commit 3b80d75

Please sign in to comment.