From 67d1476ed82cfd0b0db915d1e8996bbd4dff2ab5 Mon Sep 17 00:00:00 2001 From: David Anson Date: Sat, 7 Mar 2020 19:49:57 -0800 Subject: [PATCH] Update MD039/no-space-in-links to handle RegExp match failure (fixes #262). --- lib/md039.js | 24 +++++++++++++++--------- test/spaces_inside_link_text.md | 5 +++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/md039.js b/lib/md039.js index 477cf3ed..9218f9b3 100644 --- a/lib/md039.js +++ b/lib/md039.js @@ -28,22 +28,28 @@ module.exports = { const right = linkText.trimRight().length !== linkText.length; if (left || right) { const line = params.lines[lineNumber - 1]; + let range = null; + let fixInfo = null; const match = line.slice(lineIndex).match(spaceInLinkRe); - const column = match.index + lineIndex + 1; - const length = match[0].length; - lineIndex = column + length - 1; + if (match) { + const column = match.index + lineIndex + 1; + const length = match[0].length; + range = [ column, length ]; + fixInfo = { + "editColumn": column + 1, + "deleteCount": length - 2, + "insertText": linkText.trim() + }; + lineIndex = column + length - 1; + } addErrorContext( onError, lineNumber, `[${linkText}]`, left, right, - [ column, length ], - { - "editColumn": column + 1, - "deleteCount": length - 2, - "insertText": linkText.trim() - } + range, + fixInfo ); } } else if ((type === "softbreak") || (type === "hardbreak")) { diff --git a/test/spaces_inside_link_text.md b/test/spaces_inside_link_text.md index 96335bb3..8e6baa15 100644 --- a/test/spaces_inside_link_text.md +++ b/test/spaces_inside_link_text.md @@ -45,3 +45,8 @@ function MoreCodeButNotCode(input) { [Links](ending) [with](spaces) [error ]({MD039}) + +Wrapped [ link with leading space + ](https://example.com) {MD039} + +Non-wrapped [ link with leading space](https://example.com) {MD039}