Skip to content

Commit

Permalink
Update definition of helpers.isBlankLine to treat unterminated start/…
Browse files Browse the repository at this point in the history
…end comments as potentially blank lines (fixes #431).
  • Loading branch information
DavidAnson committed Dec 20, 2021
1 parent 1b23976 commit 6dea678
Show file tree
Hide file tree
Showing 4 changed files with 67 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 @@ -77,12 +77,18 @@ module.exports.isObject = function isObject(obj) {
return (obj !== null) && (typeof obj === "object") && !Array.isArray(obj);
};
// Returns true iff the input line is blank (no content)
// Example: Contains nothing, whitespace, or comments
var blankLineRe = />|(?:<!--.*?-->)/g;
// Example: Contains nothing, whitespace, or comment (unclosed start/end okay)
module.exports.isBlankLine = function isBlankLine(line) {
// Call to String.replace follows best practices and is not a security check
// False-positive for js/incomplete-multi-character-sanitization
return !line || !line.trim() || !line.replace(blankLineRe, "").trim();
return (!line ||
!line.trim() ||
!line
.replace(/<!--.*?-->/g, "")
.replace(/<!--.*$/g, "")
.replace(/^.*-->/g, "")
.replace(/>/g, "")
.trim());
};
/**
* Compare function for Array.prototype.sort for ascending order of numbers.
Expand Down
14 changes: 11 additions & 3 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ module.exports.isObject = function isObject(obj) {
};

// Returns true iff the input line is blank (no content)
// Example: Contains nothing, whitespace, or comments
const blankLineRe = />|(?:<!--.*?-->)/g;
// Example: Contains nothing, whitespace, or comment (unclosed start/end okay)
module.exports.isBlankLine = function isBlankLine(line) {
// Call to String.replace follows best practices and is not a security check
// False-positive for js/incomplete-multi-character-sanitization
return !line || !line.trim() || !line.replace(blankLineRe, "").trim();
return (
!line ||
!line.trim() ||
!line
.replace(/<!--.*?-->/g, "")
.replace(/<!--.*$/g, "")
.replace(/^.*-->/g, "")
.replace(/>/g, "")
.trim()
);
};

/**
Expand Down
38 changes: 38 additions & 0 deletions test/lists-with-commented-items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Lists with Commented Items

Text

- item <!-- comment -->
- item <!-- comment -->
<!--
- commented subitem: description
- commented subitem: description
-->
- item <!-- comment -->
- item <!-- comment -->

Text

- item <!-- comment -->
- item <!-- comment -->
<!-- - commented subitem: description
- commented subitem: description -->
- item <!-- comment -->
- item <!-- comment -->

Text

- item <!-- comment -->
<!-- - commented subitem: description -->
- item <!-- comment -->

Text

- item <!-- comment -->
- item <!-- comment -->
<!-- - commented subitem: description -->
<!-- - commented subitem: description -->
- item <!-- comment -->
- item <!-- comment -->

Text
14 changes: 9 additions & 5 deletions test/markdownlint-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ bar`
});

test("isBlankLine", (t) => {
t.plan(25);
t.plan(29);
const blankLines = [
null,
"",
Expand All @@ -244,7 +244,11 @@ test("isBlankLine", (t) => {
"> ",
"> > > \t",
"> <!--text-->",
">><!--text-->"
">><!--text-->",
"<!--",
" <!-- text",
"text --> ",
"-->"
];
blankLines.forEach((line) => t.true(helpers.isBlankLine(line), line || ""));
const nonBlankLines = [
Expand All @@ -253,9 +257,9 @@ test("isBlankLine", (t) => {
".",
"> .",
"<!--text--> text",
"<!--->",
"<!--",
"-->"
"text <!--text-->",
"text <!--",
"--> text"
];
nonBlankLines.forEach((line) => t.true(!helpers.isBlankLine(line), line));
});
Expand Down

0 comments on commit 6dea678

Please sign in to comment.