Skip to content

Allow anchors on function and callback autolinks. #1921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading