Skip to content

Commit

Permalink
Allow anchors on function and callback autolinks (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust authored Jun 21, 2024
1 parent e54c4e7 commit 068ba02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/ex_doc/autolink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,22 @@ defmodule ExDoc.Autolink do
end

defp parse_url(string, mode, config) do
case Regex.run(~r{^(.+)/(\d+)$}, string) do
[_, left, right] ->
case Regex.run(~r{^(.+)/(\d+)(#.*)?$}, string) do
[_, left, right | maybe_fragment] ->
with {:ok, arity} <- parse_arity(right) do
{kind, rest} = kind(left)

case config.language.parse_module_function(rest) do
{:local, function} ->
kind
|> local_url(function, arity, config, string, mode: mode)
|> maybe_append_nested_fragment(maybe_fragment)
|> maybe_remove_link(mode)

{:remote, module, function} ->
{kind, module, function, arity}
|> remote_url(config, string, mode: mode)
|> maybe_append_nested_fragment(maybe_fragment)
|> maybe_remove_link(mode)

:error ->
Expand Down Expand Up @@ -611,6 +613,10 @@ defmodule ExDoc.Autolink do
def format_visibility(:undefined, _kind), do: "undefined or private"
def format_visibility(visibility, _kind), do: "#{visibility}"

defp maybe_append_nested_fragment(nil, _), do: nil
defp maybe_append_nested_fragment(url, []), do: url
defp maybe_append_nested_fragment(url, ["#" <> fragment]), do: url <> "-" <> fragment

defp append_fragment(url, nil), do: url
defp append_fragment(url, fragment), do: url <> "#" <> fragment
end
5 changes: 5 additions & 0 deletions test/ex_doc/language/elixir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ defmodule ExDoc.Language.ElixirTest do
~s|<a href="https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3"><code class="inline">GenServer.handle_call/3</code></a>|
end

test "elixir callback fragment" do
assert autolink_doc("`c:GenServer.handle_call/3#fragment`") ==
~s|<a href="https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3-fragment"><code class="inline">GenServer.handle_call/3</code></a>|
end

test "erlang callback" do
assert autolink_doc("`c::gen_server.handle_call/3`") ==
~s|<a href="https://www.erlang.org/doc/apps/stdlib/gen_server.html#c:handle_call/3"><code class="inline">:gen_server.handle_call/3</code></a>|
Expand Down

0 comments on commit 068ba02

Please sign in to comment.