Change comment replacement character from " " (with trailing "\") to "." to avoid creating indented code blocks.

This commit is contained in:
David Anson 2021-01-30 14:36:11 -08:00
parent c4e236b858
commit c7d2416f95
4 changed files with 45 additions and 36 deletions

View file

@ -112,9 +112,8 @@ module.exports.includesSorted = function includesSorted(array, element) {
} }
return false; return false;
}; };
// Replaces the text of all properly-formatted HTML comments with whitespace // Replaces the content of properly-formatted CommonMark comments with "."
// This preserves the line/column information for the rest of the document // This preserves the line/column information for the rest of the document
// Trailing whitespace is avoided with a '\' character in the last column
// https://spec.commonmark.org/0.29/#html-blocks // https://spec.commonmark.org/0.29/#html-blocks
// https://spec.commonmark.org/0.29/#html-comment // https://spec.commonmark.org/0.29/#html-comment
var htmlCommentBegin = "<!--"; var htmlCommentBegin = "<!--";
@ -147,12 +146,9 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
.search(inlineCommentRe); .search(inlineCommentRe);
// If not a markdownlint inline directive... // If not a markdownlint inline directive...
if (inlineCommentIndex === -1) { if (inlineCommentIndex === -1) {
var blanks = content
.replace(/[^\r\n]/g, " ")
.replace(/ ([\r\n])/g, "\\$1");
text = text =
text.slice(0, i + htmlCommentBegin.length) + text.slice(0, i + htmlCommentBegin.length) +
blanks + content.replace(/[^\r\n]/g, ".") +
text.slice(j); text.slice(j);
} }
} }

View file

@ -99,9 +99,8 @@ module.exports.includesSorted = function includesSorted(array, element) {
return false; return false;
}; };
// Replaces the text of all properly-formatted HTML comments with whitespace // Replaces the content of properly-formatted CommonMark comments with "."
// This preserves the line/column information for the rest of the document // This preserves the line/column information for the rest of the document
// Trailing whitespace is avoided with a '\' character in the last column
// https://spec.commonmark.org/0.29/#html-blocks // https://spec.commonmark.org/0.29/#html-blocks
// https://spec.commonmark.org/0.29/#html-comment // https://spec.commonmark.org/0.29/#html-comment
const htmlCommentBegin = "<!--"; const htmlCommentBegin = "<!--";
@ -134,12 +133,9 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
.search(inlineCommentRe); .search(inlineCommentRe);
// If not a markdownlint inline directive... // If not a markdownlint inline directive...
if (inlineCommentIndex === -1) { if (inlineCommentIndex === -1) {
const blanks = content
.replace(/[^\r\n]/g, " ")
.replace(/ ([\r\n])/g, "\\$1");
text = text =
text.slice(0, i + htmlCommentBegin.length) + text.slice(0, i + htmlCommentBegin.length) +
blanks + content.replace(/[^\r\n]/g, ".") +
text.slice(j); text.slice(j);
} }
} }

View file

@ -0,0 +1,17 @@
# HTML Comment in Markdown Table
```xml
<!-- comment -->
```
| Table |
|-------|
| <!-- |
|comment|
| --> |
| Table |
|-------|
| <!-- \
\
--> |

View file

@ -56,17 +56,17 @@ test("clearHtmlCommentTextValid", (t) => {
"<!-->", "<!-->",
"<!--->", "<!--->",
"<!---->", "<!---->",
"<!-- -->", "<!--.........-->",
" <!-- -->", " <!--.........-->",
" <!-- -->", " <!--.........-->",
"<!-- -->", "<!--......-->",
"<!-- -->", "<!--....-->",
"<!-- -->", "<!--.-->",
"<!-- -->", "<!--....-->",
"<!---->", "<!---->",
"<!-- -->", "<!--.....-->",
"<!-- -->", "<!--.........-->",
"<!-- -->", "<!--..-->",
"<!--", "<!--",
"-->", "-->",
"<!--", "<!--",
@ -78,21 +78,21 @@ test("clearHtmlCommentTextValid", (t) => {
"-->", "-->",
"<!--", "<!--",
"", "",
" \\", "......",
"", "",
"-->", "-->",
"<!-- \\", "<!--....",
"", "",
" -->", "....-->",
"text<!-- -->text", "text<!--....-->text",
"text<!--", "text<!--",
"-->text", "-->text",
"text<!--", "text<!--",
" \\", "....",
"-->text", "-->text",
"<!-- --><!-- -->", "<!--....--><!--....-->",
"text<!-- -->text<!-- -->text", "text<!--....-->text<!--....-->text",
"text<!-- -->text", "text<!--..............-->text",
"<!--", "<!--",
"text" "text"
]; ];
@ -133,9 +133,9 @@ test("clearHtmlCommentTextNonGreedy", (t) => {
"<!----> -->" "<!----> -->"
]; ];
const nonGreedyResult = [ const nonGreedyResult = [
"<!-- --> -->", "<!--......--> -->",
"<!-- --> -->", "<!--......--> -->",
"<!-- --> -->", "<!--.--> -->",
"<!----> -->" "<!----> -->"
]; ];
const actual = helpers.clearHtmlCommentText(nonGreedyComments.join("\n")); const actual = helpers.clearHtmlCommentText(nonGreedyComments.join("\n"));
@ -153,11 +153,11 @@ test("clearHtmlCommentTextEmbedded", (t) => {
"text<!--text-->text" "text<!--text-->text"
]; ];
const embeddedResult = [ const embeddedResult = [
"text<!-- -->text", "text<!--....-->text",
"<!-- markdownlint-disable MD010 -->", "<!-- markdownlint-disable MD010 -->",
"text<!-- -->text", "text<!--....-->text",
"text<!-- markdownlint-disable MD010 -->text", "text<!-- markdownlint-disable MD010 -->text",
"text<!-- -->text" "text<!--....-->text"
]; ];
const actual = helpers.clearHtmlCommentText(embeddedComments.join("\n")); const actual = helpers.clearHtmlCommentText(embeddedComments.join("\n"));
const expected = embeddedResult.join("\n"); const expected = embeddedResult.join("\n");