From 3d760ba9e47d356cabc548c46c99a5d25865cb03 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Wed, 6 Sep 2023 23:58:58 +0300 Subject: [PATCH] MD027: handle anchor elements correctly (#463) Fixex false positives of MD027 multiple-spaces-after-blockquote-symbol if quote lines begin with a link, e.g. > This is a > [link](link) to somewhere Signed-off-by: Markus Lehtonen --- lib/mdl/doc.rb | 25 +++++++++++++++++++++++++ lib/mdl/rules.rb | 6 +++++- test/rule_tests/blockquote_spaces.md | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/mdl/doc.rb b/lib/mdl/doc.rb index 844f4741..6c35678b 100644 --- a/lib/mdl/doc.rb +++ b/lib/mdl/doc.rb @@ -268,6 +268,31 @@ def extract_text(element, prefix = '', restore_whitespace = true) lines end + ## + # Returns the element as plaintext + + def extract_as_text(element) + quotes = { + :rdquo => '"', + :ldquo => '"', + :lsquo => "'", + :rsquo => "'", + } + # If anything goes amiss here, e.g. unknown type, then nil will be + # returned and we'll just not catch that part of the text, which seems + # like a sensible failure mode. + lines = element.children.map do |e| + if e.type == :text or e.type == :codespan + e.value + elsif %i{strong em p a}.include?(e.type) + extract_as_text(e).join("\n") + elsif e.type == :smart_quote + quotes[e.value] + end + end.join.split("\n") + lines + end + private ## diff --git a/lib/mdl/rules.rb b/lib/mdl/rules.rb index 4544611f..cc1dfff5 100644 --- a/lib/mdl/rules.rb +++ b/lib/mdl/rules.rb @@ -458,7 +458,11 @@ errors = [] doc.find_type_elements(:blockquote).each do |e| linenum = doc.element_linenumber(e) - lines = doc.extract_text(e, /^\s*> /) + lines = doc.extract_as_text(e) + # Handle first line specially as whitespace is stripped from the text element + if doc.element_line(e).match(/^\s*> /) + errors << linenum + end lines.each do |line| errors << linenum if line.start_with?(' ') linenum += 1 diff --git a/test/rule_tests/blockquote_spaces.md b/test/rule_tests/blockquote_spaces.md index 214a3a46..a54cf66c 100644 --- a/test/rule_tests/blockquote_spaces.md +++ b/test/rule_tests/blockquote_spaces.md @@ -17,6 +17,8 @@ This tests other things embedded in the blockquote: > **bar** more text > 'baz' more text > `qux` more text +> [link](example.com) to site +> [link](#link) {MD027} > > - foo