diff --git a/lib/md031.js b/lib/md031.js index 34c052cd..5cfcfb4f 100644 --- a/lib/md031.js +++ b/lib/md031.js @@ -5,6 +5,8 @@ const { addErrorContext, forEachLine, isBlankLine } = require("../helpers"); const { lineMetadata } = require("./cache"); +const codeFencePrefixRe = /^(.*?)\s*[`~]/; + module.exports = { "names": [ "MD031", "blanks-around-fences" ], "description": "Fenced code blocks should be surrounded by blank lines", @@ -19,6 +21,7 @@ module.exports = { if ((includeListItems || !inItem) && ((onTopFence && !isBlankLine(lines[i - 1])) || (onBottomFence && !isBlankLine(lines[i + 1])))) { + const [ , prefix ] = line.match(codeFencePrefixRe); addErrorContext( onError, i + 1, @@ -28,7 +31,7 @@ module.exports = { null, { "lineNumber": i + (onTopFence ? 1 : 2), - "insertText": "\n" + "insertText": `${prefix}\n` }); } }); diff --git a/test/detailed-results-code-fences-in-blockquotes.md b/test/detailed-results-code-fences-in-blockquotes.md new file mode 100644 index 00000000..fd8f273b --- /dev/null +++ b/test/detailed-results-code-fences-in-blockquotes.md @@ -0,0 +1,18 @@ +# Detailed Results Code Fences in Blockquotes + +Text +```markdown +Text +``` +Text + +> Text +> ```markdown +> Text +> ``` +> Text +> > Text +> > ```markdown +> > Text +> > ``` +> > Text diff --git a/test/detailed-results-code-fences-in-blockquotes.md.fixed b/test/detailed-results-code-fences-in-blockquotes.md.fixed new file mode 100644 index 00000000..277647c4 --- /dev/null +++ b/test/detailed-results-code-fences-in-blockquotes.md.fixed @@ -0,0 +1,24 @@ +# Detailed Results Code Fences in Blockquotes + +Text + +```markdown +Text +``` + +Text + +> Text +> +> ```markdown +> Text +> ``` +> +> Text +> > Text +> > +> > ```markdown +> > Text +> > ``` +> > +> > Text diff --git a/test/detailed-results-code-fences-in-blockquotes.results.json b/test/detailed-results-code-fences-in-blockquotes.results.json new file mode 100644 index 00000000..b8011391 --- /dev/null +++ b/test/detailed-results-code-fences-in-blockquotes.results.json @@ -0,0 +1,74 @@ +[ + { + "lineNumber": 4, + "ruleNames": [ + "MD031", + "blanks-around-fences" + ], + "ruleDescription": "Fenced code blocks should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md031", + "errorDetail": null, + "errorContext": "```markdown", + "errorRange": null + }, + { + "lineNumber": 6, + "ruleNames": [ + "MD031", + "blanks-around-fences" + ], + "ruleDescription": "Fenced code blocks should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md031", + "errorDetail": null, + "errorContext": "```", + "errorRange": null + }, + { + "lineNumber": 10, + "ruleNames": [ + "MD031", + "blanks-around-fences" + ], + "ruleDescription": "Fenced code blocks should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md031", + "errorDetail": null, + "errorContext": "> ```markdown", + "errorRange": null + }, + { + "lineNumber": 12, + "ruleNames": [ + "MD031", + "blanks-around-fences" + ], + "ruleDescription": "Fenced code blocks should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md031", + "errorDetail": null, + "errorContext": "> ```", + "errorRange": null + }, + { + "lineNumber": 15, + "ruleNames": [ + "MD031", + "blanks-around-fences" + ], + "ruleDescription": "Fenced code blocks should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md031", + "errorDetail": null, + "errorContext": "> > ```markdown", + "errorRange": null + }, + { + "lineNumber": 17, + "ruleNames": [ + "MD031", + "blanks-around-fences" + ], + "ruleDescription": "Fenced code blocks should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md031", + "errorDetail": null, + "errorContext": "> > ```", + "errorRange": null + } +]