Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only group external links by url
  • Loading branch information
sorentwo committed Apr 26, 2025
commit d8941ee2f714475982df6fb65bb3034e645ada8c
4 changes: 2 additions & 2 deletions lib/ex_doc/formatter/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ defmodule ExDoc.Formatter.HTML do
defp build_extra({input, %{url: _} = input_options}, groups, _lang, _auto, _url_pattern) do
input = to_string(input)
title = input_options[:title] || input
group = GroupMatcher.match_extra(groups, {:url, input, input_options[:url]})
group = GroupMatcher.match_extra(groups, input_options[:url])

%{group: group, id: Utils.text_to_id(title), title: title, url: input_options[:url]}
end
Expand Down Expand Up @@ -439,7 +439,7 @@ defmodule ExDoc.Formatter.HTML do
title_html = title_ast && ExDoc.DocAST.to_string(title_ast)
content_html = autolink_and_render(ast, language, [file: input] ++ autolink_opts, opts)

group = GroupMatcher.match_extra(groups, {:path, input})
group = GroupMatcher.match_extra(groups, input)
title = input_options[:title] || title_text || filename_to_title(input)

source_path = source_file |> Path.relative_to(File.cwd!()) |> String.replace_leading("./", "")
Expand Down
11 changes: 1 addition & 10 deletions lib/ex_doc/group_matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule ExDoc.GroupMatcher do
@doc """
Finds a matching group for the given extra filename
"""
def match_extra(group_patterns, {:path, path}) do
def match_extra(group_patterns, path) do
match_group_patterns(group_patterns, fn pattern ->
case pattern do
%Regex{} = regex -> Regex.match?(regex, path)
Expand All @@ -57,15 +57,6 @@ defmodule ExDoc.GroupMatcher do
end)
end

def match_extra(group_patterns, {:url, title, url}) do
match_group_patterns(group_patterns, fn pattern ->
case pattern do
%Regex{} = regex -> Regex.match?(regex, title) or Regex.match?(regex, url)
string when is_binary(string) -> title == string or title == url
end
end)
end

defp match_group_patterns(group_patterns, matcher) do
Enum.find_value(group_patterns, fn {group, patterns} ->
patterns = List.wrap(patterns)
Expand Down
7 changes: 7 additions & 0 deletions lib/mix/tasks/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ defmodule Mix.Tasks.Docs do
"Advanced": ~r"/advanced/"
]

External extras from a URL can also be grouped:

groups_for_extras: [
"Elixir": ~r"https://elixir-lang.org/",
"Erlang": ~r"https://www.erlang.org/"
]

Similar can be done for modules:

groups_for_modules: [
Expand Down
25 changes: 10 additions & 15 deletions test/ex_doc/group_matcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,25 @@ defmodule ExDoc.GroupMatcherTest do

describe "extras matching" do
test "by string names" do
patterns = [
Group: ["docs/handling/testing.md"]
]
patterns = [Group: ["docs/handling/testing.md"]]

assert match_extra(patterns, {:path, "docs/handling/testing.md"}) == :Group
refute match_extra(patterns, {:path, "docs/handling/setup.md"})
assert match_extra(patterns, "docs/handling/testing.md") == :Group
refute match_extra(patterns, "docs/handling/setup.md")
end

test "by regular expressions" do
patterns = [
Group: ~r/docs\/handling?/
]
patterns = [Group: ~r/docs\/handling?/]

assert match_extra(patterns, {:path, "docs/handling/testing.md"}) == :Group
assert match_extra(patterns, {:path, "docs/handling/setup.md"}) == :Group
refute match_extra(patterns, {:path, "docs/introduction.md"})
assert match_extra(patterns, "docs/handling/testing.md") == :Group
assert match_extra(patterns, "docs/handling/setup.md") == :Group
refute match_extra(patterns, "docs/introduction.md")
end

test "for extras with a title and a url" do
test "for extras with a url" do
patterns = [Group: ~r/elixir/i]

assert match_extra(patterns, {:url, "Elixir Packages", "https://hex.pm"}) == :Group
assert match_extra(patterns, {:url, "Website", "https://elixir-lang.org"}) == :Group
refute match_extra(patterns, {:url, "Example", "https://example.com"})
assert match_extra(patterns, "https://elixir-lang.org") == :Group
refute match_extra(patterns, "https://example.com")
end
end
end