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
Expand extra matcher to handle grouping urls
  • Loading branch information
sorentwo committed Apr 15, 2025
commit 6b6cad62b7d5aea595d65604f1bcb0bef7d24dba
6 changes: 3 additions & 3 deletions lib/ex_doc/formatter/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ 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] || filename_to_title(input)
group = GroupMatcher.match_extra(groups, input)
title = input_options[:title] || input
group = GroupMatcher.match_extra(groups, {:url, input, 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, input)
group = GroupMatcher.match_extra(groups, {:path, 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
15 changes: 12 additions & 3 deletions lib/ex_doc/group_matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ defmodule ExDoc.GroupMatcher do
@doc """
Finds a matching group for the given extra filename
"""
def match_extra(group_patterns, filename) do
def match_extra(group_patterns, {:path, path}) do
match_group_patterns(group_patterns, fn pattern ->
case pattern do
%Regex{} = regex -> Regex.match?(regex, filename)
string when is_binary(string) -> filename == string
%Regex{} = regex -> Regex.match?(regex, path)
string when is_binary(string) -> path == string
end
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
Expand Down
34 changes: 34 additions & 0 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,40 @@ defmodule ExDoc.Formatter.HTMLTest do
] = Jason.decode!(content)["extras"]
end

test "with custom groups for external urls", %{tmp_dir: tmp_dir} = context do
extra_config = [
extras: [
Website: [url: "https://elixir-lang.org"],
Forum: [url: "https://elixirforum.com"]
],
groups_for_extras: [Elixir: ~r/elixir/i]
]

context
|> doc_config(extra_config)
|> generate_docs()

%{"extras" => extras} =
(tmp_dir <> "/html/dist/sidebar_items-*.js")
|> read_wildcard!()
|> String.trim_leading("sidebarNodes=")
|> Jason.decode!()

assert %{
"group" => "Elixir",
"id" => "website",
"title" => "Website",
"url" => "https://elixir-lang.org"
} in extras

assert %{
"group" => "Elixir",
"id" => "forum",
"title" => "Forum",
"url" => "https://elixirforum.com"
} in extras
end

test "with auto-extracted titles", %{tmp_dir: tmp_dir} = context do
generate_docs(doc_config(context, extras: ["test/fixtures/ExtraPage.md"]))
content = File.read!(tmp_dir <> "/html/extrapage.html")
Expand Down
18 changes: 13 additions & 5 deletions test/ex_doc/group_matcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,26 @@ defmodule ExDoc.GroupMatcherTest do
Group: ["docs/handling/testing.md"]
]

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

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

assert match_extra(patterns, "docs/handling/testing.md") == :Group
assert match_extra(patterns, "docs/handling/setup.md") == :Group
assert match_extra(patterns, "docs/introduction.md") == nil
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"})
end

test "for extras with a title and 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"})
end
end
end