Skip to content

Commit

Permalink
Escaped characters in alt text of image links are now unescaped
Browse files Browse the repository at this point in the history
This fixes #135 insofar that there is a working solution by
escaping the pipe characters.
  • Loading branch information
gettalong committed Jun 14, 2014
1 parent c8779f4 commit 5d80e91
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/kramdown/converter/kramdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,17 @@ def convert_a(el, opts)
end

def convert_img(el, opts)
alt_text = el.attr['alt'].gsub(ESCAPED_CHAR_RE) { $1 ? "\\#{$1}" : $2 }
if el.attr['src'].empty?
"![#{el.attr['alt']}]()"
"![#{alt_text}]()"
else
title = (el.attr['title'] ? ' "' + el.attr['title'].gsub(/"/, """) + '"' : '')
link = if el.attr['src'].count("()") > 0
"<#{el.attr['src']}>"
else
el.attr['src']
end
"![#{el.attr['alt']}](#{link}#{title})"
"![#{alt_text}](#{link}#{title})"
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/kramdown/parser/kramdown/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#++
#

require 'kramdown/parser/kramdown/escaped_chars'

module Kramdown
module Parser
class Kramdown
Expand Down Expand Up @@ -76,7 +78,7 @@ def parse_link
add_text(result)
return
end
alt_text = extract_string(reset_pos...@src.pos, @src)
alt_text = extract_string(reset_pos...@src.pos, @src).gsub(ESCAPED_CHARS, '\1')
@src.scan(LINK_BRACKET_STOP_RE)

# reference style link or no link url
Expand Down
1 change: 1 addition & 0 deletions test/testcases/span/01_link/imagelinks.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@

<p>No id: <img src="other.png" alt="imgo" title="Title" /></p>

<p>With escaped pipe: <img src="other.png" alt="an | pipe" /></p>
2 changes: 2 additions & 0 deletions test/testcases/span/01_link/imagelinks.text
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ No id: ![imgo]

[img]: other.png
[imgo]: other.png "Title"

With escaped pipe: ![an \| pipe](other.png)

0 comments on commit 5d80e91

Please sign in to comment.