Skip to content

Commit 85f445b

Browse files
committed
Merge pull request #454 from padde/fix-autolink-anchors
Fix autolink anchors for non URL safe function names
2 parents 6334219 + 62c6601 commit 85f445b

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/ex_doc/formatter/html/autolink.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ defmodule ExDoc.Formatter.HTML.Autolink do
208208
|> List.flatten()
209209
|> Enum.filter(&(&1 in locals))
210210
|> Enum.reduce(bin, fn (x, acc) ->
211+
{prefix, _, function_name, arity} = split_function(x)
211212
escaped = Regex.escape(x)
212-
Regex.replace(~r/(?<!\[)`(\s*(#{escaped})\s*)`(?!\])/, acc, "[`\\1`](#\\2)")
213+
Regex.replace(~r/(?<!\[)`(\s*#{escaped}\s*)`(?!\])/, acc,
214+
"[`#{function_name}/#{arity}`](##{prefix}#{enc_h function_name}/#{arity})")
213215
end)
214216
end
215217

@@ -250,7 +252,7 @@ defmodule ExDoc.Formatter.HTML.Autolink do
250252
{prefix, mod_str, function_name, arity} = split_function(x)
251253
escaped = Regex.escape(x)
252254
Regex.replace(~r/(?<!\[)`(\s*#{escaped}\s*)`(?!\])/, acc,
253-
"[`#{mod_str}.#{function_name}/#{arity}`](#{mod_str}.html##{prefix}#{function_name}/#{arity})")
255+
"[`#{mod_str}.#{function_name}/#{arity}`](#{mod_str}.html##{prefix}#{enc_h function_name}/#{arity})")
254256
end)
255257
end
256258

test/ex_doc/formatter/html/autolink_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule ExDoc.Formatter.HTML.AutolinkTest do
1010
assert Autolink.local_doc("`example/2` then `example/2`",
1111
["example/2"]) == "[`example/2`](#example/2) then [`example/2`](#example/2)"
1212
assert Autolink.local_doc("` spaces/0 `", ["spaces/0"]) ==
13-
"[` spaces/0 `](#spaces/0)"
13+
"[`spaces/0`](#spaces/0)"
1414
assert Autolink.local_doc("`example/1` and `example/2`",
1515
["example/1", "example/2"]) == "[`example/1`](#example/1) and [`example/2`](#example/2)"
1616
assert Autolink.local_doc("`funny_name\?/1` and `funny_name!/2`",
@@ -33,6 +33,7 @@ defmodule ExDoc.Formatter.HTML.AutolinkTest do
3333
assert Autolink.local_doc("`!/1`", ["!/1"]) === "[`!/1`](#!/1)"
3434
assert Autolink.local_doc("`../2`", ["../2"]) === "[`../2`](#../2)"
3535
assert Autolink.local_doc("`--/2`", ["--/2"]) === "[`--/2`](#--/2)"
36+
assert Autolink.local_doc("`<<>>/1`", ["<<>>/1"]) === "[`<<>>/1`](#%3C%3C%3E%3E/1)"
3637
end
3738

3839
# project_functions
@@ -61,6 +62,7 @@ defmodule ExDoc.Formatter.HTML.AutolinkTest do
6162
assert Autolink.project_functions("`Mod.!/1`", ["Mod.!/1"]) === "[`Mod.!/1`](Mod.html#!/1)"
6263
assert Autolink.project_functions("`Mod.../2`", ["Mod.../2"]) === "[`Mod.../2`](Mod.html#../2)"
6364
assert Autolink.project_functions("`Mod.--/2`", ["Mod.--/2"]) === "[`Mod.--/2`](Mod.html#--/2)"
65+
assert Autolink.project_functions("`Mod.<<>>/1`", ["Mod.<<>>/1"]) === "[`Mod.<<>>/1`](Mod.html#%3C%3C%3E%3E/1)"
6466
end
6567

6668
test "autolink creates links for callbacks" do

0 commit comments

Comments
 (0)