Skip to content

Commit

Permalink
MD027: handle anchor elements correctly (#463)
Browse files Browse the repository at this point in the history
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 <markus.lehtonen@intel.com>
  • Loading branch information
marquiz committed Sep 6, 2023
1 parent 01a3955 commit 3d760ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/mdl/doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down
6 changes: 5 additions & 1 deletion lib/mdl/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/rule_tests/blockquote_spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d760ba

Please sign in to comment.