mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Ignore the content of (valid) HTML comments when linting (fixes #64).
This commit is contained in:
parent
8c34383f80
commit
0a678d25c1
6 changed files with 240 additions and 2 deletions
|
@ -7,8 +7,9 @@ module.exports.newLineRe = /\r\n|\r|\n/;
|
|||
module.exports.frontMatterRe = /^(---|\+\+\+)$[^]*?^\1$(\r\n|\r|\n)/m;
|
||||
|
||||
// Regular expression for matching inline disable/enable comments
|
||||
module.exports.inlineCommentRe =
|
||||
var inlineCommentRe =
|
||||
/<!--\s*markdownlint-(dis|en)able((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
|
||||
module.exports.inlineCommentRe = inlineCommentRe;
|
||||
|
||||
// readFile options for reading with the UTF-8 encoding
|
||||
module.exports.utf8Encoding = { "encoding": "utf8" };
|
||||
|
@ -26,3 +27,36 @@ module.exports.assign = assign;
|
|||
module.exports.clone = function clone(obj) {
|
||||
return assign({}, obj);
|
||||
};
|
||||
|
||||
// Replaces the text of all properly-formatted HTML comments with whitespace
|
||||
// This preserves the line/column information for the rest of the document
|
||||
// Trailing whitespace is avoided with a '\' character in the last column
|
||||
// See https://www.w3.org/TR/html5/syntax.html#comments for details
|
||||
var htmlCommentBegin = "<!--";
|
||||
var htmlCommentEnd = "-->";
|
||||
function clearHtmlCommentText(text) {
|
||||
var i = 0;
|
||||
while ((i = text.indexOf(htmlCommentBegin, i)) !== -1) {
|
||||
var j = text.indexOf(htmlCommentEnd, i);
|
||||
if (j === -1) {
|
||||
j = text.length;
|
||||
text += "\\";
|
||||
}
|
||||
var comment = text.slice(i + htmlCommentBegin.length, j);
|
||||
if ((comment.length > 0) &&
|
||||
(comment[0] !== ">") &&
|
||||
(comment[comment.length - 1] !== "-") &&
|
||||
(comment.indexOf("--") === -1) &&
|
||||
(text.slice(i, j + htmlCommentEnd.length)
|
||||
.search(inlineCommentRe) === -1)) {
|
||||
var blanks = comment
|
||||
.replace(/[^\r\n]/g, " ")
|
||||
.replace(/ ([\r\n])/g, "\\$1");
|
||||
text = text.slice(0, i + htmlCommentBegin.length) +
|
||||
blanks + text.slice(j);
|
||||
}
|
||||
i = j + htmlCommentEnd.length;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
module.exports.clearHtmlCommentText = clearHtmlCommentText;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue