diff --git a/doc/Rules.md b/doc/Rules.md index c5dbd4ce..57e952c7 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -341,6 +341,9 @@ Fixable: Most violations can be fixed by tooling This rule is triggered on any lines that end with unexpected whitespace. To fix this, remove the trailing space from the end of the line. +Note: Trailing space is allowed in indented and fenced code blocks because some +languages require it. + The `br_spaces` parameter allows an exception to this rule for a specific number of trailing spaces, typically used to insert an explicit line break. The default value allows 2 spaces to indicate a hard break (\
element). diff --git a/lib/md009.js b/lib/md009.js index 59c2b572..163d24d8 100644 --- a/lib/md009.js +++ b/lib/md009.js @@ -47,12 +47,10 @@ module.exports = { codeInlineLineNumbers.sort(numericSortAscending); } const expected = (brSpaces < 2) ? 0 : brSpaces; - let inFencedCode = 0; - forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => { - inFencedCode += onFence; + forEachLine(lineMetadata(), (line, lineIndex, inCode) => { const lineNumber = lineIndex + 1; const trailingSpaces = line.length - line.trimRight().length; - if ((!inCode || inFencedCode) && trailingSpaces && + if (trailingSpaces && !inCode && !includesSorted(listItemLineNumbers, lineNumber)) { if ((expected !== trailingSpaces) || (strict && diff --git a/test/code-block-trailing-spaces.md b/test/code-block-trailing-spaces.md new file mode 100644 index 00000000..65e73bca --- /dev/null +++ b/test/code-block-trailing-spaces.md @@ -0,0 +1,40 @@ +# code-block-trailing-spaces + +Text + +Two trailing spaces {MD009} + +Text + + Indented code block + + Statement + Indented statement + + Statement + Indented statement + + Two trailing spaces + +Text + +```text +Fenced code block + + Statement + Indented statement + + Statement + Indented statement + + Two trailing spaces +``` + +Text + + diff --git a/test/code_block_fenced.md b/test/code_block_fenced.md index f29e10d2..f8ee2c92 100644 --- a/test/code_block_fenced.md +++ b/test/code_block_fenced.md @@ -11,12 +11,6 @@ And here is more text This is a code block that won't trigger. ``` -```text -This is code - -with a line with trailing spaces. {MD009:16} -``` - But we'll do another: And this {MD046}