Skip to content

Commit a91e1f2

Browse files
committed
underlines within links have to be escaped
in links the underline will be escaped with the ansi escape sequence code `4` (underline). To avoid this behavior it have to be properly escaped.
1 parent 174f031 commit a91e1f2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/iex/lib/iex/ansi_docs.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ defmodule IEx.ANSIDocs do
253253
end
254254

255255
defp handle_links(text) do
256+
text
257+
|> remove_square_brackets_in_link
258+
|> escape_underlines_in_link
259+
end
260+
261+
defp escape_underlines_in_link(text) do
262+
case Regex.match?(%r{.*(https?\S*)}, text) do
263+
true ->
264+
Regex.replace(%r{_}, text, "\\\\_")
265+
_ ->
266+
text
267+
end
268+
end
269+
270+
defp remove_square_brackets_in_link(text) do
256271
Regex.replace(%r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
257272
end
258273

lib/iex/test/iex/ansi_docs_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,11 @@ defmodule IEx.AnsiDocsTest do
194194
result = format("(`hello world`)")
195195
assert result == "(\e[36mhello world\e[0m)\n\e[0m"
196196
end
197+
198+
test "escaping of underlines within links" do
199+
result = format("(http://en.wikipedia.org/wiki/ANSI_escape_code)")
200+
assert result == "(http://en.wikipedia.org/wiki/ANSI_escape_code)\n\e[0m"
201+
result = format("[ANSI escape code](http://en.wikipedia.org/wiki/ANSI_escape_code)")
202+
assert result == "ANSI escape code (http://en.wikipedia.org/wiki/ANSI_escape_code)\n\e[0m"
203+
end
197204
end

0 commit comments

Comments
 (0)