From c3e8eab87b51d2e2786ec6c00126bd307d5f754d Mon Sep 17 00:00:00 2001 From: David Anson Date: Sun, 28 Jul 2019 16:48:32 -0700 Subject: [PATCH] Update MD038/no-space-in-code to ignore backticks in link destinations (fixes #206). --- .eslintrc.json | 3 ++- helpers/helpers.js | 17 ++++++++++++++- test/spaces_inside_codespan_elements.md | 28 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 4f33347f..c63e314e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -12,6 +12,7 @@ "array-bracket-spacing": ["error", "always"], "array-element-newline": "off", "capitalized-comments": "off", + "complexity": "off", "dot-location": ["error", "property"], "func-style": ["error", "declaration"], "function-paren-newline": "off", @@ -22,7 +23,7 @@ "max-lines": "off", "max-lines-per-function": "off", "max-params": ["error", 10], - "max-statements": ["error", 33], + "max-statements": "off", "multiline-comment-style": ["error", "separate-lines"], "multiline-ternary": "off", "newline-per-chained-call": "off", diff --git a/helpers/helpers.js b/helpers/helpers.js index a8f1ae60..288972f5 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -259,10 +259,25 @@ module.exports.forEachInlineCodeSpan = let startColumn = -1; let tickCount = 0; let currentTicks = 0; + let state = "normal"; // Deliberate <= so trailing 0 completes the last span (ex: "text `code`") for (; index <= input.length; index++) { const char = input[index]; - if (char === "`") { + // Ignore backticks in link destination + if ((char === "[") && (state === "normal")) { + state = "linkTextOpen"; + } else if ((char === "]") && (state === "linkTextOpen")) { + state = "linkTextClosed"; + } else if ((char === "(") && (state === "linkTextClosed")) { + state = "linkDestinationOpen"; + } else if ( + ((char === "(") && (state === "linkDestinationOpen")) || + ((char === ")") && (state === "linkDestinationOpen")) || + (state === "linkTextClosed")) { + state = "normal"; + } + // Parse backtick open/close + if ((char === "`") && (state !== "linkDestinationOpen")) { // Count backticks at start or end of code span currentTicks++; if ((startIndex === -1) || (startColumn === -1)) { diff --git a/test/spaces_inside_codespan_elements.md b/test/spaces_inside_codespan_elements.md index f90e12dd..2412a9d9 100644 --- a/test/spaces_inside_codespan_elements.md +++ b/test/spaces_inside_codespan_elements.md @@ -67,3 +67,31 @@ text and ``\`code with ignored escaped \` backticks`` ``multiple trailing spaces not allowed ` `` text `code` {MD038} `` ` leading and trailing space allowed for backtick ` `` text `code` + +Text [link](https://example.com/link`link) text `code`. + +Text [link](https://example.com/link```link) text ```code```. + +Text [link](https://example.com/link`link`link`link) text `code`. + +Text [link](https://example.com/link "title`title") text `code`. + +Text [link](#link`link) text `code`. + +Text [link] (#link`link) text `code`. {MD038} + +Text [link[link](#link`link) text `code`. + +Text [link(link](#link`link) text `code`. + +Text [link)link](#link`link) text `code`. + +Text [link](#link[link`link) text `code`. + +Text [link](#link]link`link) text `code`. + +Text [link](#link(link`link) text `code`. {MD038} + +Text [`link`](xref:custom.link`1) text `code`. + +Text ``code [link](#link`link) code`` text `code`.