Skip to content

Commit

Permalink
Update MD018/no-missing-space-atx and MD020/no-missing-space-closed-a…
Browse files Browse the repository at this point in the history
…tx to ignore the content of HTML blocks (fixes DavidAnson#1268).
  • Loading branch information
fyc09 authored Jul 20, 2024
1 parent a80387a commit 6c8d0fd
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
16 changes: 12 additions & 4 deletions demo/markdownlint-browser.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 Down Expand Up @@ -394,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 @@ -407,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 block.
* @returns {void}
*/

Expand Down Expand Up @@ -4210,8 +4216,9 @@ module.exports = {
"tags": [ "headings", "atx", "spaces" ],
"parser": "none",
"function": function MD018(params, onError) {
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
forEachLine(lineMetadata(), (line, lineIndex, inCode, inFence, inTable, inItem, inBreak, inHtml) => {
if (!inCode &&
!inHtml &&
/^#+[^# \t]/.test(line) &&
!/#\s*$/.test(line) &&
!line.startsWith("#️⃣")) {
Expand Down Expand Up @@ -4353,8 +4360,9 @@ module.exports = {
"tags": [ "headings", "atx_closed", "spaces" ],
"parser": "none",
"function": function MD020(params, onError) {
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
if (!inCode) {
forEachLine(lineMetadata(), (line, lineIndex, inCode, inFence, inTable, inItem, inBreak, inHtml) => {
if (!inCode &&
!inHtml) {
const match =
/^(#+)([ \t]*)([^#]*?[^#\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
if (match) {
Expand Down
8 changes: 7 additions & 1 deletion helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,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 @@ -382,6 +382,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 @@ -395,6 +400,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 block.
* @returns {void}
*/

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

<style> {MD033}

##Heading 1

## Heading 2

##Heading 3##

## Heading 4 ##

</style>
39 changes: 39 additions & 0 deletions test/snapshots/markdownlint-test-scenarios.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -19683,6 +19683,45 @@ Generated by [AVA](https://avajs.dev).
`,
}

## ignore-html-block.md

> Snapshot 1

{
errors: [
{
errorContext: null,
errorDetail: 'Element: style',
errorRange: [
1,
7,
],
fixInfo: null,
lineNumber: 3,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
],
fixed: `# ignore-html-block.md␊
<style> {MD033}␊
##Heading 1␊
## Heading 2␊
##Heading 3##␊
## Heading 4 ##␊
</style>␊
`,
}

## inconsistent_bullet_indent_same_level.md

> Snapshot 1
Expand Down
Binary file modified test/snapshots/markdownlint-test-scenarios.js.snap
Binary file not shown.

0 comments on commit 6c8d0fd

Please sign in to comment.